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