xref: /netbsd-src/external/gpl3/binutils/dist/bfd/elf32-vax.c (revision ead2c0eee3abe6bcf08c63bfc78eb8a93a579b2b)
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       || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1362       || h->forced_local)
1363     {
1364       h->got.refcount = 0;
1365       h->got.offset = (bfd_vma) -1;
1366       h->plt.refcount = 0;
1367       h->plt.offset = (bfd_vma) -1;
1368     }
1369   else if (h->got.refcount > 0)
1370     {
1371       bfd_boolean dyn;
1372 
1373       /* Make sure this symbol is output as a dynamic symbol.  */
1374       if (h->dynindx == -1)
1375 	{
1376 	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
1377 	    return FALSE;
1378 	}
1379 
1380       dyn = elf_hash_table (info)->dynamic_sections_created;
1381       /* Allocate space in the .got and .rela.got sections.  */
1382       if (info->shared || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
1383 	{
1384 	  sgot->size += 4;
1385 	  srelgot->size += sizeof (Elf32_External_Rela);
1386 	}
1387     }
1388 
1389   return TRUE;
1390 }
1391 
1392 /* Relocate an VAX ELF section.  */
1393 
1394 static bfd_boolean
1395 elf_vax_relocate_section (bfd *output_bfd,
1396 			  struct bfd_link_info *info,
1397 			  bfd *input_bfd,
1398 			  asection *input_section,
1399 			  bfd_byte *contents,
1400 			  Elf_Internal_Rela *relocs,
1401 			  Elf_Internal_Sym *local_syms,
1402 			  asection **local_sections)
1403 {
1404   bfd *dynobj;
1405   Elf_Internal_Shdr *symtab_hdr;
1406   struct elf_link_hash_entry **sym_hashes;
1407   bfd_vma plt_index;
1408   bfd_vma got_offset;
1409   asection *sgot;
1410   asection *splt;
1411   asection *sgotplt;
1412   asection *sreloc;
1413   Elf_Internal_Rela *rel;
1414   Elf_Internal_Rela *relend;
1415 
1416   dynobj = elf_hash_table (info)->dynobj;
1417   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1418   sym_hashes = elf_sym_hashes (input_bfd);
1419 
1420   sgot = NULL;
1421   splt = NULL;
1422   sgotplt = NULL;
1423   sreloc = NULL;
1424 
1425   rel = relocs;
1426   relend = relocs + input_section->reloc_count;
1427   for (; rel < relend; rel++)
1428     {
1429       int r_type;
1430       reloc_howto_type *howto;
1431       unsigned long r_symndx;
1432       struct elf_link_hash_entry *h;
1433       Elf_Internal_Sym *sym;
1434       asection *sec;
1435       bfd_vma relocation;
1436       bfd_reloc_status_type r;
1437 
1438       r_type = ELF32_R_TYPE (rel->r_info);
1439       if (r_type < 0 || r_type >= (int) R_VAX_max)
1440 	{
1441 	  bfd_set_error (bfd_error_bad_value);
1442 	  return FALSE;
1443 	}
1444       howto = howto_table + r_type;
1445 
1446       r_symndx = ELF32_R_SYM (rel->r_info);
1447       h = NULL;
1448       sym = NULL;
1449       sec = NULL;
1450       if (r_symndx < symtab_hdr->sh_info)
1451 	{
1452 	  sym = local_syms + r_symndx;
1453 	  sec = local_sections[r_symndx];
1454 #if 0 /* XXXMRG */
1455 	  relocation = (sec->output_section->vma
1456 			+ sec->output_offset
1457 			+ sym->st_value);
1458 #else
1459 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1460 #endif
1461 	}
1462       else
1463 	{
1464 	  bfd_boolean unresolved_reloc;
1465 	  bfd_boolean warned;
1466 
1467 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1468 				   r_symndx, symtab_hdr, sym_hashes,
1469 				   h, sec, relocation,
1470 				   unresolved_reloc, warned);
1471 
1472 	  if ((h->root.type == bfd_link_hash_defined
1473 	      || h->root.type == bfd_link_hash_defweak)
1474 	      && ((r_type == R_VAX_PLT32
1475 		   && h->plt.offset != (bfd_vma) -1
1476 		   && !h->forced_local
1477 		   && elf_hash_table (info)->dynamic_sections_created)
1478 		  || (r_type == R_VAX_GOT32
1479 		      && h->got.offset != (bfd_vma) -1
1480 		      && !h->forced_local
1481 		      && elf_hash_table (info)->dynamic_sections_created
1482 		      && (! info->shared
1483 			  || (! info->symbolic && h->dynindx != -1)
1484 			  || !h->def_regular))
1485 		  || (info->shared
1486 		      && ((! info->symbolic && h->dynindx != -1)
1487 			  || !h->def_regular)
1488 		      && ((input_section->flags & SEC_ALLOC) != 0
1489 			  /* DWARF will emit R_VAX_32 relocations in its
1490 			     sections against symbols defined externally
1491 			     in shared libraries.  We can't do anything
1492 			     with them here.  */
1493 
1494 			  || ((input_section->flags & SEC_DEBUGGING) != 0
1495 			      && h->def_dynamic))
1496 		      && (r_type == R_VAX_8
1497 			  || r_type == R_VAX_16
1498 			  || r_type == R_VAX_32))))
1499 	    /* In these cases, we don't need the relocation
1500 	       value.  We check specially because in some
1501 	       obscure cases sec->output_section will be NULL.  */
1502 	    relocation = 0;
1503 	}
1504 
1505       if (sec != NULL && elf_discarded_section (sec))
1506 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1507 					 rel, relend, howto, contents);
1508 
1509       if (info->relocatable)
1510 	continue;
1511 
1512       switch (r_type)
1513 	{
1514 	case R_VAX_GOT32:
1515 	  /* Relocation is to the address of the entry for this symbol
1516 	     in the global offset table.  */
1517 	  if (h == NULL
1518 	      || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1519 	      || h->got.offset == (bfd_vma) -1
1520 	      || h->forced_local)
1521 	    break;
1522 
1523 	  /* Relocation is the offset of the entry for this symbol in
1524 	     the global offset table.  */
1525 
1526 	  {
1527 	    bfd_boolean dyn;
1528 	    bfd_vma off;
1529 
1530 	    if (sgot == NULL)
1531 	      {
1532 		sgot = bfd_get_section_by_name (dynobj, ".got");
1533 		BFD_ASSERT (sgot != NULL);
1534 	      }
1535 
1536 	    BFD_ASSERT (h != NULL);
1537 	    off = h->got.offset;
1538 	    BFD_ASSERT (off != (bfd_vma) -1);
1539 	    BFD_ASSERT (off < sgot->size);
1540 
1541 	    dyn = elf_hash_table (info)->dynamic_sections_created;
1542 	    if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1543 		|| (info->shared
1544 		    && SYMBOL_REFERENCES_LOCAL (info, h)))
1545 	      {
1546 		/* The symbol was forced to be local
1547 		   because of a version file..  We must initialize
1548 		   this entry in the global offset table.  Since
1549 		   the offset must always be a multiple of 4, we
1550 		   use the least significant bit to record whether
1551 		   we have initialized it already.
1552 
1553 		   When doing a dynamic link, we create a .rela.got
1554 		   relocation entry to initialize the value.  This
1555 		   is done in the finish_dynamic_symbol routine.  */
1556 		if ((off & 1) != 0)
1557 		  off &= ~1;
1558 		else
1559 		  {
1560 		    bfd_put_32 (output_bfd, relocation + rel->r_addend,
1561 				sgot->contents + off);
1562 		    h->got.offset |= 1;
1563 		  }
1564 	      } else {
1565 		bfd_put_32 (output_bfd, rel->r_addend, sgot->contents + off);
1566 	      }
1567 
1568 	    relocation = sgot->output_offset + off;
1569 	    /* The GOT relocation uses the addend.  */
1570 	    rel->r_addend = 0;
1571 
1572 	    /* Change the reference to be indirect.  */
1573 	    contents[rel->r_offset - 1] |= 0x10;
1574 	    relocation += sgot->output_section->vma;
1575 	  }
1576 	  break;
1577 
1578 	case R_VAX_PC32:
1579 	  /* If we are creating an executable and the function this
1580 	     reloc refers to is in a shared lib, then we made a PLT
1581 	     entry for this symbol and need to handle the reloc like
1582 	     a PLT reloc.  */
1583 	  if (info->shared)
1584 	     goto r_vax_pc32_shared;
1585 	  /* Fall through.  */
1586 	case R_VAX_PLT32:
1587 	  /* Relocation is to the entry for this symbol in the
1588 	     procedure linkage table.  */
1589 
1590 	  /* Resolve a PLTxx reloc against a local symbol directly,
1591 	     without using the procedure linkage table.  */
1592 	  if (h == NULL
1593 	      || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1594 	      || h->forced_local)
1595 	    break;
1596 
1597 	  if (h->plt.offset == (bfd_vma) -1
1598 	      || h->forced_local
1599 	      || !elf_hash_table (info)->dynamic_sections_created)
1600 	    {
1601 	      /* We didn't make a PLT entry for this symbol.  This
1602 		 happens when statically linking PIC code, or when
1603 		 using -Bsymbolic.  */
1604 	      break;
1605 	    }
1606 
1607 	  if (splt == NULL)
1608 	    {
1609 	      splt = bfd_get_section_by_name (dynobj, ".plt");
1610 	      BFD_ASSERT (splt != NULL);
1611 	    }
1612 
1613 	  if (sgotplt == NULL)
1614 	    {
1615 	      sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1616 	      BFD_ASSERT (sgotplt != NULL);
1617 	    }
1618 
1619 	  plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1620 
1621 	  /* Get the offset into the .got table of the entry that
1622 	     corresponds to this function.  Each .got entry is 4 bytes.
1623 	     The first two are reserved.  */
1624 	  got_offset = (plt_index + 3) * 4;
1625 
1626 	  /* We want the relocation to point into the .got.plt instead
1627 	     of the plt itself.  */
1628 	  relocation = (sgotplt->output_section->vma
1629 			+ sgotplt->output_offset
1630 			+ got_offset);
1631 	  contents[rel->r_offset-1] |= 0x10; /* make indirect */
1632 	  if (rel->r_addend == 2)
1633 	    {
1634 	      h->plt.offset |= 1;
1635 	    }
1636 	  else if (rel->r_addend != 0)
1637 	    (*_bfd_error_handler)
1638 	      (_("%s: warning: PLT addend of %d to `%s' from %s section ignored"),
1639 		      bfd_get_filename (input_bfd), rel->r_addend,
1640 		      h->root.root.string,
1641 		      bfd_get_section_name (input_bfd, input_section));
1642 	  rel->r_addend = 0;
1643 
1644 	  break;
1645 
1646 	case R_VAX_PC8:
1647 	case R_VAX_PC16:
1648 	r_vax_pc32_shared:
1649 	  if (h == NULL
1650 	      || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1651 	      || h->forced_local)
1652 	    break;
1653 	  /* Fall through.  */
1654 	case R_VAX_8:
1655 	case R_VAX_16:
1656 	case R_VAX_32:
1657 	  if (info->shared
1658 	      && r_symndx != STN_UNDEF
1659 	      && (input_section->flags & SEC_ALLOC) != 0
1660 	      && ((r_type != R_VAX_PC8
1661 		   && r_type != R_VAX_PC16
1662 		   && r_type != R_VAX_PC32)
1663 	          || ((input_section->flags & SEC_CODE) != 0
1664 		      && (!info->symbolic
1665 			  || (!h->def_regular && h->type != STT_SECTION)))))
1666 	    {
1667 	      Elf_Internal_Rela outrel;
1668 	      bfd_byte *loc;
1669 	      bfd_boolean skip, relocate;
1670 
1671 	      /* When generating a shared object, these relocations
1672 		 are copied into the output file to be resolved at run
1673 		 time.  */
1674 	      if (sreloc == NULL)
1675 		{
1676 		  sreloc = _bfd_elf_get_dynamic_reloc_section
1677 		    (input_bfd, input_section, /*rela?*/ TRUE);
1678 		  if (sreloc == NULL)
1679 		    return FALSE;
1680 		}
1681 
1682 	      skip = FALSE;
1683 	      relocate = FALSE;
1684 
1685 	      outrel.r_offset =
1686 		_bfd_elf_section_offset (output_bfd, info, input_section,
1687 					 rel->r_offset);
1688 	      if (outrel.r_offset == (bfd_vma) -1)
1689 		skip = TRUE;
1690 	      if (outrel.r_offset == (bfd_vma) -2)
1691 		skip = TRUE, relocate = TRUE;
1692 	      outrel.r_offset += (input_section->output_section->vma
1693 				  + input_section->output_offset);
1694 
1695 	      if (skip)
1696 		  memset (&outrel, 0, sizeof outrel);
1697 	      /* h->dynindx may be -1 if the symbol was marked to
1698                  become local.  */
1699 	      else if (h != NULL
1700 		       && ((! info->symbolic && h->dynindx != -1)
1701 			   || !h->def_regular))
1702 		{
1703 		  BFD_ASSERT (h->dynindx != -1);
1704 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1705 		  outrel.r_addend = relocation + rel->r_addend;
1706 		}
1707 	      else
1708 		{
1709 		  if (r_type == R_VAX_32)
1710 		    {
1711 		      relocate = TRUE;
1712 		      outrel.r_info = ELF32_R_INFO (0, R_VAX_RELATIVE);
1713 		      outrel.r_addend = bfd_get_signed_32(input_bfd,
1714 							 &contents[rel->r_offset])
1715 					+ relocation + rel->r_addend;
1716 		    }
1717 		  else
1718 		    {
1719 		      long indx;
1720 
1721 		      if (bfd_is_abs_section (sec))
1722 			indx = 0;
1723 		      else if (sec == NULL || sec->owner == NULL)
1724 			{
1725 			  bfd_set_error (bfd_error_bad_value);
1726 			  return FALSE;
1727 			}
1728 		      else
1729 			{
1730 			  asection *osec;
1731 
1732 			  /* We are turning this relocation into one
1733 			     against a section symbol.  It would be
1734 			     proper to subtract the symbol's value,
1735 			     osec->vma, from the emitted reloc addend,
1736 			     but ld.so expects buggy relocs.  */
1737 			  osec = sec->output_section;
1738 			  indx = elf_section_data (osec)->dynindx;
1739 			  if (indx == 0)
1740 			    {
1741 			      struct elf_link_hash_table *htab;
1742 			      htab = elf_hash_table (info);
1743 			      osec = htab->text_index_section;
1744 			      indx = elf_section_data (osec)->dynindx;
1745 			    }
1746 			  BFD_ASSERT (indx != 0);
1747 			}
1748 
1749 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
1750 		      outrel.r_addend = relocation + rel->r_addend;
1751 		    }
1752 		}
1753 
1754 	      if (strcmp (bfd_get_section_name (input_bfd, input_section),
1755 			   ".text") == 0 ||
1756 		  (info->shared
1757 		   && ELF32_R_TYPE(outrel.r_info) != R_VAX_32
1758 		   && ELF32_R_TYPE(outrel.r_info) != R_VAX_RELATIVE
1759 		   && ELF32_R_TYPE(outrel.r_info) != R_VAX_COPY
1760 		   && ELF32_R_TYPE(outrel.r_info) != R_VAX_JMP_SLOT
1761 		   && ELF32_R_TYPE(outrel.r_info) != R_VAX_GLOB_DAT))
1762 		{
1763 		  if (h != NULL)
1764 		    (*_bfd_error_handler)
1765 		      (_("%s: warning: %s relocation against symbol `%s' from %s section"),
1766 		      bfd_get_filename (input_bfd), howto->name,
1767 		      h->root.root.string,
1768 		      bfd_get_section_name (input_bfd, input_section));
1769 		  else
1770 		    (*_bfd_error_handler)
1771 		      (_("%s: warning: %s relocation to 0x%x from %s section"),
1772 		      bfd_get_filename (input_bfd), howto->name,
1773 		      outrel.r_addend,
1774 		      bfd_get_section_name (input_bfd, input_section));
1775 		}
1776 	      loc = sreloc->contents;
1777 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1778 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1779 
1780 	      /* This reloc will be computed at runtime, so there's no
1781                  need to do anything now, except for R_VAX_32
1782                  relocations that have been turned into
1783                  R_VAX_RELATIVE.  */
1784 	      if (!relocate)
1785 		continue;
1786 	    }
1787 
1788 	  break;
1789 
1790 	case R_VAX_GNU_VTINHERIT:
1791 	case R_VAX_GNU_VTENTRY:
1792 	  /* These are no-ops in the end.  */
1793 	  continue;
1794 
1795 	default:
1796 	  break;
1797 	}
1798 
1799       /* VAX PCREL relocations are from the end of relocation, not the start.
1800          So subtract the difference from the relocation amount since we can't
1801          add it to the offset.  */
1802       if (howto->pc_relative && howto->pcrel_offset)
1803 	relocation -= bfd_get_reloc_size(howto);
1804 
1805       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1806 				    contents, rel->r_offset,
1807 				    relocation, rel->r_addend);
1808 
1809       if (r != bfd_reloc_ok)
1810 	{
1811 	  switch (r)
1812 	    {
1813 	    default:
1814 	    case bfd_reloc_outofrange:
1815 	      abort ();
1816 	    case bfd_reloc_overflow:
1817 	      {
1818 		const char *name;
1819 
1820 		if (h != NULL)
1821 		  name = NULL;
1822 		else
1823 		  {
1824 		    name = bfd_elf_string_from_elf_section (input_bfd,
1825 							    symtab_hdr->sh_link,
1826 							    sym->st_name);
1827 		    if (name == NULL)
1828 		      return FALSE;
1829 		    if (*name == '\0')
1830 		      name = bfd_section_name (input_bfd, sec);
1831 		  }
1832 		if (!(info->callbacks->reloc_overflow
1833 		      (info, (h ? &h->root : NULL), name, howto->name,
1834 		       (bfd_vma) 0, input_bfd, input_section,
1835 		       rel->r_offset)))
1836 		  return FALSE;
1837 	      }
1838 	      break;
1839 	    }
1840 	}
1841     }
1842 
1843   return TRUE;
1844 }
1845 
1846 /* Finish up dynamic symbol handling.  We set the contents of various
1847    dynamic sections here.  */
1848 
1849 static bfd_boolean
1850 elf_vax_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
1851 			       struct elf_link_hash_entry *h,
1852 			       Elf_Internal_Sym *sym)
1853 {
1854   bfd *dynobj;
1855 
1856   dynobj = elf_hash_table (info)->dynobj;
1857 
1858   if (h->plt.offset != (bfd_vma) -1)
1859     {
1860       asection *splt;
1861       asection *sgot;
1862       asection *srela;
1863       bfd_vma plt_index;
1864       bfd_vma got_offset;
1865       bfd_vma addend;
1866       Elf_Internal_Rela rela;
1867       bfd_byte *loc;
1868 
1869       /* This symbol has an entry in the procedure linkage table.  Set
1870 	 it up.  */
1871       BFD_ASSERT (h->dynindx != -1);
1872 
1873       splt = bfd_get_section_by_name (dynobj, ".plt");
1874       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1875       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1876       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1877 
1878       addend = 2 * (h->plt.offset & 1);
1879       h->plt.offset &= ~1;
1880 
1881       /* Get the index in the procedure linkage table which
1882 	 corresponds to this symbol.  This is the index of this symbol
1883 	 in all the symbols for which we are making plt entries.  The
1884 	 first entry in the procedure linkage table is reserved.  */
1885       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1886 
1887       /* Get the offset into the .got table of the entry that
1888 	 corresponds to this function.  Each .got entry is 4 bytes.
1889 	 The first two are reserved.  */
1890       got_offset = (plt_index + 3) * 4;
1891 
1892       /* Fill in the entry in the procedure linkage table.  */
1893       memcpy (splt->contents + h->plt.offset, elf_vax_plt_entry,
1894 	          PLT_ENTRY_SIZE);
1895 
1896       /* The offset is relative to the first extension word.  */
1897       bfd_put_32 (output_bfd,
1898 		  -(h->plt.offset + 8),
1899 		  splt->contents + h->plt.offset + 4);
1900 
1901       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1902 		  splt->contents + h->plt.offset + 8);
1903 
1904       /* Fill in the entry in the global offset table.  */
1905       bfd_put_32 (output_bfd,
1906 		  (splt->output_section->vma
1907 		   + splt->output_offset
1908 		   + h->plt.offset) + addend,
1909 		  sgot->contents + got_offset);
1910 
1911       /* Fill in the entry in the .rela.plt section.  */
1912       rela.r_offset = (sgot->output_section->vma
1913 		       + sgot->output_offset
1914 		       + got_offset);
1915       rela.r_info = ELF32_R_INFO (h->dynindx, R_VAX_JMP_SLOT);
1916       rela.r_addend = addend;
1917       loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
1918       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1919 
1920       if (!h->def_regular)
1921 	{
1922 	  /* Mark the symbol as undefined, rather than as defined in
1923 	     the .plt section.  Leave the value alone.  */
1924 	  sym->st_shndx = SHN_UNDEF;
1925 	}
1926     }
1927 
1928   if (h->got.offset != (bfd_vma) -1)
1929     {
1930       asection *sgot;
1931       asection *srela;
1932       Elf_Internal_Rela rela;
1933       bfd_byte *loc;
1934 
1935       /* This symbol has an entry in the global offset table.  Set it
1936 	 up.  */
1937       sgot = bfd_get_section_by_name (dynobj, ".got");
1938       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1939       BFD_ASSERT (sgot != NULL && srela != NULL);
1940 
1941       rela.r_offset = (sgot->output_section->vma
1942 		       + sgot->output_offset
1943 		       + (h->got.offset &~ 1));
1944 
1945       /* If the symbol was forced to be local because of a version file
1946 	 locally we just want to emit a RELATIVE reloc.  The entry in
1947 	 the global offset table will already have been initialized in
1948 	 the relocate_section function.  */
1949       if (info->shared
1950 	  && h->dynindx == -1
1951 	  && h->def_regular)
1952 	{
1953 	  rela.r_info = ELF32_R_INFO (0, R_VAX_RELATIVE);
1954 	}
1955       else
1956 	{
1957 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_VAX_GLOB_DAT);
1958 	}
1959       rela.r_addend = bfd_get_signed_32 (output_bfd,
1960 					 (sgot->contents
1961 					  + (h->got.offset & ~1)));
1962 
1963       loc = srela->contents;
1964       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
1965       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1966     }
1967 
1968   if (h->needs_copy)
1969     {
1970       asection *s;
1971       Elf_Internal_Rela rela;
1972       bfd_byte *loc;
1973 
1974       /* This symbol needs a copy reloc.  Set it up.  */
1975       BFD_ASSERT (h->dynindx != -1
1976 		  && (h->root.type == bfd_link_hash_defined
1977 		      || h->root.type == bfd_link_hash_defweak));
1978 
1979       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1980 				   ".rela.bss");
1981       BFD_ASSERT (s != NULL);
1982 
1983       rela.r_offset = (h->root.u.def.value
1984 		       + h->root.u.def.section->output_section->vma
1985 		       + h->root.u.def.section->output_offset);
1986       rela.r_info = ELF32_R_INFO (h->dynindx, R_VAX_COPY);
1987       rela.r_addend = 0;
1988       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
1989       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1990     }
1991 
1992   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1993   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1994       || h == elf_hash_table (info)->hgot)
1995     sym->st_shndx = SHN_ABS;
1996 
1997   return TRUE;
1998 }
1999 
2000 /* Finish up the dynamic sections.  */
2001 
2002 static bfd_boolean
2003 elf_vax_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2004 {
2005   bfd *dynobj;
2006   asection *sgot;
2007   asection *sdyn;
2008 
2009   dynobj = elf_hash_table (info)->dynobj;
2010 
2011   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2012   BFD_ASSERT (sgot != NULL);
2013   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2014 
2015   if (elf_hash_table (info)->dynamic_sections_created)
2016     {
2017       asection *splt;
2018       Elf32_External_Dyn *dyncon, *dynconend;
2019 
2020       splt = bfd_get_section_by_name (dynobj, ".plt");
2021       BFD_ASSERT (splt != NULL && sdyn != NULL);
2022 
2023       dyncon = (Elf32_External_Dyn *) sdyn->contents;
2024       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2025       for (; dyncon < dynconend; dyncon++)
2026 	{
2027 	  Elf_Internal_Dyn dyn;
2028 	  const char *name;
2029 	  asection *s;
2030 
2031 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2032 
2033 	  switch (dyn.d_tag)
2034 	    {
2035 	    default:
2036 	      break;
2037 
2038 	    case DT_PLTGOT:
2039 	      name = ".got";
2040 	      goto get_vma;
2041 	    case DT_JMPREL:
2042 	      name = ".rela.plt";
2043 	    get_vma:
2044 	      s = bfd_get_section_by_name (output_bfd, name);
2045 	      BFD_ASSERT (s != NULL);
2046 	      dyn.d_un.d_ptr = s->vma;
2047 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2048 	      break;
2049 
2050 	    case DT_PLTRELSZ:
2051 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2052 	      BFD_ASSERT (s != NULL);
2053 	      dyn.d_un.d_val = s->size;
2054 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2055 	      break;
2056 
2057 	    case DT_RELASZ:
2058 	      /* The procedure linkage table relocs (DT_JMPREL) should
2059 		 not be included in the overall relocs (DT_RELA).
2060 		 Therefore, we override the DT_RELASZ entry here to
2061 		 make it not include the JMPREL relocs.  Since the
2062 		 linker script arranges for .rela.plt to follow all
2063 		 other relocation sections, we don't have to worry
2064 		 about changing the DT_RELA entry.  */
2065 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2066 	      if (s != NULL)
2067 		dyn.d_un.d_val -= s->size;
2068 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2069 	      break;
2070 	    }
2071 	}
2072 
2073       /* Fill in the first entry in the procedure linkage table.  */
2074       if (splt->size > 0)
2075 	{
2076 	  memcpy (splt->contents, elf_vax_plt0_entry, PLT_ENTRY_SIZE);
2077 	  bfd_put_32 (output_bfd,
2078 		          (sgot->output_section->vma
2079 		           + sgot->output_offset + 4
2080 		           - (splt->output_section->vma + 6)),
2081 		          splt->contents + 2);
2082 	  bfd_put_32 (output_bfd,
2083 		          (sgot->output_section->vma
2084 		           + sgot->output_offset + 8
2085 		           - (splt->output_section->vma + 12)),
2086 		          splt->contents + 8);
2087           elf_section_data (splt->output_section)->this_hdr.sh_entsize
2088            = PLT_ENTRY_SIZE;
2089 	}
2090     }
2091 
2092   /* Fill in the first three entries in the global offset table.  */
2093   if (sgot->size > 0)
2094     {
2095       if (sdyn == NULL)
2096 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2097       else
2098 	bfd_put_32 (output_bfd,
2099 		    sdyn->output_section->vma + sdyn->output_offset,
2100 		    sgot->contents);
2101       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2102       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2103     }
2104 
2105   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2106 
2107   return TRUE;
2108 }
2109 
2110 static enum elf_reloc_type_class
2111 elf_vax_reloc_type_class (const Elf_Internal_Rela *rela)
2112 {
2113   switch ((int) ELF32_R_TYPE (rela->r_info))
2114     {
2115     case R_VAX_RELATIVE:
2116       return reloc_class_relative;
2117     case R_VAX_JMP_SLOT:
2118       return reloc_class_plt;
2119     case R_VAX_COPY:
2120       return reloc_class_copy;
2121     default:
2122       return reloc_class_normal;
2123     }
2124 }
2125 
2126 static bfd_vma
2127 elf_vax_plt_sym_val (bfd_vma i, const asection *plt,
2128 		     const arelent *rel ATTRIBUTE_UNUSED)
2129 {
2130   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
2131 }
2132 
2133 #define TARGET_LITTLE_SYM		bfd_elf32_vax_vec
2134 #define TARGET_LITTLE_NAME		"elf32-vax"
2135 #define ELF_MACHINE_CODE		EM_VAX
2136 #define ELF_MAXPAGESIZE			0x10000
2137 
2138 #define elf_backend_create_dynamic_sections \
2139 					_bfd_elf_create_dynamic_sections
2140 #define bfd_elf32_bfd_link_hash_table_create \
2141 					elf_vax_link_hash_table_create
2142 #define bfd_elf32_bfd_copy_private_bfd_data \
2143 					elf32_vax_copy_private_bfd_data
2144 
2145 #define bfd_elf32_bfd_final_link	bfd_elf_gc_common_final_link
2146 
2147 #define elf_backend_check_relocs	elf_vax_check_relocs
2148 #define elf_backend_adjust_dynamic_symbol \
2149 					elf_vax_adjust_dynamic_symbol
2150 #define elf_backend_size_dynamic_sections \
2151 					elf_vax_size_dynamic_sections
2152 #define elf_backend_init_index_section	_bfd_elf_init_1_index_section
2153 #define elf_backend_relocate_section	elf_vax_relocate_section
2154 #define elf_backend_finish_dynamic_symbol \
2155 					elf_vax_finish_dynamic_symbol
2156 #define elf_backend_finish_dynamic_sections \
2157 					elf_vax_finish_dynamic_sections
2158 #define elf_backend_reloc_type_class	elf_vax_reloc_type_class
2159 #define elf_backend_gc_mark_hook	elf_vax_gc_mark_hook
2160 #define elf_backend_gc_sweep_hook	elf_vax_gc_sweep_hook
2161 #define elf_backend_plt_sym_val		elf_vax_plt_sym_val
2162 #define bfd_elf32_bfd_merge_private_bfd_data \
2163                                         elf32_vax_merge_private_bfd_data
2164 #define bfd_elf32_bfd_set_private_flags \
2165                                         elf32_vax_set_private_flags
2166 #define bfd_elf32_bfd_print_private_bfd_data \
2167                                         elf32_vax_print_private_bfd_data
2168 
2169 #define elf_backend_can_gc_sections	1
2170 #define elf_backend_want_got_plt	1
2171 #define elf_backend_plt_readonly	1
2172 #define elf_backend_want_plt_sym	0
2173 #define elf_backend_got_header_size	16
2174 #define elf_backend_rela_normal		1
2175 
2176 #include "elf32-target.h"
2177