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