OSDN Git Service

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