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