* elf64-sparc.c (sparc64_elf_relocate_section): Ignore overflows
[platform/upstream/binutils.git] / bfd / elf32-vax.c
1 /* VAX series support for 32-bit ELF
2    Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004 Free Software Foundation, Inc.
4    Contributed by Matt Thomas <matt@3am-software.com>.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/vax.h"
28
29 static reloc_howto_type *reloc_type_lookup
30   PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void rtype_to_howto
32   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
33 static struct bfd_hash_entry *elf_vax_link_hash_newfunc
34   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
35 static struct bfd_link_hash_table *elf_vax_link_hash_table_create
36   PARAMS ((bfd *));
37 static bfd_boolean elf_vax_check_relocs
38   PARAMS ((bfd *, struct bfd_link_info *, asection *,
39            const Elf_Internal_Rela *));
40 static asection *elf_vax_gc_mark_hook
41   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
42            struct elf_link_hash_entry *, Elf_Internal_Sym *));
43 static bfd_boolean elf_vax_gc_sweep_hook
44   PARAMS ((bfd *, struct bfd_link_info *, asection *,
45            const Elf_Internal_Rela *));
46 static bfd_boolean elf_vax_adjust_dynamic_symbol
47   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
48 static bfd_boolean elf_vax_size_dynamic_sections
49   PARAMS ((bfd *, struct bfd_link_info *));
50 static bfd_boolean elf_vax_relocate_section
51   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
52            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
53 static bfd_boolean elf_vax_finish_dynamic_symbol
54   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
55            Elf_Internal_Sym *));
56 static bfd_boolean elf_vax_finish_dynamic_sections
57   PARAMS ((bfd *, struct bfd_link_info *));
58
59 static bfd_boolean elf32_vax_set_private_flags
60   PARAMS ((bfd *, flagword));
61 static bfd_boolean elf32_vax_merge_private_bfd_data
62   PARAMS ((bfd *, bfd *));
63 static bfd_boolean elf32_vax_print_private_bfd_data
64   PARAMS ((bfd *, PTR));
65
66 static reloc_howto_type howto_table[] = {
67   HOWTO (R_VAX_NONE,            /* type */
68          0,                     /* rightshift */
69          0,                     /* size (0 = byte, 1 = short, 2 = long) */
70          0,                     /* bitsize */
71          FALSE,                 /* pc_relative */
72          0,                     /* bitpos */
73          complain_overflow_dont, /* complain_on_overflow */
74          bfd_elf_generic_reloc, /* special_function */
75          "R_VAX_NONE",          /* name */
76          FALSE,                 /* partial_inplace */
77          0,                     /* src_mask */
78          0x00000000,            /* dst_mask */
79          FALSE),                /* pcrel_offset */
80
81   HOWTO (R_VAX_32,              /* type */
82          0,                     /* rightshift */
83          2,                     /* size (0 = byte, 1 = short, 2 = long) */
84          32,                    /* bitsize */
85          FALSE,                 /* pc_relative */
86          0,                     /* bitpos */
87          complain_overflow_bitfield, /* complain_on_overflow */
88          bfd_elf_generic_reloc, /* special_function */
89          "R_VAX_32",            /* name */
90          FALSE,                 /* partial_inplace */
91          0,                     /* src_mask */
92          0xffffffff,            /* dst_mask */
93          FALSE),                /* pcrel_offset */
94
95   HOWTO (R_VAX_16,              /* type */
96          0,                     /* rightshift */
97          1,                     /* size (0 = byte, 1 = short, 2 = long) */
98          16,                    /* bitsize */
99          FALSE,                 /* pc_relative */
100          0,                     /* bitpos */
101          complain_overflow_bitfield, /* complain_on_overflow */
102          bfd_elf_generic_reloc, /* special_function */
103          "R_VAX_16",            /* name */
104          FALSE,                 /* partial_inplace */
105          0,                     /* src_mask */
106          0x0000ffff,            /* dst_mask */
107          FALSE),                /* pcrel_offset */
108
109   HOWTO (R_VAX_8,               /* type */
110          0,                     /* rightshift */
111          0,                     /* size (0 = byte, 1 = short, 2 = long) */
112          8,                     /* bitsize */
113          FALSE,                 /* pc_relative */
114          0,                     /* bitpos */
115          complain_overflow_bitfield, /* complain_on_overflow */
116          bfd_elf_generic_reloc, /* special_function */
117          "R_VAX_8",             /* name */
118          FALSE,                 /* partial_inplace */
119          0,                     /* src_mask */
120          0x000000ff,            /* dst_mask */
121          FALSE),                /* pcrel_offset */
122
123   HOWTO (R_VAX_PC32,            /* type */
124          0,                     /* rightshift */
125          2,                     /* size (0 = byte, 1 = short, 2 = long) */
126          32,                    /* bitsize */
127          TRUE,                  /* pc_relative */
128          0,                     /* bitpos */
129          complain_overflow_bitfield, /* complain_on_overflow */
130          bfd_elf_generic_reloc, /* special_function */
131          "R_VAX_PC32",          /* name */
132          FALSE,                 /* partial_inplace */
133          0,                     /* src_mask */
134          0xffffffff,            /* dst_mask */
135          TRUE),                 /* pcrel_offset */
136
137   HOWTO (R_VAX_PC16,            /* type */
138          0,                     /* rightshift */
139          1,                     /* size (0 = byte, 1 = short, 2 = long) */
140          16,                    /* bitsize */
141          TRUE,                  /* pc_relative */
142          0,                     /* bitpos */
143          complain_overflow_signed, /* complain_on_overflow */
144          bfd_elf_generic_reloc, /* special_function */
145          "R_VAX_PC16",          /* name */
146          FALSE,                 /* partial_inplace */
147          0,                     /* src_mask */
148          0x0000ffff,            /* dst_mask */
149          TRUE),                 /* pcrel_offset */
150
151   HOWTO (R_VAX_PC8,             /* type */
152          0,                     /* rightshift */
153          0,                     /* size (0 = byte, 1 = short, 2 = long) */
154          8,                     /* bitsize */
155          TRUE,                  /* pc_relative */
156          0,                     /* bitpos */
157          complain_overflow_signed, /* complain_on_overflow */
158          bfd_elf_generic_reloc, /* special_function */
159          "R_VAX_PC8",           /* name */
160          FALSE,                 /* partial_inplace */
161          0,                     /* src_mask */
162          0x000000ff,            /* dst_mask */
163          TRUE),                 /* pcrel_offset */
164
165   HOWTO (R_VAX_GOT32,           /* type */
166          0,                     /* rightshift */
167          2,                     /* size (0 = byte, 1 = short, 2 = long) */
168          32,                    /* bitsize */
169          TRUE,                  /* pc_relative */
170          0,                     /* bitpos */
171          complain_overflow_bitfield, /* complain_on_overflow */
172          bfd_elf_generic_reloc, /* special_function */
173          "R_VAX_GOT32",         /* name */
174          FALSE,                 /* partial_inplace */
175          0,                     /* src_mask */
176          0xffffffff,            /* dst_mask */
177          TRUE),                 /* pcrel_offset */
178
179   EMPTY_HOWTO (-1),
180   EMPTY_HOWTO (-1),
181   EMPTY_HOWTO (-1),
182   EMPTY_HOWTO (-1),
183   EMPTY_HOWTO (-1),
184
185   HOWTO (R_VAX_PLT32,           /* type */
186          0,                     /* rightshift */
187          2,                     /* size (0 = byte, 1 = short, 2 = long) */
188          32,                    /* bitsize */
189          TRUE,                  /* pc_relative */
190          0,                     /* bitpos */
191          complain_overflow_bitfield, /* complain_on_overflow */
192          bfd_elf_generic_reloc, /* special_function */
193          "R_VAX_PLT32",         /* name */
194          FALSE,                 /* partial_inplace */
195          0,                     /* src_mask */
196          0xffffffff,            /* dst_mask */
197          TRUE),                 /* pcrel_offset */
198
199   EMPTY_HOWTO (-1),
200   EMPTY_HOWTO (-1),
201   EMPTY_HOWTO (-1),
202   EMPTY_HOWTO (-1),
203   EMPTY_HOWTO (-1),
204
205   HOWTO (R_VAX_COPY,            /* type */
206          0,                     /* rightshift */
207          0,                     /* size (0 = byte, 1 = short, 2 = long) */
208          0,                     /* bitsize */
209          FALSE,                 /* pc_relative */
210          0,                     /* bitpos */
211          complain_overflow_dont, /* complain_on_overflow */
212          bfd_elf_generic_reloc, /* special_function */
213          "R_VAX_COPY",          /* name */
214          FALSE,                 /* partial_inplace */
215          0,                     /* src_mask */
216          0xffffffff,            /* dst_mask */
217          FALSE),                /* pcrel_offset */
218
219   HOWTO (R_VAX_GLOB_DAT,        /* type */
220          0,                     /* rightshift */
221          2,                     /* size (0 = byte, 1 = short, 2 = long) */
222          32,                    /* bitsize */
223          FALSE,                 /* pc_relative */
224          0,                     /* bitpos */
225          complain_overflow_dont, /* complain_on_overflow */
226          bfd_elf_generic_reloc, /* special_function */
227          "R_VAX_GLOB_DAT",      /* name */
228          FALSE,                 /* partial_inplace */
229          0,                     /* src_mask */
230          0xffffffff,            /* dst_mask */
231          FALSE),                /* pcrel_offset */
232
233   HOWTO (R_VAX_JMP_SLOT,        /* type */
234          0,                     /* rightshift */
235          2,                     /* size (0 = byte, 1 = short, 2 = long) */
236          32,                    /* bitsize */
237          FALSE,                 /* pc_relative */
238          0,                     /* bitpos */
239          complain_overflow_dont, /* complain_on_overflow */
240          bfd_elf_generic_reloc, /* special_function */
241          "R_VAX_JMP_SLOT",      /* name */
242          FALSE,                 /* partial_inplace */
243          0,                     /* src_mask */
244          0xffffffff,            /* dst_mask */
245          FALSE),                /* pcrel_offset */
246
247   HOWTO (R_VAX_RELATIVE,        /* 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_dont, /* complain_on_overflow */
254          bfd_elf_generic_reloc, /* special_function */
255          "R_VAX_RELATIVE",      /* name */
256          FALSE,                 /* partial_inplace */
257          0,                     /* src_mask */
258          0xffffffff,            /* dst_mask */
259          FALSE),                /* pcrel_offset */
260
261   /* GNU extension to record C++ vtable hierarchy */
262   HOWTO (R_VAX_GNU_VTINHERIT,   /* type */
263          0,                     /* rightshift */
264          2,                     /* size (0 = byte, 1 = short, 2 = long) */
265          0,                     /* bitsize */
266          FALSE,                 /* pc_relative */
267          0,                     /* bitpos */
268          complain_overflow_dont, /* complain_on_overflow */
269          NULL,                  /* special_function */
270          "R_VAX_GNU_VTINHERIT", /* name */
271          FALSE,                 /* partial_inplace */
272          0,                     /* src_mask */
273          0,                     /* dst_mask */
274          FALSE),                /* pcrel_offset */
275
276   /* GNU extension to record C++ vtable member usage */
277   HOWTO (R_VAX_GNU_VTENTRY,     /* type */
278          0,                     /* rightshift */
279          2,                     /* size (0 = byte, 1 = short, 2 = long) */
280          0,                     /* bitsize */
281          FALSE,                 /* pc_relative */
282          0,                     /* bitpos */
283          complain_overflow_dont, /* complain_on_overflow */
284          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
285          "R_VAX_GNU_VTENTRY",   /* name */
286          FALSE,                 /* partial_inplace */
287          0,                     /* src_mask */
288          0,                     /* dst_mask */
289          FALSE),                /* pcrel_offset */
290 };
291
292 static void
293 rtype_to_howto (abfd, cache_ptr, dst)
294      bfd *abfd ATTRIBUTE_UNUSED;
295      arelent *cache_ptr;
296      Elf_Internal_Rela *dst;
297 {
298   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_VAX_max);
299   cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
300 }
301
302 #define elf_info_to_howto rtype_to_howto
303
304 static const struct
305 {
306   bfd_reloc_code_real_type bfd_val;
307   int elf_val;
308 } reloc_map[] = {
309   { BFD_RELOC_NONE, R_VAX_NONE },
310   { BFD_RELOC_32, R_VAX_32 },
311   { BFD_RELOC_16, R_VAX_16 },
312   { BFD_RELOC_8, R_VAX_8 },
313   { BFD_RELOC_32_PCREL, R_VAX_PC32 },
314   { BFD_RELOC_16_PCREL, R_VAX_PC16 },
315   { BFD_RELOC_8_PCREL, R_VAX_PC8 },
316   { BFD_RELOC_32_GOT_PCREL, R_VAX_GOT32 },
317   { BFD_RELOC_32_PLT_PCREL, R_VAX_PLT32 },
318   { BFD_RELOC_NONE, R_VAX_COPY },
319   { BFD_RELOC_VAX_GLOB_DAT, R_VAX_GLOB_DAT },
320   { BFD_RELOC_VAX_JMP_SLOT, R_VAX_JMP_SLOT },
321   { BFD_RELOC_VAX_RELATIVE, R_VAX_RELATIVE },
322   { BFD_RELOC_CTOR, R_VAX_32 },
323   { BFD_RELOC_VTABLE_INHERIT, R_VAX_GNU_VTINHERIT },
324   { BFD_RELOC_VTABLE_ENTRY, R_VAX_GNU_VTENTRY },
325 };
326
327 static reloc_howto_type *
328 reloc_type_lookup (abfd, code)
329      bfd *abfd ATTRIBUTE_UNUSED;
330      bfd_reloc_code_real_type code;
331 {
332   unsigned int i;
333   for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
334     {
335       if (reloc_map[i].bfd_val == code)
336         return &howto_table[reloc_map[i].elf_val];
337     }
338   return 0;
339 }
340
341 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
342 #define ELF_ARCH bfd_arch_vax
343 /* end code generated by elf.el */
344 \f
345 /* Functions for the VAX ELF linker.  */
346
347 /* The name of the dynamic interpreter.  This is put in the .interp
348    section.  */
349
350 #define ELF_DYNAMIC_INTERPRETER "/usr/libexec/ld.elf_so"
351
352 /* The size in bytes of an entry in the procedure linkage table.  */
353
354 #define PLT_ENTRY_SIZE 12
355
356 /* The first entry in a procedure linkage table looks like this.  See
357    the SVR4 ABI VAX supplement to see how this works.  */
358
359 static const bfd_byte elf_vax_plt0_entry[PLT_ENTRY_SIZE] =
360 {
361   0xdd, 0xef,           /* pushl l^ */
362   0, 0, 0, 0,           /* offset to .plt.got + 4 */
363   0x17, 0xff,           /* jmp @L^(pc) */
364   0, 0, 0, 0,           /* offset to .plt.got + 8 */
365 };
366
367 /* Subsequent entries in a procedure linkage table look like this.  */
368
369 static const bfd_byte elf_vax_plt_entry[PLT_ENTRY_SIZE] =
370 {
371   0x40, 0x00,           /* .word ^M<r6> */
372   0x16, 0xef,           /* jsb L^(pc) */
373   0, 0, 0, 0,           /* replaced with offset to start of .plt  */
374   0, 0, 0, 0,           /* index into .rela.plt */
375 };
376
377 /* The VAX linker needs to keep track of the number of relocs that it
378    decides to copy in check_relocs for each symbol.  This is so that it
379    can discard PC relative relocs if it doesn't need them when linking
380    with -Bsymbolic.  We store the information in a field extending the
381    regular ELF linker hash table.  */
382
383 /* This structure keeps track of the number of PC relative relocs we have
384    copied for a given symbol.  */
385
386 struct elf_vax_pcrel_relocs_copied
387 {
388   /* Next section.  */
389   struct elf_vax_pcrel_relocs_copied *next;
390   /* A section in dynobj.  */
391   asection *section;
392   /* Number of relocs copied in this section.  */
393   bfd_size_type count;
394 };
395
396 /* VAX ELF linker hash entry.  */
397
398 struct elf_vax_link_hash_entry
399 {
400   struct elf_link_hash_entry root;
401
402   /* Number of PC relative relocs copied for this symbol.  */
403   struct elf_vax_pcrel_relocs_copied *pcrel_relocs_copied;
404
405   bfd_vma got_addend;
406 };
407
408 /* VAX ELF linker hash table.  */
409
410 struct elf_vax_link_hash_table
411 {
412   struct elf_link_hash_table root;
413 };
414
415 /* Declare this now that the above structures are defined.  */
416
417 static bfd_boolean elf_vax_discard_copies
418   PARAMS ((struct elf_vax_link_hash_entry *, PTR));
419
420 /* Declare this now that the above structures are defined.  */
421
422 static bfd_boolean elf_vax_instantiate_got_entries
423   PARAMS ((struct elf_link_hash_entry *, PTR));
424
425 /* Traverse an VAX ELF linker hash table.  */
426
427 #define elf_vax_link_hash_traverse(table, func, info)                   \
428   (elf_link_hash_traverse                                               \
429    (&(table)->root,                                                     \
430     (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
431     (info)))
432
433 /* Get the VAX ELF linker hash table from a link_info structure.  */
434
435 #define elf_vax_hash_table(p) \
436   ((struct elf_vax_link_hash_table *) (p)->hash)
437
438 /* Create an entry in an VAX ELF linker hash table.  */
439
440 static struct bfd_hash_entry *
441 elf_vax_link_hash_newfunc (entry, table, string)
442      struct bfd_hash_entry *entry;
443      struct bfd_hash_table *table;
444      const char *string;
445 {
446   struct elf_vax_link_hash_entry *ret =
447     (struct elf_vax_link_hash_entry *) entry;
448
449   /* Allocate the structure if it has not already been allocated by a
450      subclass.  */
451   if (ret == (struct elf_vax_link_hash_entry *) NULL)
452     ret = ((struct elf_vax_link_hash_entry *)
453            bfd_hash_allocate (table,
454                               sizeof (struct elf_vax_link_hash_entry)));
455   if (ret == (struct elf_vax_link_hash_entry *) NULL)
456     return (struct bfd_hash_entry *) ret;
457
458   /* Call the allocation method of the superclass.  */
459   ret = ((struct elf_vax_link_hash_entry *)
460          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
461                                      table, string));
462   if (ret != (struct elf_vax_link_hash_entry *) NULL)
463     {
464       ret->pcrel_relocs_copied = NULL;
465     }
466
467   return (struct bfd_hash_entry *) ret;
468 }
469
470 /* Create an VAX ELF linker hash table.  */
471
472 static struct bfd_link_hash_table *
473 elf_vax_link_hash_table_create (abfd)
474      bfd *abfd;
475 {
476   struct elf_vax_link_hash_table *ret;
477   bfd_size_type amt = sizeof (struct elf_vax_link_hash_table);
478
479   ret = (struct elf_vax_link_hash_table *) bfd_malloc (amt);
480   if (ret == (struct elf_vax_link_hash_table *) NULL)
481     return NULL;
482
483   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
484                                        elf_vax_link_hash_newfunc))
485     {
486       free (ret);
487       return NULL;
488     }
489
490   return &ret->root.root;
491 }
492
493 /* Keep vax-specific flags in the ELF header */
494 static bfd_boolean
495 elf32_vax_set_private_flags (abfd, flags)
496      bfd *abfd;
497      flagword flags;
498 {
499   elf_elfheader (abfd)->e_flags = flags;
500   elf_flags_init (abfd) = TRUE;
501   return TRUE;
502 }
503
504 /* Merge backend specific data from an object file to the output
505    object file when linking.  */
506 static bfd_boolean
507 elf32_vax_merge_private_bfd_data (ibfd, obfd)
508      bfd *ibfd;
509      bfd *obfd;
510 {
511   flagword out_flags;
512   flagword in_flags;
513
514   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
515       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
516     return TRUE;
517
518   in_flags  = elf_elfheader (ibfd)->e_flags;
519   out_flags = elf_elfheader (obfd)->e_flags;
520
521   if (!elf_flags_init (obfd))
522     {
523       elf_flags_init (obfd) = TRUE;
524       elf_elfheader (obfd)->e_flags = in_flags;
525     }
526
527   return TRUE;
528 }
529
530 /* Display the flags field */
531 static bfd_boolean
532 elf32_vax_print_private_bfd_data (abfd, ptr)
533      bfd *abfd;
534      PTR ptr;
535 {
536   FILE *file = (FILE *) ptr;
537
538   BFD_ASSERT (abfd != NULL && ptr != NULL);
539
540   /* Print normal ELF private data.  */
541   _bfd_elf_print_private_bfd_data (abfd, ptr);
542
543   /* Ignore init flag - it may not be set, despite the flags field containing valid data.  */
544
545   /* xgettext:c-format */
546   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
547
548   if (elf_elfheader (abfd)->e_flags & EF_VAX_NONPIC)
549     fprintf (file, _(" [nonpic]"));
550
551   if (elf_elfheader (abfd)->e_flags & EF_VAX_DFLOAT)
552     fprintf (file, _(" [d-float]"));
553
554   if (elf_elfheader (abfd)->e_flags & EF_VAX_GFLOAT)
555     fprintf (file, _(" [g-float]"));
556
557   fputc ('\n', file);
558
559   return TRUE;
560 }
561 /* Look through the relocs for a section during the first phase, and
562    allocate space in the global offset table or procedure linkage
563    table.  */
564
565 static bfd_boolean
566 elf_vax_check_relocs (abfd, info, sec, relocs)
567      bfd *abfd;
568      struct bfd_link_info *info;
569      asection *sec;
570      const Elf_Internal_Rela *relocs;
571 {
572   bfd *dynobj;
573   Elf_Internal_Shdr *symtab_hdr;
574   struct elf_link_hash_entry **sym_hashes;
575   const Elf_Internal_Rela *rel;
576   const Elf_Internal_Rela *rel_end;
577   asection *sgot;
578   asection *srelgot;
579   asection *sreloc;
580
581   if (info->relocatable)
582     return TRUE;
583
584   dynobj = elf_hash_table (info)->dynobj;
585   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
586   sym_hashes = elf_sym_hashes (abfd);
587
588   sgot = NULL;
589   srelgot = NULL;
590   sreloc = NULL;
591
592   rel_end = relocs + sec->reloc_count;
593   for (rel = relocs; rel < rel_end; rel++)
594     {
595       unsigned long r_symndx;
596       struct elf_link_hash_entry *h;
597
598       r_symndx = ELF32_R_SYM (rel->r_info);
599
600       if (r_symndx < symtab_hdr->sh_info)
601         h = NULL;
602       else
603         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
604
605       switch (ELF32_R_TYPE (rel->r_info))
606         {
607         case R_VAX_GOT32:
608           if (h != NULL
609               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
610             break;
611
612           /* This symbol requires a global offset table entry.  */
613
614           if (dynobj == NULL)
615             {
616               /* Create the .got section.  */
617               elf_hash_table (info)->dynobj = dynobj = abfd;
618               if (!_bfd_elf_create_got_section (dynobj, info))
619                 return FALSE;
620             }
621
622           if (sgot == NULL)
623             {
624               sgot = bfd_get_section_by_name (dynobj, ".got");
625               BFD_ASSERT (sgot != NULL);
626             }
627
628           if (srelgot == NULL
629               && (h != NULL || info->shared))
630             {
631               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
632               if (srelgot == NULL)
633                 {
634                   srelgot = bfd_make_section (dynobj, ".rela.got");
635                   if (srelgot == NULL
636                       || !bfd_set_section_flags (dynobj, srelgot,
637                                                  (SEC_ALLOC
638                                                   | SEC_LOAD
639                                                   | SEC_HAS_CONTENTS
640                                                   | SEC_IN_MEMORY
641                                                   | SEC_LINKER_CREATED
642                                                   | SEC_READONLY))
643                       || !bfd_set_section_alignment (dynobj, srelgot, 2))
644                     return FALSE;
645                 }
646             }
647
648           if (h != NULL)
649             {
650               struct elf_vax_link_hash_entry *eh;
651
652               eh = (struct elf_vax_link_hash_entry *) h;
653               if (h->got.refcount == -1)
654                 {
655                   h->got.refcount = 1;
656                   eh->got_addend = rel->r_addend;
657                 }
658               else
659                 {
660                   h->got.refcount++;
661                   if (eh->got_addend != (bfd_vma) rel->r_addend)
662                     (*_bfd_error_handler)
663                       (_("%s: warning: GOT addend of %ld to `%s' does not match previous GOT addend of %ld"),
664                               bfd_get_filename (abfd), rel->r_addend,
665                               h->root.root.string,
666                               eh->got_addend);
667
668                 }
669             }
670           break;
671
672         case R_VAX_PLT32:
673           /* This symbol requires a procedure linkage table entry.  We
674              actually build the entry in adjust_dynamic_symbol,
675              because this might be a case of linking PIC code which is
676              never referenced by a dynamic object, in which case we
677              don't need to generate a procedure linkage table entry
678              after all.  */
679
680           /* If this is a local symbol, we resolve it directly without
681              creating a procedure linkage table entry.  */
682           if (h == NULL)
683             continue;
684
685           h->needs_plt = 1;
686           if (h->plt.refcount == -1)
687             h->plt.refcount = 1;
688           else
689             h->plt.refcount++;
690           break;
691
692         case R_VAX_PC8:
693         case R_VAX_PC16:
694         case R_VAX_PC32:
695           /* If we are creating a shared library and this is not a local
696              symbol, we need to copy the reloc into the shared library.
697              However when linking with -Bsymbolic and this is a global
698              symbol which is defined in an object we are including in the
699              link (i.e., DEF_REGULAR is set), then we can resolve the
700              reloc directly.  At this point we have not seen all the input
701              files, so it is possible that DEF_REGULAR is not set now but
702              will be set later (it is never cleared).  We account for that
703              possibility below by storing information in the
704              pcrel_relocs_copied field of the hash table entry.  */
705           if (!(info->shared
706                 && (sec->flags & SEC_ALLOC) != 0
707                 && h != NULL
708                 && (!info->symbolic
709                     || !h->def_regular)))
710             {
711               if (h != NULL)
712                 {
713                   /* Make sure a plt entry is created for this symbol if
714                      it turns out to be a function defined by a dynamic
715                      object.  */
716                   if (h->plt.refcount == -1)
717                     h->plt.refcount = 1;
718                   else
719                     h->plt.refcount++;
720                 }
721               break;
722             }
723           /* Fall through.  */
724         case R_VAX_8:
725         case R_VAX_16:
726         case R_VAX_32:
727           if (h != NULL)
728             {
729               /* Make sure a plt entry is created for this symbol if it
730                  turns out to be a function defined by a dynamic object.  */
731               if (h->plt.refcount == -1)
732                 h->plt.refcount = 1;
733               else
734                 h->plt.refcount++;
735             }
736
737           /* If we are creating a shared library, we need to copy the
738              reloc into the shared library.  */
739           if (info->shared
740               && (sec->flags & SEC_ALLOC) != 0)
741             {
742               /* When creating a shared object, we must copy these
743                  reloc types into the output file.  We create a reloc
744                  section in dynobj and make room for this reloc.  */
745               if (sreloc == NULL)
746                 {
747                   const char *name;
748
749                   name = (bfd_elf_string_from_elf_section
750                           (abfd,
751                            elf_elfheader (abfd)->e_shstrndx,
752                            elf_section_data (sec)->rel_hdr.sh_name));
753                   if (name == NULL)
754                     return FALSE;
755
756                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
757                               && strcmp (bfd_get_section_name (abfd, sec),
758                                          name + 5) == 0);
759
760                   sreloc = bfd_get_section_by_name (dynobj, name);
761                   if (sreloc == NULL)
762                     {
763                       sreloc = bfd_make_section (dynobj, name);
764                       if (sreloc == NULL
765                           || !bfd_set_section_flags (dynobj, sreloc,
766                                                      (SEC_ALLOC
767                                                       | SEC_LOAD
768                                                       | SEC_HAS_CONTENTS
769                                                       | SEC_IN_MEMORY
770                                                       | SEC_LINKER_CREATED
771                                                       | SEC_READONLY))
772                           || !bfd_set_section_alignment (dynobj, sreloc, 2))
773                         return FALSE;
774                     }
775                   if (sec->flags & SEC_READONLY)
776                     info->flags |= DF_TEXTREL;
777                 }
778
779               sreloc->size += sizeof (Elf32_External_Rela);
780
781               /* If we are linking with -Bsymbolic, we count the number of
782                  PC relative relocations we have entered for this symbol,
783                  so that we can discard them again if the symbol is later
784                  defined by a regular object.  Note that this function is
785                  only called if we are using a vaxelf linker hash table,
786                  which means that h is really a pointer to an
787                  elf_vax_link_hash_entry.  */
788               if ((ELF32_R_TYPE (rel->r_info) == R_VAX_PC8
789                    || ELF32_R_TYPE (rel->r_info) == R_VAX_PC16
790                    || ELF32_R_TYPE (rel->r_info) == R_VAX_PC32)
791                   && info->symbolic)
792                 {
793                   struct elf_vax_link_hash_entry *eh;
794                   struct elf_vax_pcrel_relocs_copied *p;
795
796                   eh = (struct elf_vax_link_hash_entry *) h;
797
798                   for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
799                     if (p->section == sreloc)
800                       break;
801
802                   if (p == NULL)
803                     {
804                       p = ((struct elf_vax_pcrel_relocs_copied *)
805                            bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
806                       if (p == NULL)
807                         return FALSE;
808                       p->next = eh->pcrel_relocs_copied;
809                       eh->pcrel_relocs_copied = p;
810                       p->section = sreloc;
811                       p->count = 0;
812                     }
813
814                   ++p->count;
815                 }
816             }
817
818           break;
819
820           /* This relocation describes the C++ object vtable hierarchy.
821              Reconstruct it for later use during GC.  */
822         case R_VAX_GNU_VTINHERIT:
823           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
824             return FALSE;
825           break;
826
827           /* This relocation describes which C++ vtable entries are actually
828              used.  Record for later use during GC.  */
829         case R_VAX_GNU_VTENTRY:
830           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
831             return FALSE;
832           break;
833
834         default:
835           break;
836         }
837     }
838
839   return TRUE;
840 }
841
842 /* Return the section that should be marked against GC for a given
843    relocation.  */
844
845 static asection *
846 elf_vax_gc_mark_hook (sec, info, rel, h, sym)
847      asection *sec;
848      struct bfd_link_info *info ATTRIBUTE_UNUSED;
849      Elf_Internal_Rela *rel;
850      struct elf_link_hash_entry *h;
851      Elf_Internal_Sym *sym;
852 {
853   if (h != NULL)
854     {
855       switch (ELF32_R_TYPE (rel->r_info))
856         {
857         case R_VAX_GNU_VTINHERIT:
858         case R_VAX_GNU_VTENTRY:
859           break;
860
861         default:
862           switch (h->root.type)
863             {
864             default:
865               break;
866
867             case bfd_link_hash_defined:
868             case bfd_link_hash_defweak:
869               return h->root.u.def.section;
870
871             case bfd_link_hash_common:
872               return h->root.u.c.p->section;
873             }
874         }
875     }
876   else
877     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
878
879   return NULL;
880 }
881
882 /* Update the got entry reference counts for the section being removed.  */
883
884 static bfd_boolean
885 elf_vax_gc_sweep_hook (abfd, info, sec, relocs)
886      bfd *abfd;
887      struct bfd_link_info *info;
888      asection *sec;
889      const Elf_Internal_Rela *relocs;
890 {
891   Elf_Internal_Shdr *symtab_hdr;
892   struct elf_link_hash_entry **sym_hashes;
893   const Elf_Internal_Rela *rel, *relend;
894   bfd *dynobj;
895
896   dynobj = elf_hash_table (info)->dynobj;
897   if (dynobj == NULL)
898     return TRUE;
899
900   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
901   sym_hashes = elf_sym_hashes (abfd);
902
903   relend = relocs + sec->reloc_count;
904   for (rel = relocs; rel < relend; rel++)
905     {
906       unsigned long r_symndx;
907       struct elf_link_hash_entry *h;
908
909       switch (ELF32_R_TYPE (rel->r_info))
910         {
911         case R_VAX_GOT32:
912           r_symndx = ELF32_R_SYM (rel->r_info);
913           if (r_symndx >= symtab_hdr->sh_info)
914             {
915               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
916               if (h->got.refcount > 0)
917                 --h->got.refcount;
918             }
919           break;
920
921         case R_VAX_PLT32:
922         case R_VAX_PC8:
923         case R_VAX_PC16:
924         case R_VAX_PC32:
925         case R_VAX_8:
926         case R_VAX_16:
927         case R_VAX_32:
928           r_symndx = ELF32_R_SYM (rel->r_info);
929           if (r_symndx >= symtab_hdr->sh_info)
930             {
931               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
932               if (h->plt.refcount > 0)
933                 --h->plt.refcount;
934             }
935           break;
936
937         default:
938           break;
939         }
940     }
941
942   return TRUE;
943 }
944
945 /* Adjust a symbol defined by a dynamic object and referenced by a
946    regular object.  The current definition is in some section of the
947    dynamic object, but we're not including those sections.  We have to
948    change the definition to something the rest of the link can
949    understand.  */
950
951 static bfd_boolean
952 elf_vax_adjust_dynamic_symbol (info, h)
953      struct bfd_link_info *info;
954      struct elf_link_hash_entry *h;
955 {
956   bfd *dynobj;
957   asection *s;
958   unsigned int power_of_two;
959
960   dynobj = elf_hash_table (info)->dynobj;
961
962   /* Make sure we know what is going on here.  */
963   BFD_ASSERT (dynobj != NULL
964               && (h->needs_plt
965                   || h->u.weakdef != NULL
966                   || (h->def_dynamic
967                       && h->ref_regular
968                       && !h->def_regular)));
969
970   /* If this is a function, put it in the procedure linkage table.  We
971      will fill in the contents of the procedure linkage table later,
972      when we know the address of the .got section.  */
973   if (h->type == STT_FUNC
974       || h->needs_plt)
975     {
976       if (! info->shared
977           && !h->def_dynamic
978           && !h->ref_dynamic
979           /* We must always create the plt entry if it was referenced
980              by a PLTxxO relocation.  In this case we already recorded
981              it as a dynamic symbol.  */
982           && h->dynindx == -1)
983         {
984           /* This case can occur if we saw a PLTxx reloc in an input
985              file, but the symbol was never referred to by a dynamic
986              object.  In such a case, we don't actually need to build
987              a procedure linkage table, and we can just do a PCxx
988              reloc instead.  */
989           BFD_ASSERT (h->needs_plt);
990           h->plt.offset = (bfd_vma) -1;
991           return TRUE;
992         }
993
994       /* GC may have rendered this entry unused.  */
995       if (h->plt.refcount <= 0)
996         {
997           h->needs_plt = 0;
998           h->plt.offset = (bfd_vma) -1;
999           return TRUE;
1000         }
1001
1002       /* Make sure this symbol is output as a dynamic symbol.  */
1003       if (h->dynindx == -1)
1004         {
1005           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1006             return FALSE;
1007         }
1008
1009       s = bfd_get_section_by_name (dynobj, ".plt");
1010       BFD_ASSERT (s != NULL);
1011
1012       /* If this is the first .plt entry, make room for the special
1013          first entry.  */
1014       if (s->size == 0)
1015         {
1016           s->size += PLT_ENTRY_SIZE;
1017         }
1018
1019       /* If this symbol is not defined in a regular file, and we are
1020          not generating a shared library, then set the symbol to this
1021          location in the .plt.  This is required to make function
1022          pointers compare as equal between the normal executable and
1023          the shared library.  */
1024       if (!info->shared
1025           && !h->def_regular)
1026         {
1027           h->root.u.def.section = s;
1028           h->root.u.def.value = s->size;
1029         }
1030
1031       h->plt.offset = s->size;
1032
1033       /* Make room for this entry.  */
1034       s->size += PLT_ENTRY_SIZE;
1035
1036       /* We also need to make an entry in the .got.plt section, which
1037          will be placed in the .got section by the linker script.  */
1038
1039       s = bfd_get_section_by_name (dynobj, ".got.plt");
1040       BFD_ASSERT (s != NULL);
1041       s->size += 4;
1042
1043       /* We also need to make an entry in the .rela.plt section.  */
1044
1045       s = bfd_get_section_by_name (dynobj, ".rela.plt");
1046       BFD_ASSERT (s != NULL);
1047       s->size += sizeof (Elf32_External_Rela);
1048
1049       return TRUE;
1050     }
1051
1052   /* Reinitialize the plt offset now that it is not used as a reference
1053      count any more.  */
1054   h->plt.offset = (bfd_vma) -1;
1055
1056   /* If this is a weak symbol, and there is a real definition, the
1057      processor independent code will have arranged for us to see the
1058      real definition first, and we can just use the same value.  */
1059   if (h->u.weakdef != NULL)
1060     {
1061       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1062                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1063       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1064       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1065       return TRUE;
1066     }
1067
1068   /* This is a reference to a symbol defined by a dynamic object which
1069      is not a function.  */
1070
1071   /* If we are creating a shared library, we must presume that the
1072      only references to the symbol are via the global offset table.
1073      For such cases we need not do anything here; the relocations will
1074      be handled correctly by relocate_section.  */
1075   if (info->shared)
1076     return TRUE;
1077
1078   /* We must allocate the symbol in our .dynbss section, which will
1079      become part of the .bss section of the executable.  There will be
1080      an entry for this symbol in the .dynsym section.  The dynamic
1081      object will contain position independent code, so all references
1082      from the dynamic object to this symbol will go through the global
1083      offset table.  The dynamic linker will use the .dynsym entry to
1084      determine the address it must put in the global offset table, so
1085      both the dynamic object and the regular object will refer to the
1086      same memory location for the variable.  */
1087
1088   s = bfd_get_section_by_name (dynobj, ".dynbss");
1089   BFD_ASSERT (s != NULL);
1090
1091   /* We must generate a R_VAX_COPY reloc to tell the dynamic linker to
1092      copy the initial value out of the dynamic object and into the
1093      runtime process image.  We need to remember the offset into the
1094      .rela.bss section we are going to use.  */
1095   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1096     {
1097       asection *srel;
1098
1099       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1100       BFD_ASSERT (srel != NULL);
1101       srel->size += sizeof (Elf32_External_Rela);
1102       h->needs_copy = 1;
1103     }
1104
1105   /* We need to figure out the alignment required for this symbol.  I
1106      have no idea how ELF linkers handle this.  */
1107   power_of_two = bfd_log2 (h->size);
1108   if (power_of_two > 3)
1109     power_of_two = 3;
1110
1111   /* Apply the required alignment.  */
1112   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1113   if (power_of_two > bfd_get_section_alignment (dynobj, s))
1114     {
1115       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1116         return FALSE;
1117     }
1118
1119   /* Define the symbol as being at this point in the section.  */
1120   h->root.u.def.section = s;
1121   h->root.u.def.value = s->size;
1122
1123   /* Increment the section size to make room for the symbol.  */
1124   s->size += h->size;
1125
1126   return TRUE;
1127 }
1128
1129 /* Set the sizes of the dynamic sections.  */
1130
1131 static bfd_boolean
1132 elf_vax_size_dynamic_sections (output_bfd, info)
1133      bfd *output_bfd;
1134      struct bfd_link_info *info;
1135 {
1136   bfd *dynobj;
1137   asection *s;
1138   bfd_boolean plt;
1139   bfd_boolean relocs;
1140   bfd_boolean reltext;
1141
1142   dynobj = elf_hash_table (info)->dynobj;
1143   BFD_ASSERT (dynobj != NULL);
1144
1145   if (elf_hash_table (info)->dynamic_sections_created)
1146     {
1147       /* Set the contents of the .interp section to the interpreter.  */
1148       if (info->executable)
1149         {
1150           s = bfd_get_section_by_name (dynobj, ".interp");
1151           BFD_ASSERT (s != NULL);
1152           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1153           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1154         }
1155     }
1156   else
1157     {
1158       /* We may have created entries in the .rela.got and .got sections.
1159          However, if we are not creating the dynamic sections, we will
1160          not actually use these entries.  Reset the size of .rela.got
1161          and .got, which will cause it to get stripped from the output
1162          file below.  */
1163       s = bfd_get_section_by_name (dynobj, ".rela.got");
1164       if (s != NULL)
1165         s->size = 0;
1166       s = bfd_get_section_by_name (dynobj, ".got.plt");
1167       if (s != NULL)
1168         s->size = 0;
1169       s = bfd_get_section_by_name (dynobj, ".got");
1170       if (s != NULL)
1171         s->size = 0;
1172     }
1173
1174   /* If this is a -Bsymbolic shared link, then we need to discard all PC
1175      relative relocs against symbols defined in a regular object.  We
1176      allocated space for them in the check_relocs routine, but we will not
1177      fill them in in the relocate_section routine.  */
1178   if (info->shared && info->symbolic)
1179     elf_vax_link_hash_traverse (elf_vax_hash_table (info),
1180                                 elf_vax_discard_copies,
1181                                 (PTR) NULL);
1182
1183   /* If this is a -Bsymbolic shared link or a static link, we need to
1184      discard all the got entries we've recorded.  Otherwise, we need to
1185      instantiate (allocate space for them).  */
1186   elf_link_hash_traverse (elf_hash_table (info),
1187                           elf_vax_instantiate_got_entries,
1188                           (PTR) info);
1189
1190   /* The check_relocs and adjust_dynamic_symbol entry points have
1191      determined the sizes of the various dynamic sections.  Allocate
1192      memory for them.  */
1193   plt = FALSE;
1194   relocs = FALSE;
1195   reltext = FALSE;
1196   for (s = dynobj->sections; s != NULL; s = s->next)
1197     {
1198       const char *name;
1199       bfd_boolean strip;
1200
1201       if ((s->flags & SEC_LINKER_CREATED) == 0)
1202         continue;
1203
1204       /* It's OK to base decisions on the section name, because none
1205          of the dynobj section names depend upon the input files.  */
1206       name = bfd_get_section_name (dynobj, s);
1207
1208       strip = FALSE;
1209
1210       if (strcmp (name, ".plt") == 0)
1211         {
1212           if (s->size == 0)
1213             {
1214               /* Strip this section if we don't need it; see the
1215                  comment below.  */
1216               strip = TRUE;
1217             }
1218           else
1219             {
1220               /* Remember whether there is a PLT.  */
1221               plt = TRUE;
1222             }
1223         }
1224       else if (strncmp (name, ".rela", 5) == 0)
1225         {
1226           if (s->size == 0)
1227             {
1228               /* If we don't need this section, strip it from the
1229                  output file.  This is mostly to handle .rela.bss and
1230                  .rela.plt.  We must create both sections in
1231                  create_dynamic_sections, because they must be created
1232                  before the linker maps input sections to output
1233                  sections.  The linker does that before
1234                  adjust_dynamic_symbol is called, and it is that
1235                  function which decides whether anything needs to go
1236                  into these sections.  */
1237               strip = TRUE;
1238             }
1239           else
1240             {
1241               asection *target;
1242
1243               /* Remember whether there are any reloc sections other
1244                  than .rela.plt.  */
1245               if (strcmp (name, ".rela.plt") != 0)
1246                 {
1247                   const char *outname;
1248
1249                   relocs = TRUE;
1250
1251                   /* If this relocation section applies to a read only
1252                      section, then we probably need a DT_TEXTREL
1253                      entry.  .rela.plt is actually associated with
1254                      .got.plt, which is never readonly.  */
1255                   outname = bfd_get_section_name (output_bfd,
1256                                                   s->output_section);
1257                   target = bfd_get_section_by_name (output_bfd, outname + 5);
1258                   if (target != NULL
1259                       && (target->flags & SEC_READONLY) != 0
1260                       && (target->flags & SEC_ALLOC) != 0)
1261                     reltext = TRUE;
1262                 }
1263
1264               /* We use the reloc_count field as a counter if we need
1265                  to copy relocs into the output file.  */
1266               s->reloc_count = 0;
1267             }
1268         }
1269       else if (strncmp (name, ".got", 4) != 0)
1270         {
1271           /* It's not one of our sections, so don't allocate space.  */
1272           continue;
1273         }
1274
1275       if (strip)
1276         {
1277           _bfd_strip_section_from_output (info, s);
1278           continue;
1279         }
1280
1281       /* Allocate memory for the section contents.  */
1282       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->size);
1283       if (s->contents == NULL && s->size != 0)
1284         return FALSE;
1285     }
1286
1287   if (elf_hash_table (info)->dynamic_sections_created)
1288     {
1289       /* Add some entries to the .dynamic section.  We fill in the
1290          values later, in elf_vax_finish_dynamic_sections, but we
1291          must add the entries now so that we get the correct size for
1292          the .dynamic section.  The DT_DEBUG entry is filled in by the
1293          dynamic linker and used by the debugger.  */
1294 #define add_dynamic_entry(TAG, VAL) \
1295   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1296
1297       if (!info->shared)
1298         {
1299           if (!add_dynamic_entry (DT_DEBUG, 0))
1300             return FALSE;
1301         }
1302
1303       if (plt)
1304         {
1305           if (!add_dynamic_entry (DT_PLTGOT, 0)
1306               || !add_dynamic_entry (DT_PLTRELSZ, 0)
1307               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1308               || !add_dynamic_entry (DT_JMPREL, 0))
1309             return FALSE;
1310         }
1311
1312       if (relocs)
1313         {
1314           if (!add_dynamic_entry (DT_RELA, 0)
1315               || !add_dynamic_entry (DT_RELASZ, 0)
1316               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1317             return FALSE;
1318         }
1319
1320       if (reltext || (info->flags & DF_TEXTREL) != 0)
1321         {
1322           if (!add_dynamic_entry (DT_TEXTREL, 0))
1323             return FALSE;
1324         }
1325     }
1326 #undef add_dynamic_entry
1327
1328   return TRUE;
1329 }
1330
1331 /* This function is called via elf_vax_link_hash_traverse if we are
1332    creating a shared object with -Bsymbolic.  It discards the space
1333    allocated to copy PC relative relocs against symbols which are defined
1334    in regular objects.  We allocated space for them in the check_relocs
1335    routine, but we won't fill them in in the relocate_section routine.  */
1336
1337 static bfd_boolean
1338 elf_vax_discard_copies (h, ignore)
1339      struct elf_vax_link_hash_entry *h;
1340      PTR ignore ATTRIBUTE_UNUSED;
1341 {
1342   struct elf_vax_pcrel_relocs_copied *s;
1343
1344   if (h->root.root.type == bfd_link_hash_warning)
1345     h = (struct elf_vax_link_hash_entry *) h->root.root.u.i.link;
1346
1347   /* We only discard relocs for symbols defined in a regular object.  */
1348   if (!h->root.def_regular)
1349     return TRUE;
1350
1351   for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1352     s->section->size -= s->count * sizeof (Elf32_External_Rela);
1353
1354   return TRUE;
1355 }
1356
1357 /* This function is called via elf_link_hash_traverse.  It looks for entries
1358    that have GOT or PLT (.GOT) references.  If creating a static object or a
1359    shared object with -Bsymbolic, it resets the reference count back to 0
1360    and sets the offset to -1 so normal PC32 relocation will be done.  If
1361    creating a shared object or executable, space in the .got and .rela.got
1362    will be reserved for the symbol.  */
1363
1364 static bfd_boolean
1365 elf_vax_instantiate_got_entries (h, infoptr)
1366      struct elf_link_hash_entry *h;
1367      PTR infoptr;
1368 {
1369   struct bfd_link_info *info = (struct bfd_link_info *) infoptr;
1370   bfd *dynobj;
1371   asection *sgot;
1372   asection *srelgot;
1373
1374   /* We don't care about non-GOT (and non-PLT) entries.  */
1375   if (h->got.refcount <= 0 && h->plt.refcount <= 0)
1376     return TRUE;
1377
1378   dynobj = elf_hash_table (info)->dynobj;
1379   if (dynobj == NULL)
1380     return TRUE;
1381
1382   sgot = bfd_get_section_by_name (dynobj, ".got");
1383   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1384
1385   if (!elf_hash_table (info)->dynamic_sections_created
1386       || (info->shared && info->symbolic))
1387     {
1388       h->got.refcount = 0;
1389       h->got.offset = (bfd_vma) -1;
1390       h->plt.refcount = 0;
1391       h->plt.offset = (bfd_vma) -1;
1392     }
1393   else if (h->got.refcount > 0)
1394     {
1395       /* Make sure this symbol is output as a dynamic symbol.  */
1396       if (h->dynindx == -1)
1397         {
1398           if (!bfd_elf_link_record_dynamic_symbol (info, h))
1399             return FALSE;
1400         }
1401
1402       /* Allocate space in the .got and .rela.got sections.  */
1403       sgot->size += 4;
1404       srelgot->size += sizeof (Elf32_External_Rela);
1405     }
1406
1407   return TRUE;
1408 }
1409
1410 /* Relocate an VAX ELF section.  */
1411
1412 static bfd_boolean
1413 elf_vax_relocate_section (output_bfd, info, input_bfd, input_section,
1414                            contents, relocs, local_syms, local_sections)
1415      bfd *output_bfd;
1416      struct bfd_link_info *info;
1417      bfd *input_bfd;
1418      asection *input_section;
1419      bfd_byte *contents;
1420      Elf_Internal_Rela *relocs;
1421      Elf_Internal_Sym *local_syms;
1422      asection **local_sections;
1423 {
1424   bfd *dynobj;
1425   Elf_Internal_Shdr *symtab_hdr;
1426   struct elf_link_hash_entry **sym_hashes;
1427   bfd_vma *local_got_offsets;
1428   bfd_vma plt_index;
1429   bfd_vma got_offset;
1430   asection *sgot;
1431   asection *splt;
1432   asection *sgotplt;
1433   asection *sreloc;
1434   Elf_Internal_Rela *rel;
1435   Elf_Internal_Rela *relend;
1436
1437   if (info->relocatable)
1438     return TRUE;
1439
1440   dynobj = elf_hash_table (info)->dynobj;
1441   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1442   sym_hashes = elf_sym_hashes (input_bfd);
1443   local_got_offsets = elf_local_got_offsets (input_bfd);
1444
1445   sgot = NULL;
1446   splt = NULL;
1447   sgotplt = NULL;
1448   sreloc = NULL;
1449
1450   rel = relocs;
1451   relend = relocs + input_section->reloc_count;
1452   for (; rel < relend; rel++)
1453     {
1454       int r_type;
1455       reloc_howto_type *howto;
1456       unsigned long r_symndx;
1457       struct elf_link_hash_entry *h;
1458       Elf_Internal_Sym *sym;
1459       asection *sec;
1460       bfd_vma relocation;
1461       bfd_reloc_status_type r;
1462
1463       r_type = ELF32_R_TYPE (rel->r_info);
1464       if (r_type < 0 || r_type >= (int) R_VAX_max)
1465         {
1466           bfd_set_error (bfd_error_bad_value);
1467           return FALSE;
1468         }
1469       howto = howto_table + r_type;
1470
1471       /* This is a final link.  */
1472       r_symndx = ELF32_R_SYM (rel->r_info);
1473       h = NULL;
1474       sym = NULL;
1475       sec = NULL;
1476       if (r_symndx < symtab_hdr->sh_info)
1477         {
1478           sym = local_syms + r_symndx;
1479           sec = local_sections[r_symndx];
1480           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1481         }
1482       else
1483         {
1484           bfd_boolean unresolved_reloc;
1485           bfd_boolean warned;
1486
1487           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1488                                    r_symndx, symtab_hdr, sym_hashes,
1489                                    h, sec, relocation,
1490                                    unresolved_reloc, warned);
1491            
1492           if ((h->root.type == bfd_link_hash_defined
1493               || h->root.type == bfd_link_hash_defweak)
1494               && ((r_type == R_VAX_PLT32
1495                    && h->plt.offset != (bfd_vma) -1
1496                    && elf_hash_table (info)->dynamic_sections_created)
1497                   || (r_type == R_VAX_GOT32
1498                       && strcmp (h->root.root.string,
1499                                  "_GLOBAL_OFFSET_TABLE_") != 0
1500                       && elf_hash_table (info)->dynamic_sections_created
1501                       && (! info->shared
1502                           || (! info->symbolic && h->dynindx != -1)
1503                           || !h->def_regular))
1504                   || (info->shared
1505                       && ((! info->symbolic && h->dynindx != -1)
1506                           || !h->def_regular)
1507                       && ((input_section->flags & SEC_ALLOC) != 0
1508                           /* DWARF will emit R_VAX_32 relocations in its
1509                              sections against symbols defined externally
1510                              in shared libraries.  We can't do anything
1511                              with them here.  */
1512
1513                           || ((input_section->flags & SEC_DEBUGGING) != 0
1514                               && h->def_dynamic))
1515                       && (r_type == R_VAX_8
1516                           || r_type == R_VAX_16
1517                           || r_type == R_VAX_32
1518                           || r_type == R_VAX_PC8
1519                           || r_type == R_VAX_PC16
1520                           || r_type == R_VAX_PC32))))
1521             /* In these cases, we don't need the relocation
1522                value.  We check specially because in some
1523                obscure cases sec->output_section will be NULL.  */
1524             relocation = 0;
1525         }
1526
1527       switch (r_type)
1528         {
1529         case R_VAX_GOT32:
1530           /* Relocation is to the address of the entry for this symbol
1531              in the global offset table.  */
1532           if (h == NULL || h->got.offset == (bfd_vma) -1)
1533             break;
1534
1535           /* Relocation is the offset of the entry for this symbol in
1536              the global offset table.  */
1537
1538           {
1539             bfd_vma off;
1540
1541             if (sgot == NULL)
1542               {
1543                 sgot = bfd_get_section_by_name (dynobj, ".got");
1544                 BFD_ASSERT (sgot != NULL);
1545               }
1546
1547             BFD_ASSERT (h != NULL);
1548             off = h->got.offset;
1549             BFD_ASSERT (off != (bfd_vma) -1);
1550             BFD_ASSERT (off < sgot->size);
1551
1552             if (info->shared
1553                 && h->dynindx == -1
1554                 && h->def_regular)
1555               {
1556                 /* The symbol was forced to be local
1557                    because of a version file..  We must initialize
1558                    this entry in the global offset table.  Since
1559                    the offset must always be a multiple of 4, we
1560                    use the least significant bit to record whether
1561                    we have initialized it already.
1562
1563                    When doing a dynamic link, we create a .rela.got
1564                    relocation entry to initialize the value.  This
1565                    is done in the finish_dynamic_symbol routine.  */
1566                 if ((off & 1) != 0)
1567                   off &= ~1;
1568                 else
1569                   {
1570                     bfd_put_32 (output_bfd, relocation + rel->r_addend,
1571                                 sgot->contents + off);
1572                     h->got.offset |= 1;
1573                   }
1574               } else {
1575                 bfd_put_32 (output_bfd, rel->r_addend, sgot->contents + off);
1576               }
1577
1578             relocation = sgot->output_offset + off;
1579             /* The GOT relocation uses the addend.  */
1580             rel->r_addend = 0;
1581
1582             /* Change the reference to be indirect.  */
1583             contents[rel->r_offset - 1] |= 0x10;
1584             relocation += sgot->output_section->vma;
1585           }
1586           break;
1587
1588         case R_VAX_PLT32:
1589           /* Relocation is to the entry for this symbol in the
1590              procedure linkage table.  */
1591
1592           /* Resolve a PLTxx reloc against a local symbol directly,
1593              without using the procedure linkage table.  */
1594           if (h == NULL)
1595             break;
1596
1597           if (h->plt.offset == (bfd_vma) -1
1598               || !elf_hash_table (info)->dynamic_sections_created)
1599             {
1600               /* We didn't make a PLT entry for this symbol.  This
1601                  happens when statically linking PIC code, or when
1602                  using -Bsymbolic.  */
1603               break;
1604             }
1605
1606           if (splt == NULL)
1607             {
1608               splt = bfd_get_section_by_name (dynobj, ".plt");
1609               BFD_ASSERT (splt != NULL);
1610             }
1611
1612           if (sgotplt == NULL)
1613             {
1614               sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1615               BFD_ASSERT (splt != NULL);
1616             }
1617
1618           plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1619
1620           /* Get the offset into the .got table of the entry that
1621              corresponds to this function.  Each .got entry is 4 bytes.
1622              The first two are reserved.  */
1623           got_offset = (plt_index + 3) * 4;
1624
1625           /* We want the relocate to point into the .got.plt instead
1626              of the plt itself.  */
1627           relocation = (sgotplt->output_section->vma
1628                         + sgotplt->output_offset
1629                         + got_offset);
1630           contents[rel->r_offset-1] |= 0x10; /* make indirect */
1631           if (rel->r_addend == 2)
1632             {
1633               h->plt.offset |= 1;
1634             }
1635           else if (rel->r_addend != 0)
1636             (*_bfd_error_handler)
1637               (_("%s: warning: PLT addend of %d to `%s' from %s section ignored"),
1638                       bfd_get_filename (input_bfd), rel->r_addend,
1639                       h->root.root.string,
1640                       bfd_get_section_name (input_bfd, input_section));
1641           rel->r_addend = 0;
1642
1643           break;
1644
1645         case R_VAX_PC8:
1646         case R_VAX_PC16:
1647         case R_VAX_PC32:
1648           if (h == NULL)
1649             break;
1650           /* Fall through.  */
1651         case R_VAX_8:
1652         case R_VAX_16:
1653         case R_VAX_32:
1654           if (info->shared
1655               && r_symndx != 0
1656               && (input_section->flags & SEC_ALLOC) != 0
1657               && ((r_type != R_VAX_PC8
1658                    && r_type != R_VAX_PC16
1659                    && r_type != R_VAX_PC32)
1660                   || (!info->symbolic
1661                       || !h->def_regular)))
1662             {
1663               Elf_Internal_Rela outrel;
1664               bfd_byte *loc;
1665               bfd_boolean skip, relocate;
1666
1667               /* When generating a shared object, these relocations
1668                  are copied into the output file to be resolved at run
1669                  time.  */
1670               if (sreloc == NULL)
1671                 {
1672                   const char *name;
1673
1674                   name = (bfd_elf_string_from_elf_section
1675                           (input_bfd,
1676                            elf_elfheader (input_bfd)->e_shstrndx,
1677                            elf_section_data (input_section)->rel_hdr.sh_name));
1678                   if (name == NULL)
1679                     return FALSE;
1680
1681                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1682                               && strcmp (bfd_get_section_name (input_bfd,
1683                                                                input_section),
1684                                          name + 5) == 0);
1685
1686                   sreloc = bfd_get_section_by_name (dynobj, name);
1687                   BFD_ASSERT (sreloc != NULL);
1688                 }
1689
1690               skip = FALSE;
1691               relocate = FALSE;
1692
1693               outrel.r_offset =
1694                 _bfd_elf_section_offset (output_bfd, info, input_section,
1695                                          rel->r_offset);
1696               if (outrel.r_offset == (bfd_vma) -1)
1697                 skip = TRUE;
1698               if (outrel.r_offset == (bfd_vma) -2)
1699                 skip = TRUE, relocate = TRUE;
1700               outrel.r_offset += (input_section->output_section->vma
1701                                   + input_section->output_offset);
1702
1703               if (skip)
1704                   memset (&outrel, 0, sizeof outrel);
1705               /* h->dynindx may be -1 if the symbol was marked to
1706                  become local.  */
1707               else if (h != NULL
1708                        && ((! info->symbolic && h->dynindx != -1)
1709                            || !h->def_regular))
1710                 {
1711                   BFD_ASSERT (h->dynindx != -1);
1712                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1713                   outrel.r_addend = relocation + rel->r_addend;
1714                 }
1715               else
1716                 {
1717                   if (r_type == R_VAX_32)
1718                     {
1719                       relocate = TRUE;
1720                       outrel.r_info = ELF32_R_INFO (0, R_VAX_RELATIVE);
1721                       BFD_ASSERT (bfd_get_signed_32 (input_bfd,
1722                                                      &contents[rel->r_offset]) == 0);
1723                       outrel.r_addend = relocation + rel->r_addend;
1724                     }
1725                   else
1726                     {
1727                       long indx;
1728
1729                       if (bfd_is_abs_section (sec))
1730                         indx = 0;
1731                       else if (sec == NULL || sec->owner == NULL)
1732                         {
1733                           bfd_set_error (bfd_error_bad_value);
1734                           return FALSE;
1735                         }
1736                       else
1737                         {
1738                           asection *osec;
1739
1740                           osec = sec->output_section;
1741                           indx = elf_section_data (osec)->dynindx;
1742                           BFD_ASSERT (indx > 0);
1743                         }
1744
1745                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1746                       outrel.r_addend = relocation + rel->r_addend;
1747                     }
1748                 }
1749
1750               if (!strcmp (bfd_get_section_name (input_bfd, input_section),
1751                            ".text") != 0 ||
1752                   (info->shared
1753                    && ELF32_R_TYPE(outrel.r_info) != R_VAX_32
1754                    && ELF32_R_TYPE(outrel.r_info) != R_VAX_RELATIVE
1755                    && ELF32_R_TYPE(outrel.r_info) != R_VAX_COPY
1756                    && ELF32_R_TYPE(outrel.r_info) != R_VAX_JMP_SLOT
1757                    && ELF32_R_TYPE(outrel.r_info) != R_VAX_GLOB_DAT))
1758                 {
1759                   if (h != NULL)
1760                     (*_bfd_error_handler)
1761                       (_("%s: warning: %s relocation against symbol `%s' from %s section"),
1762                       bfd_get_filename (input_bfd), howto->name,
1763                       h->root.root.string,
1764                       bfd_get_section_name (input_bfd, input_section));
1765                   else
1766                     (*_bfd_error_handler)
1767                       (_("%s: warning: %s relocation to 0x%x from %s section"),
1768                       bfd_get_filename (input_bfd), howto->name,
1769                       outrel.r_addend,
1770                       bfd_get_section_name (input_bfd, input_section));
1771                 }
1772               loc = sreloc->contents;
1773               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1774               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1775
1776               /* This reloc will be computed at runtime, so there's no
1777                  need to do anything now, except for R_VAX_32
1778                  relocations that have been turned into
1779                  R_VAX_RELATIVE.  */
1780               if (!relocate)
1781                 continue;
1782             }
1783
1784           break;
1785
1786         case R_VAX_GNU_VTINHERIT:
1787         case R_VAX_GNU_VTENTRY:
1788           /* These are no-ops in the end.  */
1789           continue;
1790
1791         default:
1792           break;
1793         }
1794
1795       /* VAX PCREL relocations are from the end of relocation, not the start.
1796          So subtract the difference from the relocation amount since we can't
1797          add it to the offset.  */
1798       if (howto->pc_relative && howto->pcrel_offset)
1799         relocation -= bfd_get_reloc_size(howto);
1800
1801       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1802                                     contents, rel->r_offset,
1803                                     relocation, rel->r_addend);
1804
1805       if (r != bfd_reloc_ok)
1806         {
1807           switch (r)
1808             {
1809             default:
1810             case bfd_reloc_outofrange:
1811               abort ();
1812             case bfd_reloc_overflow:
1813               {
1814                 const char *name;
1815
1816                 if (h != NULL)
1817                   name = NULL;
1818                 else
1819                   {
1820                     name = bfd_elf_string_from_elf_section (input_bfd,
1821                                                             symtab_hdr->sh_link,
1822                                                             sym->st_name);
1823                     if (name == NULL)
1824                       return FALSE;
1825                     if (*name == '\0')
1826                       name = bfd_section_name (input_bfd, sec);
1827                   }
1828                 if (!(info->callbacks->reloc_overflow
1829                       (info, (h ? &h->root : NULL), name, howto->name,
1830                        (bfd_vma) 0, input_bfd, input_section,
1831                        rel->r_offset)))
1832                   return FALSE;
1833               }
1834               break;
1835             }
1836         }
1837     }
1838
1839   return TRUE;
1840 }
1841
1842 /* Finish up dynamic symbol handling.  We set the contents of various
1843    dynamic sections here.  */
1844
1845 static bfd_boolean
1846 elf_vax_finish_dynamic_symbol (output_bfd, info, h, sym)
1847      bfd *output_bfd;
1848      struct bfd_link_info *info;
1849      struct elf_link_hash_entry *h;
1850      Elf_Internal_Sym *sym;
1851 {
1852   bfd *dynobj;
1853
1854   dynobj = elf_hash_table (info)->dynobj;
1855
1856   if (h->plt.offset != (bfd_vma) -1)
1857     {
1858       asection *splt;
1859       asection *sgot;
1860       asection *srela;
1861       bfd_vma plt_index;
1862       bfd_vma got_offset;
1863       bfd_vma addend;
1864       Elf_Internal_Rela rela;
1865       bfd_byte *loc;
1866
1867       /* This symbol has an entry in the procedure linkage table.  Set
1868          it up.  */
1869       BFD_ASSERT (h->dynindx != -1);
1870
1871       splt = bfd_get_section_by_name (dynobj, ".plt");
1872       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1873       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1874       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1875
1876       addend = 2 * (h->plt.offset & 1);
1877       h->plt.offset &= ~1;
1878
1879       /* Get the index in the procedure linkage table which
1880          corresponds to this symbol.  This is the index of this symbol
1881          in all the symbols for which we are making plt entries.  The
1882          first entry in the procedure linkage table is reserved.  */
1883       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1884
1885       /* Get the offset into the .got table of the entry that
1886          corresponds to this function.  Each .got entry is 4 bytes.
1887          The first two are reserved.  */
1888       got_offset = (plt_index + 3) * 4;
1889
1890       /* Fill in the entry in the procedure linkage table.  */
1891       memcpy (splt->contents + h->plt.offset, elf_vax_plt_entry,
1892                   PLT_ENTRY_SIZE);
1893
1894       /* The offset is relative to the first extension word.  */
1895       bfd_put_32 (output_bfd,
1896                   -(h->plt.offset + 8),
1897                   splt->contents + h->plt.offset + 4);
1898
1899       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1900                   splt->contents + h->plt.offset + 8);
1901
1902       /* Fill in the entry in the global offset table.  */
1903       bfd_put_32 (output_bfd,
1904                   (splt->output_section->vma
1905                    + splt->output_offset
1906                    + h->plt.offset) + addend,
1907                   sgot->contents + got_offset);
1908
1909       /* Fill in the entry in the .rela.plt section.  */
1910       rela.r_offset = (sgot->output_section->vma
1911                        + sgot->output_offset
1912                        + got_offset);
1913       rela.r_info = ELF32_R_INFO (h->dynindx, R_VAX_JMP_SLOT);
1914       rela.r_addend = addend;
1915       loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
1916       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1917
1918       if (!h->def_regular)
1919         {
1920           /* Mark the symbol as undefined, rather than as defined in
1921              the .plt section.  Leave the value alone.  */
1922           sym->st_shndx = SHN_UNDEF;
1923         }
1924     }
1925
1926   if (h->got.offset != (bfd_vma) -1)
1927     {
1928       asection *sgot;
1929       asection *srela;
1930       Elf_Internal_Rela rela;
1931       bfd_byte *loc;
1932
1933       /* This symbol has an entry in the global offset table.  Set it
1934          up.  */
1935       sgot = bfd_get_section_by_name (dynobj, ".got");
1936       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1937       BFD_ASSERT (sgot != NULL && srela != NULL);
1938
1939       rela.r_offset = (sgot->output_section->vma
1940                        + sgot->output_offset
1941                        + (h->got.offset &~ 1));
1942
1943       /* If the symbol was forced to be local because of a version file
1944          locally we just want to emit a RELATIVE reloc.  The entry in
1945          the global offset table will already have been initialized in
1946          the relocate_section function.  */
1947       if (info->shared
1948           && h->dynindx == -1
1949           && h->def_regular)
1950         {
1951           rela.r_info = ELF32_R_INFO (0, R_VAX_RELATIVE);
1952         }
1953       else
1954         {
1955           rela.r_info = ELF32_R_INFO (h->dynindx, R_VAX_GLOB_DAT);
1956         }
1957       rela.r_addend = bfd_get_signed_32 (output_bfd,
1958                                          (sgot->contents
1959                                           + (h->got.offset & ~1)));
1960
1961       loc = srela->contents;
1962       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
1963       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1964     }
1965
1966   if (h->needs_copy)
1967     {
1968       asection *s;
1969       Elf_Internal_Rela rela;
1970       bfd_byte *loc;
1971
1972       /* This symbol needs a copy reloc.  Set it up.  */
1973       BFD_ASSERT (h->dynindx != -1
1974                   && (h->root.type == bfd_link_hash_defined
1975                       || h->root.type == bfd_link_hash_defweak));
1976
1977       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1978                                    ".rela.bss");
1979       BFD_ASSERT (s != NULL);
1980
1981       rela.r_offset = (h->root.u.def.value
1982                        + h->root.u.def.section->output_section->vma
1983                        + h->root.u.def.section->output_offset);
1984       rela.r_info = ELF32_R_INFO (h->dynindx, R_VAX_COPY);
1985       rela.r_addend = 0;
1986       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
1987       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1988     }
1989
1990   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1991   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1992       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1993     sym->st_shndx = SHN_ABS;
1994
1995   return TRUE;
1996 }
1997
1998 /* Finish up the dynamic sections.  */
1999
2000 static bfd_boolean
2001 elf_vax_finish_dynamic_sections (output_bfd, info)
2002      bfd *output_bfd;
2003      struct bfd_link_info *info;
2004 {
2005   bfd *dynobj;
2006   asection *sgot;
2007   asection *sdyn;
2008
2009   dynobj = elf_hash_table (info)->dynobj;
2010
2011   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2012   BFD_ASSERT (sgot != NULL);
2013   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2014
2015   if (elf_hash_table (info)->dynamic_sections_created)
2016     {
2017       asection *splt;
2018       Elf32_External_Dyn *dyncon, *dynconend;
2019
2020       splt = bfd_get_section_by_name (dynobj, ".plt");
2021       BFD_ASSERT (splt != NULL && sdyn != NULL);
2022
2023       dyncon = (Elf32_External_Dyn *) sdyn->contents;
2024       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2025       for (; dyncon < dynconend; dyncon++)
2026         {
2027           Elf_Internal_Dyn dyn;
2028           const char *name;
2029           asection *s;
2030
2031           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2032
2033           switch (dyn.d_tag)
2034             {
2035             default:
2036               break;
2037
2038             case DT_PLTGOT:
2039               name = ".got";
2040               goto get_vma;
2041             case DT_JMPREL:
2042               name = ".rela.plt";
2043             get_vma:
2044               s = bfd_get_section_by_name (output_bfd, name);
2045               BFD_ASSERT (s != NULL);
2046               dyn.d_un.d_ptr = s->vma;
2047               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2048               break;
2049
2050             case DT_PLTRELSZ:
2051               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2052               BFD_ASSERT (s != NULL);
2053               dyn.d_un.d_val = s->size;
2054               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2055               break;
2056
2057             case DT_RELASZ:
2058               /* The procedure linkage table relocs (DT_JMPREL) should
2059                  not be included in the overall relocs (DT_RELA).
2060                  Therefore, we override the DT_RELASZ entry here to
2061                  make it not include the JMPREL relocs.  Since the
2062                  linker script arranges for .rela.plt to follow all
2063                  other relocation sections, we don't have to worry
2064                  about changing the DT_RELA entry.  */
2065               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2066               if (s != NULL)
2067                 dyn.d_un.d_val -= s->size;
2068               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2069               break;
2070             }
2071         }
2072
2073       /* Fill in the first entry in the procedure linkage table.  */
2074       if (splt->size > 0)
2075         {
2076           memcpy (splt->contents, elf_vax_plt0_entry, PLT_ENTRY_SIZE);
2077           bfd_put_32 (output_bfd,
2078                           (sgot->output_section->vma
2079                            + sgot->output_offset + 4
2080                            - (splt->output_section->vma + 6)),
2081                           splt->contents + 2);
2082           bfd_put_32 (output_bfd,
2083                           (sgot->output_section->vma
2084                            + sgot->output_offset + 8
2085                            - (splt->output_section->vma + 12)),
2086                           splt->contents + 8);
2087           elf_section_data (splt->output_section)->this_hdr.sh_entsize
2088            = PLT_ENTRY_SIZE;
2089         }
2090     }
2091
2092   /* Fill in the first three entries in the global offset table.  */
2093   if (sgot->size > 0)
2094     {
2095       if (sdyn == NULL)
2096         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2097       else
2098         bfd_put_32 (output_bfd,
2099                     sdyn->output_section->vma + sdyn->output_offset,
2100                     sgot->contents);
2101       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2102       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2103     }
2104
2105   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2106
2107   return TRUE;
2108 }
2109
2110 #define TARGET_LITTLE_SYM               bfd_elf32_vax_vec
2111 #define TARGET_LITTLE_NAME              "elf32-vax"
2112 #define ELF_MACHINE_CODE                EM_VAX
2113 #define ELF_MAXPAGESIZE                 0x1000
2114
2115 #define elf_backend_create_dynamic_sections \
2116                                         _bfd_elf_create_dynamic_sections
2117 #define bfd_elf32_bfd_link_hash_table_create \
2118                                         elf_vax_link_hash_table_create
2119 #define bfd_elf32_bfd_final_link        bfd_elf_gc_common_final_link
2120
2121 #define elf_backend_check_relocs        elf_vax_check_relocs
2122 #define elf_backend_adjust_dynamic_symbol \
2123                                         elf_vax_adjust_dynamic_symbol
2124 #define elf_backend_size_dynamic_sections \
2125                                         elf_vax_size_dynamic_sections
2126 #define elf_backend_relocate_section    elf_vax_relocate_section
2127 #define elf_backend_finish_dynamic_symbol \
2128                                         elf_vax_finish_dynamic_symbol
2129 #define elf_backend_finish_dynamic_sections \
2130                                         elf_vax_finish_dynamic_sections
2131 #define elf_backend_gc_mark_hook        elf_vax_gc_mark_hook
2132 #define elf_backend_gc_sweep_hook       elf_vax_gc_sweep_hook
2133 #define bfd_elf32_bfd_merge_private_bfd_data \
2134                                         elf32_vax_merge_private_bfd_data
2135 #define bfd_elf32_bfd_set_private_flags \
2136                                         elf32_vax_set_private_flags
2137 #define bfd_elf32_bfd_print_private_bfd_data \
2138                                         elf32_vax_print_private_bfd_data
2139
2140 #define elf_backend_can_gc_sections     1
2141 #define elf_backend_want_got_plt        1
2142 #define elf_backend_plt_readonly        1
2143 #define elf_backend_want_plt_sym        0
2144 #define elf_backend_got_header_size     16
2145 #define elf_backend_rela_normal         1
2146
2147 #include "elf32-target.h"