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