2010-01-27 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / bfd / vms.c
CommitLineData
252b5132
RH
1/* vms.c -- BFD back-end for VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
66eb6687 3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
0c376465
TG
4 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 Main file.
252b5132
RH
7
8 Written by Klaus K"ampf (kkaempf@rmi.de)
9
7920ce38
NC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
cd123cb7 12 the Free Software Foundation; either version 3 of the License, or
7920ce38 13 (at your option) any later version.
252b5132 14
7920ce38
NC
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
252b5132 19
7920ce38
NC
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
cd123cb7
NC
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
252b5132 24
0c376465
TG
25#ifdef VMS
26#include <rms.h>
11d73275 27#include <unixlib.h>
0c376465
TG
28#include <starlet.h>
29#define RME$C_SETRFM 0x00000001
30#include <unistd.h>
31#endif
32
252b5132 33#include "sysdep.h"
3db64b00 34#include "bfd.h"
252b5132
RH
35#include "bfdlink.h"
36#include "libbfd.h"
37
38#include "vms.h"
39
0c376465
TG
40static bfd_boolean vms_initialize (bfd *);
41static bfd_boolean fill_section_ptr (struct bfd_hash_entry *, PTR);
42static bfd_boolean vms_fixup_sections (bfd *);
43static bfd_boolean copy_symbols (struct bfd_hash_entry *, PTR);
44static bfd_reloc_status_type reloc_nil (bfd *, arelent *, asymbol *, PTR,
45 asection *, bfd *, char **);
46static int vms_slurp_module (bfd *abfd);
47static int vms_slurp_image (bfd *abfd);
48static const struct bfd_target *vms_object_p (bfd *abfd);
0c376465
TG
49static bfd_boolean vms_mkobject (bfd *abfd);
50static bfd_boolean vms_write_object_contents (bfd *abfd);
51static void free_reloc_stream (bfd *abfd, asection *section, void *data);
52static bfd_boolean vms_close_and_cleanup (bfd *abfd);
0c376465
TG
53static bfd_boolean vms_new_section_hook (bfd *abfd, asection *section);
54static bfd_boolean vms_get_section_contents
55 (bfd *abfd, asection *section, PTR x1, file_ptr x2, bfd_size_type x3);
0c376465
TG
56static long vms_get_symtab_upper_bound (bfd *abfd);
57static long vms_canonicalize_symtab (bfd *abfd, asymbol **symbols);
58static void vms_print_symbol (bfd *abfd, PTR file, asymbol *symbol,
59 bfd_print_symbol_type how);
60static void vms_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret);
61static bfd_boolean vms_bfd_is_local_label_name (bfd *abfd, const char *);
0c376465
TG
62static bfd_boolean vms_find_nearest_line
63 (bfd *abfd, asection *section, asymbol **symbols, bfd_vma offset,
64 const char **file, const char **func, unsigned int *line);
0c376465
TG
65static void alloc_reloc_stream (bfd *abfd, asection *section,
66 void *alloc_error);
67static bfd_boolean vms_slurp_reloc_table (bfd *abfd, asection *section,
68 asymbol **symbols);
69static long vms_get_reloc_upper_bound (bfd *abfd, asection *sect);
70static long vms_canonicalize_reloc (bfd *abfd, asection *srcsec,
71 arelent **location, asymbol **symbols);
72static const struct reloc_howto_struct *vms_bfd_reloc_type_lookup
73 (bfd *abfd, bfd_reloc_code_real_type code);
74static bfd_boolean vms_set_arch_mach
75 (bfd *abfd, enum bfd_architecture arch, unsigned long mach);
76static bfd_boolean vms_set_section_contents
77 (bfd *abfd, asection *section, const PTR location, file_ptr offset,
78 bfd_size_type count);
0c376465 79
3c9458e9 80#define vms_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
7920ce38
NC
81#define vms_make_empty_symbol _bfd_generic_make_empty_symbol
82#define vms_bfd_link_just_syms _bfd_generic_link_just_syms
1338dd10
PB
83#define vms_bfd_copy_link_hash_symbol_type \
84 _bfd_generic_copy_link_hash_symbol_type
7920ce38
NC
85#define vms_bfd_is_group_section bfd_generic_is_group_section
86#define vms_bfd_discard_group bfd_generic_discard_group
87#define vms_section_already_linked _bfd_generic_section_already_linked
3023e3f6 88#define vms_bfd_define_common_symbol bfd_generic_define_common_symbol
7920ce38
NC
89#define vms_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
90#define vms_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
252b5132 91
e4d2e665 92#define vms_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
e4d2e665
TG
93#define vms_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
94#define vms_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
95#define vms_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
96#define vms_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
97#define vms_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
98#define vms_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
99#define vms_read_minisymbols _bfd_generic_read_minisymbols
100#define vms_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
101#define vms_get_lineno _bfd_nosymbols_get_lineno
102#define vms_find_inliner_info _bfd_nosymbols_find_inliner_info
103#define vms_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
0c376465
TG
104\f
105#ifdef VMS_DEBUG
106/* Cause debug info to be emitted for the structure. */
107struct vms_private_data_struct _vms_private_data_struct_dummy;
108struct vms_section_data_struct _vms_section_data_struct_dummy;
109#endif
110
7920ce38
NC
111extern const bfd_target vms_vax_vec;
112extern const bfd_target vms_alpha_vec;
252b5132 113
0c376465 114/* Initialize private data */
b34976b6 115static bfd_boolean
7920ce38 116vms_initialize (bfd * abfd)
252b5132 117{
dc810e39 118 bfd_size_type amt;
252b5132 119
dc810e39 120 bfd_set_start_address (abfd, (bfd_vma) -1);
252b5132 121
dc810e39 122 amt = sizeof (struct vms_private_data_struct);
0c376465 123 abfd->tdata.any = bfd_zalloc (abfd, amt);
7920ce38 124 if (abfd->tdata.any == NULL)
b34976b6 125 return FALSE;
252b5132 126
0c376465
TG
127 if (bfd_get_flavour (abfd) == bfd_target_ovax_flavour)
128 PRIV (is_vax) = TRUE;
129
8a0c27cd 130 PRIV (file_format) = FF_UNKNOWN;
8a0c27cd 131
dc810e39 132 amt = sizeof (struct stack_struct) * STACKSIZE;
7920ce38
NC
133 PRIV (stack) = bfd_alloc (abfd, amt);
134 if (PRIV (stack) == NULL)
487e54f2 135 goto error_ret1;
252b5132 136
dc810e39 137 amt = sizeof (struct bfd_hash_table);
7920ce38
NC
138 PRIV (vms_symbol_table) = bfd_alloc (abfd, amt);
139 if (PRIV (vms_symbol_table) == NULL)
487e54f2 140 goto error_ret1;
252b5132 141
66eb6687
AM
142 if (!bfd_hash_table_init (PRIV (vms_symbol_table), _bfd_vms_hash_newfunc,
143 sizeof (vms_symbol_entry)))
487e54f2 144 goto error_ret1;
252b5132 145
487e54f2 146 amt = MAX_OUTREC_SIZE;
7920ce38
NC
147 PRIV (output_buf) = bfd_alloc (abfd, amt);
148 if (PRIV (output_buf) == NULL)
487e54f2
AM
149 goto error_ret2;
150
8a0c27cd 151 PRIV (length_pos) = 2;
252b5132 152
b34976b6 153 return TRUE;
487e54f2
AM
154
155 error_ret2:
156 bfd_hash_table_free (PRIV (vms_symbol_table));
157 error_ret1:
158 bfd_release (abfd, abfd->tdata.any);
7920ce38 159 abfd->tdata.any = NULL;
b34976b6 160 return FALSE;
252b5132
RH
161}
162
0c376465
TG
163struct pair
164{
165 unsigned int section_count;
166 asection **sections;
167};
168
169/* Fill symbol->section with section pointer.
170
252b5132 171 symbol->section is filled with the section index for defined symbols
0c376465 172 during reading the GSD/EGSD section. But we need the pointer to the
252b5132
RH
173 bfd section later.
174
0c376465 175 It has the correct value for referenced (undefined section) symbols.
252b5132 176
0c376465 177 Called from bfd_hash_traverse in vms_fixup_sections. */
252b5132 178
b34976b6 179static bfd_boolean
0c376465 180fill_section_ptr (struct bfd_hash_entry *entry, void *sections)
252b5132 181{
0c376465
TG
182 asymbol *sym = ((vms_symbol_entry *)entry)->symbol;
183 struct pair *data = (struct pair *)sections;
184 unsigned long sec = (unsigned long)sym->section;
252b5132 185
e4d2e665 186 vms_debug2 ((6, "fill_section_ptr: sym %p, sec %lu\n", sym, sec));
252b5132 187
0c376465
TG
188 if (sec < data->section_count)
189 {
190 sym->section = data->sections[sec];
252b5132 191
0c376465
TG
192 if (strcmp (sym->name, sym->section->name) == 0)
193 sym->flags |= BSF_SECTION_SYM;
194 }
195 else if (sec == (unsigned long)-1)
196 sym->section = &bfd_und_section;
771deb08 197
b34976b6 198 return TRUE;
252b5132
RH
199}
200
0c376465 201/* Fixup section pointers in symbols. */
b34976b6 202static bfd_boolean
7920ce38 203vms_fixup_sections (bfd * abfd)
252b5132 204{
0c376465
TG
205 struct pair data;
206
8a0c27cd 207 if (PRIV (fixup_done))
b34976b6 208 return TRUE;
252b5132 209
0c376465
TG
210 data.section_count = PRIV (section_count);
211 data.sections = PRIV (sections);
212 bfd_hash_traverse (PRIV (vms_symbol_table), fill_section_ptr, &data);
252b5132 213
b34976b6 214 PRIV (fixup_done) = TRUE;
b34976b6 215 return TRUE;
252b5132 216}
252b5132 217
0c376465
TG
218/* Slurp an ordered set of VMS object records. */
219int
220_bfd_vms_slurp_object_records (bfd * abfd)
252b5132 221{
0c376465 222 int err, new_type, type = -1;
252b5132
RH
223
224 do
225 {
e4d2e665
TG
226 vms_debug2 ((7, "reading at %08lx\n", bfd_tell (abfd)));
227
0c376465
TG
228 new_type = _bfd_vms_get_object_record (abfd);
229 if (new_type < 0)
252b5132 230 {
e4d2e665 231 vms_debug2 ((2, "next_record failed\n"));
0c376465 232 return -1;
252b5132
RH
233 }
234
0c376465 235 if (type == EOBJ_S_C_EGSD && new_type != EOBJ_S_C_EGSD)
252b5132 236 {
82e51918 237 if (! vms_fixup_sections (abfd))
252b5132 238 {
e4d2e665 239 vms_debug2 ((2, "vms_fixup_sections failed\n"));
0c376465 240 return -1;
252b5132
RH
241 }
242 }
243
0c376465 244 type = new_type;
252b5132 245
0c376465 246 switch (type)
252b5132
RH
247 {
248 case OBJ_S_C_HDR:
249 case EOBJ_S_C_EMH:
0c376465 250 err = _bfd_vms_slurp_hdr (abfd, type);
252b5132
RH
251 break;
252 case OBJ_S_C_EOM:
253 case OBJ_S_C_EOMW:
254 case EOBJ_S_C_EEOM:
0c376465 255 err = _bfd_vms_slurp_eom (abfd, type);
252b5132
RH
256 break;
257 case OBJ_S_C_GSD:
258 case EOBJ_S_C_EGSD:
0c376465 259 err = _bfd_vms_slurp_gsd (abfd, type);
252b5132
RH
260 break;
261 case OBJ_S_C_TIR:
262 case EOBJ_S_C_ETIR:
0c376465 263 err = _bfd_vms_slurp_tir (abfd, type);
252b5132
RH
264 break;
265 case OBJ_S_C_DBG:
266 case EOBJ_S_C_EDBG:
0c376465
TG
267 err = _bfd_vms_slurp_dbg (abfd, type);
268 PRIV (dst_ptr_end) = PRIV (image_ptr);
252b5132
RH
269 break;
270 case OBJ_S_C_TBT:
271 case EOBJ_S_C_ETBT:
0c376465
TG
272 err = _bfd_vms_slurp_tbt (abfd, type);
273 PRIV (dst_ptr_end) = PRIV (image_ptr);
252b5132
RH
274 break;
275 case OBJ_S_C_LNK:
0c376465 276 err = _bfd_vms_slurp_lnk (abfd, type);
252b5132
RH
277 break;
278 default:
279 err = -1;
280 }
281 if (err != 0)
282 {
e4d2e665 283 vms_debug2 ((2, "slurp type %d failed with %d\n", type, err));
0c376465 284 return err;
252b5132
RH
285 }
286 }
0c376465
TG
287 while (type != EOBJ_S_C_EEOM && type != OBJ_S_C_EOM && type != OBJ_S_C_EOMW);
288
289 return 0;
290}
291
292/* Slurp a VMS module and return an error status. */
293
294static int
295vms_slurp_module (bfd *abfd)
296{
297 int type, err;
298
299 if (PRIV (is_vax))
300 type = PRIV (vms_rec)[0];
301 else
302 type = bfd_getl16 (PRIV (vms_rec));
303
304 err = _bfd_vms_slurp_hdr (abfd, type);
305 if (err != 0)
306 {
307 bfd_set_error (bfd_error_wrong_format);
308 return err;
309 }
310
311 return _bfd_vms_slurp_object_records (abfd);
312}
313
314/* Slurp a VMS image and return an error status. */
315
316static int
317vms_slurp_image (bfd *abfd)
318{
319 unsigned int isd_offset, ihs_offset;
320 int err;
321
322 err = _bfd_vms_slurp_ihd (abfd, &isd_offset, &ihs_offset);
323 if (err != 0)
324 {
325 bfd_set_error (bfd_error_wrong_format);
326 return err;
327 }
252b5132 328
0c376465
TG
329 err = _bfd_vms_slurp_isd (abfd, isd_offset);
330 if (err != 0)
331 {
332 bfd_set_error (bfd_error_wrong_format);
333 return err;
334 }
335
336 return _bfd_vms_slurp_ihs (abfd, ihs_offset);
337}
338
339/* Check the format for a file being read.
340 Return a (bfd_target *) if it's an object file or zero if not. */
341
342static const struct bfd_target *
343vms_object_p (bfd *abfd)
344{
345 const struct bfd_target *target_vector;
346 const bfd_arch_info_type *arch;
347 PTR tdata_save = abfd->tdata.any;
348 bfd_vma saddr_save = bfd_get_start_address (abfd);
349 int err = 0;
350
e4d2e665 351 vms_debug2 ((1, "vms_object_p(%p)\n", abfd));
0c376465
TG
352
353 if (!vms_initialize (abfd))
354 goto error_ret;
355
356 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET))
357 goto err_wrong_format;
358
359 switch (_bfd_vms_get_first_record (abfd))
360 {
361 case FT_UNKNOWN:
362 default:
363 err = -1;
364 break;
365
366 case FT_MODULE:
367 err = vms_slurp_module (abfd);
368 break;
369
370 case FT_IMAGE:
371 err = vms_slurp_image (abfd);
372 break;
373 }
374
375 if (err != 0)
376 goto err_wrong_format;
377
378 if (PRIV (is_vax))
252b5132 379 {
82e51918 380 if (! vms_fixup_sections (abfd))
252b5132 381 {
e4d2e665 382 vms_debug2 ((2, "vms_fixup_sections failed\n"));
487e54f2 383 goto err_wrong_format;
252b5132
RH
384 }
385
0c376465 386 target_vector = &vms_vax_vec;
252b5132 387 arch = bfd_scan_arch ("vax");
0c376465 388
e4d2e665 389 vms_debug2 ((2, "arch is vax\n"));
252b5132 390 }
0c376465 391 else
252b5132 392 {
7920ce38 393 /* Set arch_info to alpha. */
0c376465 394 target_vector = &vms_alpha_vec;
252b5132 395 arch = bfd_scan_arch ("alpha");
e4d2e665 396 vms_debug2 ((2, "arch is alpha\n"));
252b5132
RH
397 }
398
252b5132 399 abfd->arch_info = arch;
252b5132 400 return target_vector;
487e54f2
AM
401
402 err_wrong_format:
403 bfd_set_error (bfd_error_wrong_format);
0c376465 404
487e54f2
AM
405 error_ret:
406 if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
407 bfd_release (abfd, abfd->tdata.any);
408 abfd->tdata.any = tdata_save;
5e9aae3e 409 bfd_set_start_address (abfd, saddr_save);
487e54f2 410 return NULL;
252b5132
RH
411}
412
252b5132
RH
413/* Set the format of a file being written. */
414
b34976b6 415static bfd_boolean
7920ce38 416vms_mkobject (bfd * abfd)
252b5132 417{
0c376465
TG
418 const bfd_arch_info_type *arch;
419
e4d2e665 420 vms_debug2 ((1, "vms_mkobject (%p)\n", abfd));
252b5132
RH
421
422 if (!vms_initialize (abfd))
7920ce38 423 return FALSE;
252b5132 424
0c376465
TG
425 if (PRIV (is_vax))
426 arch = bfd_scan_arch ("vax");
427 else
428 arch = bfd_scan_arch ("alpha");
429
430 if (arch == 0)
431 {
432 bfd_set_error(bfd_error_wrong_format);
433 return FALSE;
434 }
252b5132 435
0c376465 436 abfd->arch_info = arch;
b34976b6 437 return TRUE;
252b5132
RH
438}
439
252b5132
RH
440/* Write cached information into a file being written, at bfd_close. */
441
b34976b6 442static bfd_boolean
7920ce38 443vms_write_object_contents (bfd * abfd)
252b5132 444{
e4d2e665 445 vms_debug2 ((1, "vms_write_object_contents (%p)\n", abfd));
252b5132
RH
446
447 if (abfd->section_count > 0) /* we have sections */
448 {
8a0c27cd 449 if (PRIV (is_vax))
252b5132
RH
450 {
451 if (_bfd_vms_write_hdr (abfd, OBJ_S_C_HDR) != 0)
b34976b6 452 return FALSE;
252b5132 453 if (_bfd_vms_write_gsd (abfd, OBJ_S_C_GSD) != 0)
b34976b6 454 return FALSE;
252b5132 455 if (_bfd_vms_write_tir (abfd, OBJ_S_C_TIR) != 0)
b34976b6 456 return FALSE;
252b5132 457 if (_bfd_vms_write_tbt (abfd, OBJ_S_C_TBT) != 0)
b34976b6 458 return FALSE;
252b5132 459 if (_bfd_vms_write_dbg (abfd, OBJ_S_C_DBG) != 0)
b34976b6 460 return FALSE;
252b5132
RH
461 if (abfd->section_count > 255)
462 {
463 if (_bfd_vms_write_eom (abfd, OBJ_S_C_EOMW) != 0)
b34976b6 464 return FALSE;
252b5132
RH
465 }
466 else
467 {
468 if (_bfd_vms_write_eom (abfd, OBJ_S_C_EOM) != 0)
b34976b6 469 return FALSE;
252b5132
RH
470 }
471 }
472 else
473 {
474 if (_bfd_vms_write_hdr (abfd, EOBJ_S_C_EMH) != 0)
b34976b6 475 return FALSE;
252b5132 476 if (_bfd_vms_write_gsd (abfd, EOBJ_S_C_EGSD) != 0)
b34976b6 477 return FALSE;
252b5132 478 if (_bfd_vms_write_tir (abfd, EOBJ_S_C_ETIR) != 0)
b34976b6 479 return FALSE;
252b5132 480 if (_bfd_vms_write_tbt (abfd, EOBJ_S_C_ETBT) != 0)
b34976b6 481 return FALSE;
252b5132 482 if (_bfd_vms_write_dbg (abfd, EOBJ_S_C_EDBG) != 0)
b34976b6 483 return FALSE;
252b5132 484 if (_bfd_vms_write_eom (abfd, EOBJ_S_C_EEOM) != 0)
b34976b6 485 return FALSE;
252b5132
RH
486 }
487 }
b34976b6 488 return TRUE;
252b5132
RH
489}
490
7920ce38 491/* 4.1, generic. */
252b5132 492
0c376465
TG
493/* Free the reloc buffer for the specified section. */
494
495static void
496free_reloc_stream (bfd *abfd ATTRIBUTE_UNUSED, asection *section,
497 void *data ATTRIBUTE_UNUSED)
498{
499 if (vms_section_data (section)->reloc_stream)
500 free (vms_section_data (section)->reloc_stream);
501}
502
503#ifdef VMS
504/* Convert the file to variable record length format. This is done
505 using undocumented system call sys$modify().
506 Pure VMS version. */
507
508static void
509vms_convert_to_var (char *vms_filename)
510{
511 struct FAB fab = cc$rms_fab;
512
513 fab.fab$l_fna = vms_filename;
514 fab.fab$b_fns = strlen (vms_filename);
515 fab.fab$b_fac = FAB$M_PUT;
516 fab.fab$l_fop = FAB$M_ESC;
517 fab.fab$l_ctx = RME$C_SETRFM;
771deb08 518
0c376465 519 sys$open (&fab);
771deb08 520
0c376465 521 fab.fab$b_rfm = FAB$C_VAR;
771deb08 522
0c376465
TG
523 sys$modify (&fab);
524 sys$close (&fab);
525}
526
527static int
528vms_convert_to_var_1 (char *filename, int type)
529{
530 if (type != DECC$K_FILE)
531 return FALSE;
532 vms_convert_to_var (filename);
533 return TRUE;
534}
535
536/* Convert the file to variable record length format. This is done
537 using undocumented system call sys$modify().
538 Unix filename version. */
539
540static int
541vms_convert_to_var_unix_filename (const char *unix_filename)
542{
543 if (decc$to_vms (unix_filename, &vms_convert_to_var_1, 0, 1) != 1)
544 return FALSE;
545 return TRUE;
546}
547#endif /* VMS */
548
252b5132
RH
549/* Called when the BFD is being closed to do any necessary cleanup. */
550
b34976b6 551static bfd_boolean
7920ce38 552vms_close_and_cleanup (bfd * abfd)
252b5132 553{
e4d2e665
TG
554 vms_debug2 ((1, "vms_close_and_cleanup (%p)\n", abfd));
555
0c376465 556 if (abfd == NULL || abfd->tdata.any == NULL)
b34976b6 557 return TRUE;
252b5132 558
8a0c27cd 559 if (PRIV (vms_buf) != NULL)
487e54f2 560 free (PRIV (vms_buf));
252b5132 561
8a0c27cd 562 if (PRIV (sections) != NULL)
487e54f2 563 free (PRIV (sections));
252b5132 564
8a0c27cd 565 if (PRIV (vms_symbol_table))
487e54f2 566 bfd_hash_table_free (PRIV (vms_symbol_table));
252b5132 567
0c376465
TG
568 bfd_map_over_sections (abfd, free_reloc_stream, NULL);
569
487e54f2 570 bfd_release (abfd, abfd->tdata.any);
252b5132
RH
571 abfd->tdata.any = NULL;
572
0c376465
TG
573#ifdef VMS
574 if (abfd->direction == write_direction)
575 {
576 /* Last step on VMS is to convert the file to variable record length
577 format. */
578 if (bfd_cache_close (abfd) != TRUE)
579 return FALSE;
580 if (vms_convert_to_var_unix_filename (abfd->filename) != TRUE)
581 return FALSE;
582 }
583#endif
584
b34976b6 585 return TRUE;
252b5132
RH
586}
587
252b5132
RH
588/* Called when a new section is created. */
589
b34976b6 590static bfd_boolean
7920ce38 591vms_new_section_hook (bfd * abfd, asection *section)
252b5132 592{
0c376465
TG
593 bfd_size_type amt;
594
4e6bfe8a
AM
595 /* Count hasn't been incremented yet. */
596 unsigned int section_count = abfd->section_count + 1;
597
e4d2e665
TG
598 vms_debug2 ((1, "vms_new_section_hook (%p, [%d]%s), count %d\n",
599 abfd, section->index, section->name, section_count));
0c376465
TG
600
601 bfd_set_section_alignment (abfd, section, 0);
252b5132 602
4e6bfe8a 603 if (section_count > PRIV (section_count))
252b5132 604 {
91d6fa6a 605 amt = section_count;
dc810e39 606 amt *= sizeof (asection *);
515ef31d 607 PRIV (sections) = bfd_realloc_or_free (PRIV (sections), amt);
7920ce38 608 if (PRIV (sections) == NULL)
b34976b6 609 return FALSE;
4e6bfe8a 610 PRIV (section_count) = section_count;
252b5132 611 }
0c376465 612
e4d2e665 613 vms_debug2 ((6, "section_count: %d\n", PRIV (section_count)));
0c376465 614
8a0c27cd 615 PRIV (sections)[section->index] = section;
0c376465 616
e4d2e665 617 vms_debug2 ((7, "%d: %s\n", section->index, section->name));
252b5132 618
0c376465
TG
619 amt = sizeof (struct vms_section_data_struct);
620 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
621 if (section->used_by_bfd == NULL)
622 return FALSE;
623
f592407e 624 return _bfd_generic_new_section_hook (abfd, section);
252b5132
RH
625}
626
252b5132
RH
627/* Read the contents of a section.
628 buf points to a buffer of buf_size bytes to be filled with
629 section data (starting at offset into section) */
630
b34976b6 631static bfd_boolean
7920ce38
NC
632vms_get_section_contents (bfd * abfd ATTRIBUTE_UNUSED,
633 asection *section ATTRIBUTE_UNUSED,
634 void * buf ATTRIBUTE_UNUSED,
635 file_ptr offset ATTRIBUTE_UNUSED,
636 bfd_size_type buf_size ATTRIBUTE_UNUSED)
252b5132 637{
0c376465
TG
638 bfd_size_type size = section->size;
639
e4d2e665
TG
640 vms_debug2 ((1, "vms_get_section_contents (%p, %s, %p, off %ld, size %d)\n",
641 abfd, section->name, buf, offset, (int)buf_size));
252b5132 642
0c376465
TG
643 if (section->contents)
644 abort ();
645
646 section->contents = (unsigned char *) bfd_malloc (size);
647
648 if (section->contents == NULL)
649 {
650 bfd_set_error (bfd_error_no_memory);
651 return FALSE;
652 }
653
654 if (bfd_seek (abfd, section->filepos, SEEK_SET))
655 {
656 bfd_set_error (bfd_error_file_truncated);
657 return FALSE;
658 }
659
660 if (bfd_bread (section->contents, size, abfd) != size)
661 {
662 bfd_set_error (bfd_error_file_truncated);
663 return FALSE;
664 }
665
666 section->flags |= SEC_IN_MEMORY;
667
668 if (buf)
669 memcpy (buf, section->contents + offset, (size_t) buf_size);
670
671 return TRUE;
252b5132
RH
672}
673
7920ce38 674/* Part 4.5, symbols. */
252b5132
RH
675
676/* Return the number of bytes required to store a vector of pointers
677 to asymbols for all the symbols in the BFD abfd, including a
678 terminal NULL pointer. If there are no symbols in the BFD,
679 then return 0. If an error occurs, return -1. */
680
681static long
7920ce38 682vms_get_symtab_upper_bound (bfd * abfd)
252b5132 683{
e4d2e665
TG
684 vms_debug2 ((1, "vms_get_symtab_upper_bound (%p), %d symbols\n",
685 abfd, PRIV (gsd_sym_count)));
686
7920ce38 687 return (PRIV (gsd_sym_count) + 1) * sizeof (asymbol *);
252b5132
RH
688}
689
252b5132
RH
690/* Copy symbols from hash table to symbol vector
691
6cee3f79 692 called from bfd_hash_traverse in vms_canonicalize_symtab
7920ce38 693 init counter to 0 if entry == 0. */
252b5132 694
b34976b6 695static bfd_boolean
7920ce38 696copy_symbols (struct bfd_hash_entry *entry, void * arg)
252b5132 697{
7920ce38 698 bfd * abfd = (bfd *) arg;
252b5132 699
7920ce38 700 if (entry == NULL) /* Init counter. */
8a0c27cd 701 PRIV (symnum) = 0;
7920ce38 702 else /* Fill vector, inc counter. */
8a0c27cd 703 PRIV (symcache)[PRIV (symnum)++] = ((vms_symbol_entry *)entry)->symbol;
252b5132 704
b34976b6 705 return TRUE;
252b5132
RH
706}
707
252b5132
RH
708/* Read the symbols from the BFD abfd, and fills in the vector
709 location with pointers to the symbols and a trailing NULL.
710
7920ce38 711 Return number of symbols read. */
252b5132
RH
712
713static long
7920ce38 714vms_canonicalize_symtab (bfd * abfd, asymbol **symbols)
252b5132 715{
e4d2e665 716 vms_debug2 ((1, "vms_canonicalize_symtab (%p, <ret>)\n", abfd));
252b5132 717
7920ce38
NC
718 /* Init counter. */
719 copy_symbols (NULL, abfd);
252b5132 720
7920ce38 721 /* Traverse table and fill symbols vector. */
8a0c27cd 722 PRIV (symcache) = symbols;
7920ce38 723 bfd_hash_traverse (PRIV (vms_symbol_table), copy_symbols, abfd);
252b5132 724
8a0c27cd 725 symbols[PRIV (gsd_sym_count)] = NULL;
252b5132 726
8a0c27cd 727 return PRIV (gsd_sym_count);
252b5132
RH
728}
729
252b5132
RH
730/* Print symbol to file according to how. how is one of
731 bfd_print_symbol_name just print the name
732 bfd_print_symbol_more print more (???)
7920ce38 733 bfd_print_symbol_all print all we know, which is not much right now :-). */
252b5132
RH
734
735static void
7920ce38
NC
736vms_print_symbol (bfd * abfd,
737 void * file,
738 asymbol *symbol,
739 bfd_print_symbol_type how)
252b5132 740{
e4d2e665
TG
741 vms_debug2 ((1, "vms_print_symbol (%p, %p, %p, %d)\n",
742 abfd, file, symbol, how));
252b5132
RH
743
744 switch (how)
745 {
746 case bfd_print_symbol_name:
747 case bfd_print_symbol_more:
55f2d5ed 748 fprintf ((FILE *)file," %s", symbol->name);
252b5132
RH
749 break;
750
252b5132
RH
751 case bfd_print_symbol_all:
752 {
dc810e39 753 const char *section_name = symbol->section->name;
252b5132 754
7920ce38 755 bfd_print_symbol_vandf (abfd, file, symbol);
252b5132 756
7920ce38 757 fprintf ((FILE *) file," %-8s %s", section_name, symbol->name);
252b5132
RH
758 }
759 break;
760 }
252b5132
RH
761}
762
252b5132
RH
763/* Return information about symbol in ret.
764
765 fill type, value and name
766 type:
767 A absolute
768 B bss segment symbol
769 C common symbol
770 D data segment symbol
771 f filename
772 t a static function symbol
773 T text segment symbol
774 U undefined
7920ce38 775 - debug. */
252b5132
RH
776
777static void
7920ce38
NC
778vms_get_symbol_info (bfd * abfd ATTRIBUTE_UNUSED,
779 asymbol *symbol,
780 symbol_info *ret)
252b5132
RH
781{
782 asection *sec;
783
e4d2e665 784 vms_debug2 ((1, "vms_get_symbol_info (%p, %p, %p)\n", abfd, symbol, ret));
252b5132
RH
785
786 sec = symbol->section;
787
7920ce38 788 if (ret == NULL)
252b5132
RH
789 return;
790
0c376465
TG
791 if (sec == 0)
792 ret->type = 'U';
793 else if (bfd_is_com_section (sec))
252b5132
RH
794 ret->type = 'C';
795 else if (bfd_is_abs_section (sec))
796 ret->type = 'A';
797 else if (bfd_is_und_section (sec))
798 ret->type = 'U';
799 else if (bfd_is_ind_section (sec))
800 ret->type = 'I';
801 else if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
802 ret->type = 'T';
803 else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
804 ret->type = 'D';
805 else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
806 ret->type = 'B';
807 else
808 ret->type = '-';
809
810 if (ret->type != 'U')
811 ret->value = symbol->value + symbol->section->vma;
812 else
813 ret->value = 0;
814 ret->name = symbol->name;
252b5132
RH
815}
816
b34976b6
AM
817/* Return TRUE if the given symbol sym in the BFD abfd is
818 a compiler generated local label, else return FALSE. */
252b5132 819
b34976b6 820static bfd_boolean
7920ce38
NC
821vms_bfd_is_local_label_name (bfd * abfd ATTRIBUTE_UNUSED,
822 const char *name)
252b5132 823{
e4d2e665 824 vms_debug2 ((1, "vms_bfd_is_local_label_name (%p, %s)\n", abfd, name));
252b5132
RH
825 return name[0] == '$';
826}
827
252b5132
RH
828/* Provided a BFD, a section and an offset into the section, calculate and
829 return the name of the source file and the line nearest to the wanted
830 location. */
831
b34976b6 832static bfd_boolean
7920ce38
NC
833vms_find_nearest_line (bfd * abfd ATTRIBUTE_UNUSED,
834 asection *section ATTRIBUTE_UNUSED,
835 asymbol **symbols ATTRIBUTE_UNUSED,
836 bfd_vma offset ATTRIBUTE_UNUSED,
837 const char **file ATTRIBUTE_UNUSED,
838 const char **func ATTRIBUTE_UNUSED,
839 unsigned int *line ATTRIBUTE_UNUSED)
252b5132 840{
e4d2e665
TG
841 vms_debug2 ((1, "vms_find_nearest_line (%p, %s, %p, %ld, ...)\n",
842 abfd, section->name, symbols, (long int)offset));
0c376465 843 return _bfd_vms_find_nearest_dst_line (abfd, section, symbols, offset, file, func, line);
252b5132
RH
844}
845
7920ce38 846/* Part 4.6, relocations. */
252b5132 847
0c376465 848/* Allocate the reloc buffer for the specified section. */
252b5132 849
0c376465
TG
850static void
851alloc_reloc_stream (bfd *abfd ATTRIBUTE_UNUSED, asection *section,
852 void *alloc_error)
252b5132 853{
0c376465
TG
854 unsigned char *ptr;
855
856 /* If there were no relocations, there is nothing to do. */
857 if (section->reloc_count == 0)
858 return;
859
860 ptr = bfd_malloc (vms_section_data (section)->reloc_size);
861 if (ptr == NULL)
862 {
863 *(bfd_boolean *)alloc_error = TRUE;
864 return;
865 }
866
867 vms_section_data (section)->reloc_stream = ptr;
868}
869
870/* Read in the relocs for the specified section and internalize them.
871
872 The implementation is loosely based on the SOM code and made up
873 of 3 distinct phases:
874
875 1. When the VMS object is opened and parsed, the number and the size
876 of the relocations are computed for all sections. This makes it
877 possible to know upfront both which sections have no relocs and
878 the size of the reloc buffers for the other sections, at virtually
879 no cost for consumers that don't care about relocs at all.
880
881 2. When vms_slurp_reloc_table is invoked for the first time on a section
882 with relocs, the object is traversed and all the reloc information
883 is saved in per-section reloc buffers. It would be very inefficient
884 to scan the whole file on each invocation, so we slurp for all the
885 sections at once.
886
887 3. On subsequent invocations of vms_slurp_reloc_table, the relocs for the
888 specified section are fetched from the buffer, decoded and internalized.
889 The buffer is then freed since the internalized relocs are attached to
890 the section, turning additional invocations of vms_slurp_reloc_table
891 on the same section into no-ops.
892
893 Since VMS objects have very few sections, it could be profitable to merge
894 phase #2 and phase #3, i.e. to decode and internalize the relocs for all
895 the sections at once. The current implementation is more elegant. */
896
897static bfd_boolean
898vms_slurp_reloc_table (bfd *abfd, asection *section, asymbol **symbols)
899{
900 arelent *internal_relocs;
901 bfd_size_type amt;
902 int err;
903
904 /* If there were no relocations, there is nothing to do. */
905 if (section->reloc_count == 0)
906 return TRUE;
907
908 /* Return saved information about the relocations if it is available. */
909 if (section->relocation != NULL)
910 return TRUE;
911
912 /* If the relocation stream has not been slurped, do it now. */
913 if (vms_section_data (section)->reloc_stream == NULL)
914 {
915 bfd_boolean alloc_error = FALSE;
916 int type;
917
918 /* Size the reloc buffer for each section. */
919 bfd_map_over_sections (abfd, alloc_reloc_stream, &alloc_error);
920 if (alloc_error)
921 return FALSE;
922
923 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
924 return FALSE;
925
926 /* Reset section pointer. */
927 PRIV (image_section) = NULL;
928
929 do
930 {
931 type = _bfd_vms_get_object_record (abfd);
932 if (type != EOBJ_S_C_ETIR
933 && type != EOBJ_S_C_EDBG
934 && type != EOBJ_S_C_ETBT)
935 continue;
936 err = _bfd_vms_slurp_relocs (abfd);
937 if (err != 0)
938 {
e4d2e665 939 vms_debug2 ((2, "slurp relocs failed with %d\n", err));
0c376465
TG
940 return FALSE;
941 }
942 }
943 while (type != EOBJ_S_C_EEOM);
944 }
945
946 amt = section->reloc_count * sizeof (arelent);
947 internal_relocs = (arelent *) bfd_zalloc (abfd, amt);
948 if (internal_relocs == NULL)
949 return FALSE;
950
951 /* Decode and internalize the relocations. */
952 err = _bfd_vms_decode_relocs (abfd, internal_relocs, section, symbols);
953 if (err != 0)
954 {
e4d2e665 955 vms_debug2 ((2, "decode relocs failed with %d\n", err));
0c376465
TG
956 return FALSE;
957 }
958
959 /* We're done with the external relocations. Free them. */
960 free (vms_section_data (section)->reloc_stream);
961 vms_section_data (section)->reloc_stream = NULL;
962
963 /* Save our results and return success. */
964 section->relocation = internal_relocs;
965 return TRUE;
252b5132
RH
966}
967
0c376465
TG
968/* Return the number of bytes required to store the relocation
969 information associated with the given section. */
252b5132
RH
970
971static long
0c376465 972vms_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, asection *section)
252b5132 973{
771deb08 974 return (section->reloc_count + 1) * sizeof (arelent *);
0c376465
TG
975}
976
977/* Convert relocations from VMS (external) form into BFD internal
978 form. Return the number of relocations. */
979
980static long
981vms_canonicalize_reloc (bfd *abfd, asection *section, arelent **relptr,
982 asymbol **symbols)
983{
984 arelent *tblptr;
985 int count;
986
987 if (! vms_slurp_reloc_table (abfd, section, symbols))
988 return -1;
989
990 count = section->reloc_count;
991 tblptr = section->relocation;
992
993 while (count--)
994 *relptr++ = tblptr++;
995
996 *relptr = (arelent *) NULL;
997 return section->reloc_count;
252b5132 998}
7920ce38
NC
999\f
1000/* This is just copied from ecoff-alpha, needs to be fixed probably. */
252b5132
RH
1001
1002/* How to process the various reloc types. */
1003
1004static bfd_reloc_status_type
7920ce38
NC
1005reloc_nil (bfd * abfd ATTRIBUTE_UNUSED,
1006 arelent *reloc ATTRIBUTE_UNUSED,
1007 asymbol *sym ATTRIBUTE_UNUSED,
1008 void * data ATTRIBUTE_UNUSED,
1009 asection *sec ATTRIBUTE_UNUSED,
1010 bfd *output_bfd ATTRIBUTE_UNUSED,
1011 char **error_message ATTRIBUTE_UNUSED)
252b5132
RH
1012{
1013#if VMS_DEBUG
7920ce38 1014 vms_debug (1, "reloc_nil (abfd %p, output_bfd %p)\n", abfd, output_bfd);
252b5132
RH
1015 vms_debug (2, "In section %s, symbol %s\n",
1016 sec->name, sym->name);
1017 vms_debug (2, "reloc sym %s, addr %08lx, addend %08lx, reloc is a %s\n",
1018 reloc->sym_ptr_ptr[0]->name,
1019 (unsigned long)reloc->address,
1020 (unsigned long)reloc->addend, reloc->howto->name);
1021 vms_debug (2, "data at %p\n", data);
7920ce38 1022 /* _bfd_hexdump (2, data, bfd_get_reloc_size (reloc->howto), 0); */
252b5132
RH
1023#endif
1024
1025 return bfd_reloc_ok;
1026}
1027
1028/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
1029 from smaller values. Start with zero, widen, *then* decrement. */
1030#define MINUS_ONE (((bfd_vma)0) - 1)
1031
1032static reloc_howto_type alpha_howto_table[] =
1033{
7920ce38
NC
1034 HOWTO (ALPHA_R_IGNORE, /* Type. */
1035 0, /* Rightshift. */
1036 0, /* Size (0 = byte, 1 = short, 2 = long). */
1037 8, /* Bitsize. */
1038 TRUE, /* PC relative. */
1039 0, /* Bitpos. */
1040 complain_overflow_dont,/* Complain_on_overflow. */
1041 reloc_nil, /* Special_function. */
1042 "IGNORE", /* Name. */
1043 TRUE, /* Partial_inplace. */
1044 0, /* Source mask */
1045 0, /* Dest mask. */
1046 TRUE), /* PC rel offset. */
252b5132
RH
1047
1048 /* A 64 bit reference to a symbol. */
7920ce38
NC
1049 HOWTO (ALPHA_R_REFQUAD, /* Type. */
1050 0, /* Rightshift. */
1051 4, /* Size (0 = byte, 1 = short, 2 = long). */
1052 64, /* Bitsize. */
1053 FALSE, /* PC relative. */
1054 0, /* Bitpos. */
1055 complain_overflow_bitfield, /* Complain_on_overflow. */
1056 reloc_nil, /* Special_function. */
1057 "REFQUAD", /* Name. */
1058 TRUE, /* Partial_inplace. */
1059 MINUS_ONE, /* Source mask. */
1060 MINUS_ONE, /* Dest mask. */
1061 FALSE), /* PC rel offset. */
252b5132
RH
1062
1063 /* A 21 bit branch. The native assembler generates these for
1064 branches within the text segment, and also fills in the PC
1065 relative offset in the instruction. */
7920ce38
NC
1066 HOWTO (ALPHA_R_BRADDR, /* Type. */
1067 2, /* Rightshift. */
1068 2, /* Size (0 = byte, 1 = short, 2 = long). */
1069 21, /* Bitsize. */
1070 TRUE, /* PC relative. */
1071 0, /* Bitpos. */
1072 complain_overflow_signed, /* Complain_on_overflow. */
1073 reloc_nil, /* Special_function. */
1074 "BRADDR", /* Name. */
1075 TRUE, /* Partial_inplace. */
1076 0x1fffff, /* Source mask. */
1077 0x1fffff, /* Dest mask. */
1078 FALSE), /* PC rel offset. */
252b5132
RH
1079
1080 /* A hint for a jump to a register. */
7920ce38
NC
1081 HOWTO (ALPHA_R_HINT, /* Type. */
1082 2, /* Rightshift. */
1083 1, /* Size (0 = byte, 1 = short, 2 = long). */
1084 14, /* Bitsize. */
1085 TRUE, /* PC relative. */
1086 0, /* Bitpos. */
1087 complain_overflow_dont,/* Complain_on_overflow. */
1088 reloc_nil, /* Special_function. */
1089 "HINT", /* Name. */
1090 TRUE, /* Partial_inplace. */
1091 0x3fff, /* Source mask. */
1092 0x3fff, /* Dest mask. */
1093 FALSE), /* PC rel offset. */
252b5132
RH
1094
1095 /* 16 bit PC relative offset. */
7920ce38
NC
1096 HOWTO (ALPHA_R_SREL16, /* Type. */
1097 0, /* Rightshift. */
1098 1, /* Size (0 = byte, 1 = short, 2 = long). */
1099 16, /* Bitsize. */
1100 TRUE, /* PC relative. */
1101 0, /* Bitpos. */
1102 complain_overflow_signed, /* Complain_on_overflow. */
1103 reloc_nil, /* Special_function. */
1104 "SREL16", /* Name. */
1105 TRUE, /* Partial_inplace. */
1106 0xffff, /* Source mask. */
1107 0xffff, /* Dest mask. */
1108 FALSE), /* PC rel offset. */
252b5132
RH
1109
1110 /* 32 bit PC relative offset. */
7920ce38
NC
1111 HOWTO (ALPHA_R_SREL32, /* Type. */
1112 0, /* Rightshift. */
1113 2, /* Size (0 = byte, 1 = short, 2 = long). */
1114 32, /* Bitsize. */
1115 TRUE, /* PC relative. */
1116 0, /* Bitpos. */
1117 complain_overflow_signed, /* Complain_on_overflow. */
1118 reloc_nil, /* Special_function. */
1119 "SREL32", /* Name. */
1120 TRUE, /* Partial_inplace. */
1121 0xffffffff, /* Source mask. */
1122 0xffffffff, /* Dest mask. */
1123 FALSE), /* PC rel offset. */
252b5132
RH
1124
1125 /* A 64 bit PC relative offset. */
7920ce38
NC
1126 HOWTO (ALPHA_R_SREL64, /* Type. */
1127 0, /* Rightshift. */
1128 4, /* Size (0 = byte, 1 = short, 2 = long). */
1129 64, /* Bitsize. */
1130 TRUE, /* PC relative. */
1131 0, /* Bitpos. */
1132 complain_overflow_signed, /* Complain_on_overflow. */
1133 reloc_nil, /* Special_function. */
1134 "SREL64", /* Name. */
1135 TRUE, /* Partial_inplace. */
1136 MINUS_ONE, /* Source mask. */
1137 MINUS_ONE, /* Dest mask. */
1138 FALSE), /* PC rel offset. */
252b5132
RH
1139
1140 /* Push a value on the reloc evaluation stack. */
7920ce38
NC
1141 HOWTO (ALPHA_R_OP_PUSH, /* Type. */
1142 0, /* Rightshift. */
1143 0, /* Size (0 = byte, 1 = short, 2 = long). */
1144 0, /* Bitsize. */
1145 FALSE, /* PC relative. */
1146 0, /* Bitpos. */
1147 complain_overflow_dont,/* Complain_on_overflow. */
1148 reloc_nil, /* Special_function. */
1149 "OP_PUSH", /* Name. */
1150 FALSE, /* Partial_inplace. */
1151 0, /* Source mask. */
1152 0, /* Dest mask. */
1153 FALSE), /* PC rel offset. */
252b5132
RH
1154
1155 /* Store the value from the stack at the given address. Store it in
1156 a bitfield of size r_size starting at bit position r_offset. */
7920ce38
NC
1157 HOWTO (ALPHA_R_OP_STORE, /* Type. */
1158 0, /* Rightshift. */
1159 4, /* Size (0 = byte, 1 = short, 2 = long). */
1160 64, /* Bitsize. */
1161 FALSE, /* PC relative. */
1162 0, /* Bitpos. */
1163 complain_overflow_dont,/* Complain_on_overflow. */
1164 reloc_nil, /* Special_function. */
1165 "OP_STORE", /* Name. */
1166 FALSE, /* Partial_inplace. */
1167 0, /* Source mask. */
1168 MINUS_ONE, /* Dest mask. */
1169 FALSE), /* PC rel offset. */
252b5132
RH
1170
1171 /* Subtract the reloc address from the value on the top of the
1172 relocation stack. */
7920ce38
NC
1173 HOWTO (ALPHA_R_OP_PSUB, /* Type. */
1174 0, /* Rightshift. */
1175 0, /* Size (0 = byte, 1 = short, 2 = long). */
1176 0, /* Bitsize. */
1177 FALSE, /* PC relative. */
1178 0, /* Bitpos. */
1179 complain_overflow_dont,/* Complain_on_overflow. */
1180 reloc_nil, /* Special_function. */
1181 "OP_PSUB", /* Name. */
1182 FALSE, /* Partial_inplace. */
1183 0, /* Source mask. */
1184 0, /* Dest mask. */
1185 FALSE), /* PC rel offset. */
252b5132
RH
1186
1187 /* Shift the value on the top of the relocation stack right by the
1188 given value. */
7920ce38
NC
1189 HOWTO (ALPHA_R_OP_PRSHIFT, /* Type. */
1190 0, /* Rightshift. */
1191 0, /* Size (0 = byte, 1 = short, 2 = long). */
1192 0, /* Bitsize. */
1193 FALSE, /* PC relative. */
1194 0, /* Bitpos. */
1195 complain_overflow_dont,/* Complain_on_overflow. */
1196 reloc_nil, /* Special_function. */
1197 "OP_PRSHIFT", /* Name. */
1198 FALSE, /* Partial_inplace. */
1199 0, /* Source mask. */
1200 0, /* Dest mask. */
1201 FALSE), /* PC rel offset. */
252b5132
RH
1202
1203 /* Hack. Linkage is done by linker. */
7920ce38
NC
1204 HOWTO (ALPHA_R_LINKAGE, /* Type. */
1205 0, /* Rightshift. */
1206 8, /* Size (0 = byte, 1 = short, 2 = long). */
1207 256, /* Bitsize. */
1208 FALSE, /* PC relative. */
1209 0, /* Bitpos. */
1210 complain_overflow_dont,/* Complain_on_overflow. */
1211 reloc_nil, /* Special_function. */
1212 "LINKAGE", /* Name. */
1213 FALSE, /* Partial_inplace. */
1214 0, /* Source mask. */
1215 0, /* Dest mask. */
1216 FALSE), /* PC rel offset. */
252b5132
RH
1217
1218 /* A 32 bit reference to a symbol. */
7920ce38
NC
1219 HOWTO (ALPHA_R_REFLONG, /* Type. */
1220 0, /* Rightshift. */
1221 2, /* Size (0 = byte, 1 = short, 2 = long). */
1222 32, /* Bitsize. */
1223 FALSE, /* PC relative. */
1224 0, /* Bitpos. */
1225 complain_overflow_bitfield, /* Complain_on_overflow. */
1226 reloc_nil, /* Special_function. */
1227 "REFLONG", /* Name. */
1228 TRUE, /* Partial_inplace. */
1229 0xffffffff, /* Source mask. */
1230 0xffffffff, /* Dest mask. */
1231 FALSE), /* PC rel offset. */
252b5132
RH
1232
1233 /* A 64 bit reference to a procedure, written as 32 bit value. */
7920ce38
NC
1234 HOWTO (ALPHA_R_CODEADDR, /* Type. */
1235 0, /* Rightshift. */
1236 4, /* Size (0 = byte, 1 = short, 2 = long). */
1237 64, /* Bitsize. */
1238 FALSE, /* PC relative. */
1239 0, /* Bitpos. */
1240 complain_overflow_signed,/* Complain_on_overflow. */
1241 reloc_nil, /* Special_function. */
1242 "CODEADDR", /* Name. */
1243 FALSE, /* Partial_inplace. */
1244 0xffffffff, /* Source mask. */
1245 0xffffffff, /* Dest mask. */
1246 FALSE), /* PC rel offset. */
252b5132 1247
0c376465
TG
1248 HOWTO (ALPHA_R_NOP, /* Type. */
1249 0, /* Rightshift. */
1250 3, /* Size (0 = byte, 1 = short, 2 = long). */
1251 0, /* Bitsize. */
1252 /* The following value must match that of ALPHA_R_BSR/ALPHA_R_BOH
1253 because the calculations for the 3 relocations are the same.
1254 See B.4.5.2 of the OpenVMS Linker Utility Manual. */
1255 TRUE, /* PC relative. */
1256 0, /* Bitpos. */
1257 complain_overflow_dont,/* Complain_on_overflow. */
1258 reloc_nil, /* Special_function. */
1259 "NOP", /* Name. */
1260 FALSE, /* Partial_inplace. */
1261 0xffffffff, /* Source mask. */
1262 0xffffffff, /* Dest mask. */
1263 FALSE), /* PC rel offset. */
1264
1265 HOWTO (ALPHA_R_BSR, /* Type. */
1266 0, /* Rightshift. */
1267 3, /* Size (0 = byte, 1 = short, 2 = long). */
1268 0, /* Bitsize. */
1269 TRUE, /* PC relative. */
1270 0, /* Bitpos. */
1271 complain_overflow_dont,/* Complain_on_overflow. */
1272 reloc_nil, /* Special_function. */
1273 "BSR", /* Name. */
1274 FALSE, /* Partial_inplace. */
1275 0xffffffff, /* Source mask. */
1276 0xffffffff, /* Dest mask. */
1277 FALSE), /* PC rel offset. */
1278
1279 HOWTO (ALPHA_R_LDA, /* Type. */
1280 0, /* Rightshift. */
1281 3, /* Size (0 = byte, 1 = short, 2 = long). */
1282 0, /* Bitsize. */
1283 FALSE, /* PC relative. */
1284 0, /* Bitpos. */
1285 complain_overflow_dont,/* Complain_on_overflow. */
1286 reloc_nil, /* Special_function. */
1287 "LDA", /* Name. */
1288 FALSE, /* Partial_inplace. */
1289 0xffffffff, /* Source mask. */
1290 0xffffffff, /* Dest mask. */
1291 FALSE), /* PC rel offset. */
1292
1293 HOWTO (ALPHA_R_BOH, /* Type. */
1294 0, /* Rightshift. */
1295 3, /* Size (0 = byte, 1 = short, 2 = long, 3 = nil). */
1296 0, /* Bitsize. */
1297 TRUE, /* PC relative. */
1298 0, /* Bitpos. */
1299 complain_overflow_dont,/* Complain_on_overflow. */
1300 reloc_nil, /* Special_function. */
1301 "BOH", /* Name. */
1302 FALSE, /* Partial_inplace. */
1303 0xffffffff, /* Source mask. */
1304 0xffffffff, /* Dest mask. */
1305 FALSE), /* PC rel offset. */
252b5132
RH
1306};
1307
1308/* Return a pointer to a howto structure which, when invoked, will perform
1309 the relocation code on data from the architecture noted. */
1310
1311static const struct reloc_howto_struct *
7920ce38
NC
1312vms_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
1313 bfd_reloc_code_real_type code)
252b5132
RH
1314{
1315 int alpha_type;
1316
e4d2e665 1317 vms_debug2 ((1, "vms_bfd_reloc_type_lookup (%p, %d)\t", abfd, code));
252b5132
RH
1318
1319 switch (code)
1320 {
1321 case BFD_RELOC_16: alpha_type = ALPHA_R_SREL16; break;
1322 case BFD_RELOC_32: alpha_type = ALPHA_R_REFLONG; break;
1323 case BFD_RELOC_64: alpha_type = ALPHA_R_REFQUAD; break;
1324 case BFD_RELOC_CTOR: alpha_type = ALPHA_R_REFQUAD; break;
1325 case BFD_RELOC_23_PCREL_S2: alpha_type = ALPHA_R_BRADDR; break;
1326 case BFD_RELOC_ALPHA_HINT: alpha_type = ALPHA_R_HINT; break;
1327 case BFD_RELOC_16_PCREL: alpha_type = ALPHA_R_SREL16; break;
1328 case BFD_RELOC_32_PCREL: alpha_type = ALPHA_R_SREL32; break;
1329 case BFD_RELOC_64_PCREL: alpha_type = ALPHA_R_SREL64; break;
1330 case BFD_RELOC_ALPHA_LINKAGE: alpha_type = ALPHA_R_LINKAGE; break;
1331 case BFD_RELOC_ALPHA_CODEADDR: alpha_type = ALPHA_R_CODEADDR; break;
0c376465
TG
1332 case BFD_RELOC_ALPHA_NOP: alpha_type = ALPHA_R_NOP; break;
1333 case BFD_RELOC_ALPHA_BSR: alpha_type = ALPHA_R_BSR; break;
1334 case BFD_RELOC_ALPHA_LDA: alpha_type = ALPHA_R_LDA; break;
1335 case BFD_RELOC_ALPHA_BOH: alpha_type = ALPHA_R_BOH; break;
252b5132
RH
1336 default:
1337 (*_bfd_error_handler) ("reloc (%d) is *UNKNOWN*", code);
7920ce38 1338 return NULL;
252b5132 1339 }
e4d2e665 1340 vms_debug2 ((2, "reloc is %s\n", alpha_howto_table[alpha_type].name));
7920ce38 1341 return & alpha_howto_table[alpha_type];
252b5132
RH
1342}
1343
157090f7
AM
1344static reloc_howto_type *
1345vms_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1346 const char *r_name)
1347{
1348 unsigned int i;
1349
1350 for (i = 0;
1351 i < sizeof (alpha_howto_table) / sizeof (alpha_howto_table[0]);
1352 i++)
1353 if (alpha_howto_table[i].name != NULL
1354 && strcasecmp (alpha_howto_table[i].name, r_name) == 0)
1355 return &alpha_howto_table[i];
1356
1357 return NULL;
1358}
1359
7920ce38 1360/* Part 4.7, writing an object file. */
252b5132
RH
1361
1362/* Set the architecture and machine type in BFD abfd to arch and mach.
1363 Find the correct pointer to a structure and insert it into the arch_info
1364 pointer. */
1365
b34976b6 1366static bfd_boolean
7920ce38
NC
1367vms_set_arch_mach (bfd * abfd,
1368 enum bfd_architecture arch ATTRIBUTE_UNUSED,
1369 unsigned long mach ATTRIBUTE_UNUSED)
252b5132 1370{
e4d2e665 1371 vms_debug2 ((1, "vms_set_arch_mach (%p, %d, %ld)\n", abfd, arch, mach));
252b5132 1372
0c376465
TG
1373 if (arch != bfd_arch_alpha
1374 && arch != bfd_arch_vax
1375 && arch != bfd_arch_unknown)
1376 return FALSE;
1377
1378 return bfd_default_set_arch_mach (abfd, arch, mach);
252b5132
RH
1379}
1380
252b5132 1381/* Sets the contents of the section section in BFD abfd to the data starting
0c376465
TG
1382 in memory at LOCATION. The data is written to the output section starting
1383 at offset offset for count bytes.
252b5132 1384
b34976b6 1385 Normally TRUE is returned, else FALSE. Possible error returns are:
252b5132
RH
1386 o bfd_error_no_contents - The output section does not have the
1387 SEC_HAS_CONTENTS attribute, so nothing can be written to it.
1388 o and some more too */
1389
b34976b6 1390static bfd_boolean
7920ce38
NC
1391vms_set_section_contents (bfd * abfd,
1392 asection *section,
1393 const void * location,
1394 file_ptr offset,
1395 bfd_size_type count)
252b5132
RH
1396{
1397#if VMS_DEBUG
7920ce38
NC
1398 vms_debug (1, "vms_set_section_contents (%p, sec %s, loc %p, off %ld, count %d)\n",
1399 abfd, section->name, location, (long int)offset, (int)count);
eea6121a 1400 vms_debug (2, "size %d\n", (int) section->size);
252b5132 1401#endif
0c376465
TG
1402 if (count == (bfd_size_type)0)
1403 return TRUE;
1404
1405 if (section->contents == NULL)
1406 section->contents = bfd_alloc (abfd, section->size);
1407 if (section->contents == NULL)
1408 return FALSE;
1409
1410 memcpy (section->contents + offset, location, (size_t) count);
1411 return TRUE;
252b5132 1412}
771deb08
TG
1413
1414static bfd_boolean
1415vms_bfd_print_private_bfd_data (bfd *abfd, void *ptr)
1416{
1417 FILE *file = (FILE *)ptr;
1418
1419 fprintf (file, _("structure level: %d\n"), PRIV(hdr_data.hdr_b_strlvl));
1420 fprintf (file, _("module name : %s\n"), PRIV(hdr_data.hdr_t_name));
1421 fprintf (file, _("module version : %s\n"), PRIV(hdr_data.hdr_t_version));
1422 fprintf (file, _("module date : %s\n"), PRIV(hdr_data.hdr_t_date));
1423 fprintf (file, _("language name : %s\n"), PRIV(hdr_data.hdr_c_lnm));
1424 fprintf (file, _("source files : %s\n"), PRIV(hdr_data.hdr_c_src));
1425 fprintf (file, _("title : %s\n"), PRIV(hdr_data.hdr_c_ttl));
1426
1427 return TRUE;
1428}
7920ce38
NC
1429\f
1430const bfd_target vms_alpha_vec =
1431{
1432 "vms-alpha", /* Name. */
1433 bfd_target_evax_flavour,
1434 BFD_ENDIAN_LITTLE, /* Data byte order is little. */
1435 BFD_ENDIAN_LITTLE, /* Header byte order is little. */
1436
0c376465 1437 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS
7920ce38
NC
1438 | WP_TEXT | D_PAGED), /* Object flags. */
1439 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1440 | SEC_READONLY | SEC_CODE | SEC_DATA
1441 | SEC_HAS_CONTENTS | SEC_IN_MEMORY), /* Sect flags. */
0c376465
TG
1442 0, /* symbol_leading_char. */
1443 ' ', /* ar_pad_char. */
1444 15, /* ar_max_namelen. */
7920ce38
NC
1445 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1446 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1447 bfd_getl16, bfd_getl_signed_16, bfd_putl16,
1448 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1449 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1450 bfd_getl16, bfd_getl_signed_16, bfd_putl16,
1451
1452 {_bfd_dummy_target, vms_object_p, /* bfd_check_format. */
e4d2e665 1453 _bfd_dummy_target, _bfd_dummy_target},
7920ce38 1454 {bfd_false, vms_mkobject, /* bfd_set_format. */
e4d2e665 1455 bfd_false, bfd_false},
7920ce38 1456 {bfd_false, vms_write_object_contents, /* bfd_write_contents. */
e4d2e665 1457 bfd_false, bfd_false},
7920ce38
NC
1458
1459 BFD_JUMP_TABLE_GENERIC (vms),
1460 BFD_JUMP_TABLE_COPY (vms),
e4d2e665
TG
1461 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1462 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
7920ce38
NC
1463 BFD_JUMP_TABLE_SYMBOLS (vms),
1464 BFD_JUMP_TABLE_RELOCS (vms),
1465 BFD_JUMP_TABLE_WRITE (vms),
e4d2e665
TG
1466 BFD_JUMP_TABLE_LINK (_bfd_nolink),
1467 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
7920ce38
NC
1468
1469 NULL,
1470
0c376465 1471 (PTR) 0
7920ce38
NC
1472};
1473
1474const bfd_target vms_vax_vec =
1475{
1476 "vms-vax", /* Name. */
1477 bfd_target_ovax_flavour,
1478 BFD_ENDIAN_LITTLE, /* Data byte order is little. */
1479 BFD_ENDIAN_LITTLE, /* Header byte order is little. */
1480
1481 (HAS_RELOC | HAS_SYMS /* Object flags. */
1482 | WP_TEXT | D_PAGED
1483 | HAS_LINENO | HAS_DEBUG | HAS_LOCALS),
1484
1485 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1486 | SEC_READONLY | SEC_CODE | SEC_DATA
1487 | SEC_HAS_CONTENTS | SEC_IN_MEMORY), /* Sect flags. */
0c376465
TG
1488 0, /* symbol_leading_char */
1489 ' ', /* ar_pad_char */
1490 15, /* ar_max_namelen */
7920ce38
NC
1491 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1492 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1493 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
1494 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1495 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
0c376465 1496 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Hdrs. */
7920ce38
NC
1497
1498 {_bfd_dummy_target, vms_object_p, /* bfd_check_format. */
e4d2e665 1499 _bfd_dummy_target, _bfd_dummy_target},
7920ce38 1500 {bfd_false, vms_mkobject, /* bfd_set_format. */
e4d2e665 1501 bfd_false, bfd_false},
7920ce38 1502 {bfd_false, vms_write_object_contents, /* bfd_write_contents. */
e4d2e665 1503 bfd_false, bfd_false},
7920ce38
NC
1504
1505 BFD_JUMP_TABLE_GENERIC (vms),
1506 BFD_JUMP_TABLE_COPY (vms),
e4d2e665
TG
1507 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1508 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
7920ce38
NC
1509 BFD_JUMP_TABLE_SYMBOLS (vms),
1510 BFD_JUMP_TABLE_RELOCS (vms),
1511 BFD_JUMP_TABLE_WRITE (vms),
e4d2e665
TG
1512 BFD_JUMP_TABLE_LINK (_bfd_nolink),
1513 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
7920ce38
NC
1514
1515 NULL,
1516
0c376465 1517 (PTR) 0
7920ce38 1518};
This page took 0.645646 seconds and 4 git commands to generate.