PR gas/11486
[binutils/dougsmingw.git] / bfd / vms-hdr.c
blob343f53ec3b03979aa310704f5284f52674caf007
1 /* vms-hdr.c -- BFD back-end for VMS/VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
4 2007, 2008, 2009 Free Software Foundation, Inc.
6 HDR record handling functions
7 EMH record handling functions
9 EOM record handling functions
10 EEOM record handling functions
12 IHD record handling functions
13 EIHD record handling functions
15 ISD record handling functions
16 EISD record handling functions
18 IHS record handling functions
19 EIHS record handling functions
21 DBG record handling functions
22 EDBG record handling functions
24 TBT record handling functions
25 ETBT record handling functions
27 DST/DMT section handling functions
29 Written by Klaus K"ampf (kkaempf@rmi.de)
31 This program is free software; you can redistribute it and/or modify
32 it under the terms of the GNU General Public License as published by
33 the Free Software Foundation; either version 3 of the License, or
34 (at your option) any later version.
36 This program is distributed in the hope that it will be useful,
37 but WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 GNU General Public License for more details.
41 You should have received a copy of the GNU General Public License
42 along with this program; if not, write to the Free Software
43 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
44 MA 02110-1301, USA. */
46 #include "sysdep.h"
47 #include "bfd.h"
48 #include "bfdver.h"
49 #include "bfdlink.h"
50 #include "safe-ctype.h"
51 #include "libbfd.h"
53 #include "vms.h"
55 static struct module *new_module (bfd *);
56 static void parse_module
57 (bfd *, struct module *, unsigned char *, unsigned int);
58 static struct module *build_module_list (bfd *);
59 static bfd_boolean module_find_nearest_line
60 (bfd *, struct module *, bfd_vma, const char **, const char **,
61 unsigned int *);
62 static int vms_slurp_debug (bfd *);
64 #define SET_MODULE_PARSED(m) \
65 do { if ((m)->name == NULL) (m)->name = ""; } while (0)
66 #define IS_MODULE_PARSED(m) ((m)->name != NULL)
69 /* Read & process emh record
70 return 0 on success, -1 on error. */
72 int
73 _bfd_vms_slurp_hdr (bfd *abfd, int objtype)
75 unsigned char *ptr;
76 unsigned char *vms_rec;
77 int subtype;
79 vms_rec = PRIV(vms_rec);
81 #if VMS_DEBUG
82 vms_debug(2, "HDR/EMH\n");
83 #endif
85 switch (objtype)
87 case OBJ_S_C_HDR:
88 subtype = vms_rec[1];
89 break;
90 case EOBJ_S_C_EMH:
91 subtype = bfd_getl16 (vms_rec + 4) + EVAX_OFFSET;
92 break;
93 default:
94 subtype = -1;
97 #if VMS_DEBUG
98 vms_debug(3, "subtype %d\n", subtype);
99 #endif
101 switch (subtype)
103 case MHD_S_C_MHD:
104 /* Module header. */
105 PRIV (hdr_data).hdr_b_strlvl = vms_rec[2];
106 PRIV (hdr_data).hdr_l_recsiz = bfd_getl16 (vms_rec + 3);
107 PRIV (hdr_data).hdr_t_name = _bfd_vms_save_counted_string (vms_rec + 5);
108 ptr = vms_rec + 5 + vms_rec[5] + 1;
109 PRIV (hdr_data).hdr_t_version = _bfd_vms_save_counted_string (ptr);
110 ptr += *ptr + 1;
111 PRIV (hdr_data).hdr_t_date = _bfd_vms_save_sized_string (ptr, 17);
112 break;
114 case MHD_S_C_LNM:
115 PRIV (hdr_data).hdr_c_lnm = _bfd_vms_save_sized_string (vms_rec, PRIV (rec_size - 2));
116 break;
118 case MHD_S_C_SRC:
119 PRIV (hdr_data).hdr_c_src = _bfd_vms_save_sized_string (vms_rec, PRIV (rec_size - 2));
120 break;
122 case MHD_S_C_TTL:
123 PRIV (hdr_data).hdr_c_ttl = _bfd_vms_save_sized_string (vms_rec, PRIV (rec_size - 2));
124 break;
126 case EMH_S_C_MHD + EVAX_OFFSET:
127 /* Module header. */
128 PRIV (hdr_data).hdr_b_strlvl = vms_rec[6];
129 PRIV (hdr_data).hdr_l_arch1 = bfd_getl32 (vms_rec + 8);
130 PRIV (hdr_data).hdr_l_arch2 = bfd_getl32 (vms_rec + 12);
131 PRIV (hdr_data).hdr_l_recsiz = bfd_getl32 (vms_rec + 16);
132 PRIV (hdr_data).hdr_t_name = _bfd_vms_save_counted_string (vms_rec + 20);
133 ptr = vms_rec + 20 + vms_rec[20] + 1;
134 PRIV (hdr_data).hdr_t_version =_bfd_vms_save_counted_string (ptr);
135 ptr += *ptr + 1;
136 PRIV (hdr_data).hdr_t_date = _bfd_vms_save_sized_string (ptr, 17);
137 break;
139 case EMH_S_C_LNM + EVAX_OFFSET:
140 PRIV (hdr_data).hdr_c_lnm = _bfd_vms_save_sized_string (vms_rec, PRIV (rec_size - 6));
141 break;
143 case EMH_S_C_SRC + EVAX_OFFSET:
144 PRIV (hdr_data).hdr_c_src = _bfd_vms_save_sized_string (vms_rec, PRIV (rec_size - 6));
145 break;
147 case EMH_S_C_TTL + EVAX_OFFSET:
148 PRIV (hdr_data).hdr_c_ttl = _bfd_vms_save_sized_string (vms_rec, PRIV (rec_size - 6));
149 break;
151 case MHD_S_C_CPR:
152 case MHD_S_C_MTC:
153 case MHD_S_C_GTX:
154 case EMH_S_C_CPR + EVAX_OFFSET:
155 case EMH_S_C_MTC + EVAX_OFFSET:
156 case EMH_S_C_GTX + EVAX_OFFSET:
157 break;
159 default:
160 bfd_set_error (bfd_error_wrong_format);
161 return -1;
164 return 0;
167 /* Output routines. */
169 /* Manufacture a VMS like time on a unix based system.
170 stolen from obj-vms.c. */
172 static unsigned char *
173 get_vms_time_string (void)
175 static unsigned char tbuf[18];
176 #ifndef VMS
177 #include <time.h>
179 char *pnt;
180 time_t timeb;
182 time (& timeb);
183 pnt = ctime (&timeb);
184 pnt[3] = 0;
185 pnt[7] = 0;
186 pnt[10] = 0;
187 pnt[16] = 0;
188 pnt[24] = 0;
189 sprintf ((char *) tbuf, "%2s-%3s-%s %s",
190 pnt + 8, pnt + 4, pnt + 20, pnt + 11);
191 #else
192 #include <starlet.h>
193 struct
195 int Size;
196 unsigned char *Ptr;
197 } Descriptor;
198 Descriptor.Size = 17;
199 Descriptor.Ptr = tbuf;
200 SYS$ASCTIM (0, &Descriptor, 0, 0);
201 #endif /* not VMS */
203 #if VMS_DEBUG
204 vms_debug (6, "vmstimestring:'%s'\n", tbuf);
205 #endif
207 return tbuf;
210 /* Write object header for bfd abfd. */
213 _bfd_vms_write_hdr (bfd *abfd, int objtype)
215 asymbol *symbol;
216 unsigned int symnum;
217 int had_case = 0;
218 int had_file = 0;
219 char version [256];
221 #if VMS_DEBUG
222 vms_debug (2, "vms_write_hdr (%p)\n", abfd);
223 #endif
225 _bfd_vms_output_alignment (abfd, 2);
227 /* MHD. */
228 if (objtype != OBJ_S_C_HDR)
230 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_MHD);
231 _bfd_vms_output_short (abfd, EOBJ_S_C_STRLVL);
232 _bfd_vms_output_long (abfd, 0);
233 _bfd_vms_output_long (abfd, 0);
234 _bfd_vms_output_long (abfd, MAX_OUTREC_SIZE);
237 /* Create module name from filename. */
238 if (bfd_get_filename (abfd) != 0)
240 char *module = vms_get_module_name (bfd_get_filename (abfd), TRUE);
241 _bfd_vms_output_counted (abfd, module);
242 free (module);
244 else
245 _bfd_vms_output_counted (abfd, "NONAME");
247 _bfd_vms_output_counted (abfd, BFD_VERSION_STRING);
248 _bfd_vms_output_dump (abfd, get_vms_time_string (), EMH_DATE_LENGTH);
249 _bfd_vms_output_fill (abfd, 0, EMH_DATE_LENGTH);
250 _bfd_vms_output_flush (abfd);
252 /* LMN. */
253 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_LNM);
254 snprintf (version, sizeof (version), "GAS BFD v%s", BFD_VERSION_STRING);
255 _bfd_vms_output_dump (abfd, (unsigned char *)version, strlen (version));
256 _bfd_vms_output_flush (abfd);
258 /* SRC. */
259 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_SRC);
261 for (symnum = 0; symnum < abfd->symcount; symnum++)
263 symbol = abfd->outsymbols[symnum];
265 if (symbol->flags & BSF_FILE)
267 if (CONST_STRNEQ ((char *)symbol->name, "<CASE:"))
269 PRIV (flag_hash_long_names) = symbol->name[6] - '0';
270 PRIV (flag_show_after_trunc) = symbol->name[7] - '0';
272 if (had_file)
273 break;
274 had_case = 1;
275 continue;
278 _bfd_vms_output_dump (abfd, (unsigned char *) symbol->name,
279 (int) strlen (symbol->name));
280 if (had_case)
281 break;
282 had_file = 1;
286 if (symnum == abfd->symcount)
287 _bfd_vms_output_dump (abfd, (unsigned char *) STRING_COMMA_LEN ("noname"));
289 _bfd_vms_output_flush (abfd);
291 /* TTL. */
292 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_TTL);
293 _bfd_vms_output_dump (abfd, (unsigned char *) STRING_COMMA_LEN ("TTL"));
294 _bfd_vms_output_flush (abfd);
296 /* CPR. */
297 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_CPR);
298 _bfd_vms_output_dump (abfd,
299 (unsigned char *)"GNU BFD ported by Klaus Kämpf 1994-1996",
300 39);
301 _bfd_vms_output_flush (abfd);
303 return 0;
306 /* Process EOM/EEOM record
307 return 0 on success, -1 on error. */
310 _bfd_vms_slurp_eom (bfd *abfd, int objtype)
312 unsigned char *vms_rec;
314 #if VMS_DEBUG
315 vms_debug(2, "EOM/EEOM\n");
316 #endif
318 vms_rec = PRIV (vms_rec);
320 if ((objtype == OBJ_S_C_EOM)
321 || (objtype == OBJ_S_C_EOMW))
324 else
326 PRIV (eom_data).eom_l_total_lps
327 = bfd_getl32 (vms_rec + EEOM_S_L_TOTAL_LPS);
328 PRIV (eom_data).eom_w_comcod = bfd_getl16 (vms_rec + EEOM_S_W_COMCOD);
329 if (PRIV (eom_data).eom_w_comcod > 1)
331 (*_bfd_error_handler) (_("Object module NOT error-free !\n"));
332 bfd_set_error (bfd_error_bad_value);
333 return -1;
335 PRIV (eom_data).eom_has_transfer = FALSE;
336 if (PRIV (rec_size) > 10)
338 PRIV (eom_data).eom_has_transfer = TRUE;
339 PRIV (eom_data).eom_b_tfrflg = vms_rec[EEOM_S_B_TFRFLG];
340 PRIV (eom_data).eom_l_psindx
341 = bfd_getl32 (vms_rec + EEOM_S_L_PSINDX);
342 PRIV (eom_data).eom_l_tfradr
343 = bfd_getl32 (vms_rec + EEOM_S_L_TFRADR);
345 abfd->start_address = PRIV (eom_data).eom_l_tfradr;
348 return 0;
351 /* Write eom record for bfd abfd. */
354 _bfd_vms_write_eom (bfd *abfd, int objtype)
356 #if VMS_DEBUG
357 vms_debug (2, "vms_write_eom (%p, %d)\n", abfd, objtype);
358 #endif
360 _bfd_vms_output_begin (abfd, objtype, -1);
361 _bfd_vms_output_long (abfd, (unsigned long) (PRIV (vms_linkage_index) >> 1));
362 _bfd_vms_output_byte (abfd, 0); /* Completion code. */
363 _bfd_vms_output_byte (abfd, 0); /* Fill byte. */
365 if (bfd_get_start_address (abfd) != (bfd_vma)-1)
367 asection *section;
369 section = bfd_get_section_by_name (abfd, ".link");
370 if (section == 0)
372 bfd_set_error (bfd_error_nonrepresentable_section);
373 return -1;
375 _bfd_vms_output_short (abfd, 0);
376 _bfd_vms_output_long (abfd, (unsigned long) (section->index));
377 _bfd_vms_output_long (abfd,
378 (unsigned long) bfd_get_start_address (abfd));
379 _bfd_vms_output_long (abfd, 0);
382 _bfd_vms_output_end (abfd);
383 return 0;
386 /* Read & process IHD/EIHD record.
387 Return 0 on success, -1 on error */
389 _bfd_vms_slurp_ihd (bfd *abfd, unsigned int *isd_offset,
390 unsigned int *ihs_offset)
392 unsigned int imgtype, size;
393 bfd_vma symvva;
395 #if VMS_DEBUG
396 vms_debug (8, "_bfd_vms_slurp_ihd\n");
397 #endif
399 size = bfd_getl32 (PRIV (vms_rec) + EIHD_S_L_SIZE);
400 imgtype = bfd_getl32 (PRIV (vms_rec) + EIHD_S_L_IMGTYPE);
402 if (imgtype == EIHD_S_K_EXE)
403 abfd->flags |= EXEC_P;
405 symvva = bfd_getl64 (PRIV (vms_rec) + EIHD_S_Q_SYMVVA);
406 if (symvva != 0)
408 PRIV (symvva) = symvva;
409 abfd->flags |= DYNAMIC;
412 *isd_offset = bfd_getl32 (PRIV (vms_rec) + EIHD_S_L_ISDOFF);
413 *ihs_offset = bfd_getl32 (PRIV (vms_rec) + EIHD_S_L_SYMDBGOFF);
415 #if VMS_DEBUG
416 vms_debug (4, "EIHD record size %d imgtype %d symvva 0x%llx isd %d ihs %d\n",
417 size, imgtype, symvva, *isd_offset, *ihs_offset);
418 #endif
420 return 0;
423 /* Read & process ISD/EISD record
424 return 0 on success, -1 on error */
427 _bfd_vms_slurp_isd (bfd *abfd, unsigned int offset)
429 int section_count = 0;
430 unsigned char *p;
431 unsigned int rec_size;
433 #if VMS_DEBUG
434 vms_debug (8, "_bfd_vms_slurp_isd\n");
435 #endif
437 for (p = PRIV (vms_rec) + offset;
438 (rec_size = bfd_getl32 (p + EISD_S_L_EISDSIZE)) != 0;
439 p += rec_size)
441 unsigned long long vaddr = bfd_getl64 (p + EISD_S_Q_VIR_ADDR);
442 unsigned int size = bfd_getl32 (p + EISD_S_L_SECSIZE);
443 unsigned int flags = bfd_getl32 (p + EISD_S_L_FLAGS);
444 unsigned int vbn = bfd_getl32 (p + EISD_S_L_VBN);
445 char *name = NULL;
446 asection *section;
447 flagword bfd_flags;
449 #if VMS_DEBUG
450 vms_debug (4, "EISD record at 0x%x size 0x%x addr 0x%x bfd_flags 0x%x block %d\n",
451 p - PRIV (vms_rec), size, vaddr, flags, vbn);
452 #endif
454 /* VMS combines psects from .obj files into isects in the .exe. This
455 process doesn't preserve enough information to reliably determine
456 what's in each section without examining the data. This is
457 especially true of DWARF debug sections. */
458 bfd_flags = SEC_ALLOC;
460 if (flags & EISD_S_M_EXE)
461 bfd_flags |= SEC_CODE | SEC_HAS_CONTENTS | SEC_LOAD;
463 if (flags & EISD_S_M_NONSHRADR)
464 bfd_flags |= SEC_DATA | SEC_HAS_CONTENTS | SEC_LOAD;
466 if (!(flags & EISD_S_M_WRT))
467 bfd_flags |= SEC_READONLY;
469 if (flags & EISD_S_M_DZRO)
470 bfd_flags |= SEC_DATA;
472 if (flags & EISD_S_M_FIXUPVEC)
473 bfd_flags |= SEC_DATA | SEC_HAS_CONTENTS | SEC_LOAD;
475 if (flags & EISD_S_M_CRF)
476 bfd_flags |= SEC_HAS_CONTENTS | SEC_LOAD;
478 if (flags & EISD_S_M_GBL)
480 name = _bfd_vms_save_counted_string (p + EISD_S_T_GBLNAM);
481 bfd_flags |= SEC_COFF_SHARED_LIBRARY;
482 bfd_flags &= ~(SEC_ALLOC | SEC_LOAD);
484 else
486 name = (char*) bfd_alloc (abfd, 32);
487 sprintf (name, "$LOCAL_%03d$", section_count++);
490 section = bfd_make_section (abfd, name);
492 if (!section)
493 return -1;
495 section->filepos = vbn ? VMS_BLOCK_SIZE * (vbn - 1) : (unsigned int)-1;
496 section->size = size;
497 section->vma = vaddr;
499 if (!bfd_set_section_flags (abfd, section, bfd_flags))
500 return -1;
503 return 0;
506 /* Read & process IHS/EIHS record
507 return 0 on success, -1 on error */
509 _bfd_vms_slurp_ihs (bfd *abfd, unsigned int offset)
511 unsigned char *p = PRIV (vms_rec) + offset;
512 unsigned int gstvbn = bfd_getl32 (p + EIHS_S_L_GSTVBN);
513 unsigned int gstsize ATTRIBUTE_UNUSED = bfd_getl32 (p + EIHS_S_L_GSTSIZE);
514 unsigned int dstvbn = bfd_getl32 (p + EIHS_S_L_DSTVBN);
515 unsigned int dstsize = bfd_getl32 (p + EIHS_S_L_DSTSIZE);
516 unsigned int dmtvbn = bfd_getl32 (p + EIHS_S_L_DMTVBN);
517 unsigned int dmtbytes = bfd_getl32 (p + EIHS_S_L_DMTBYTES);
518 asection *section;
520 #if VMS_DEBUG
521 vms_debug (8, "_bfd_vms_slurp_ihs\n");
522 vms_debug (4, "EIHS record gstvbn %d gstsize %d dstvbn %d dstsize %d dmtvbn %d dmtbytes %d\n",
523 gstvbn, gstsize, dstvbn, dstsize, dmtvbn, dmtbytes);
524 #endif
526 if (dstvbn)
528 flagword bfd_flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
530 section = bfd_make_section (abfd, "$DST$");
531 if (!section)
532 return -1;
534 section->size = dstsize;
535 section->filepos = VMS_BLOCK_SIZE * (dstvbn - 1);
537 if (!bfd_set_section_flags (abfd, section, bfd_flags))
538 return -1;
540 PRIV (dst_section) = section;
541 abfd->flags |= (HAS_DEBUG | HAS_LINENO);
544 if (dmtvbn)
546 flagword bfd_flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
548 section = bfd_make_section (abfd, "$DMT$");
549 if (!section)
550 return -1;
552 section->size = dmtbytes;
553 section->filepos = VMS_BLOCK_SIZE * (dmtvbn - 1);
555 if (!bfd_set_section_flags (abfd, section, bfd_flags))
556 return -1;
559 if (gstvbn)
561 flagword bfd_flags = SEC_HAS_CONTENTS;
563 section = bfd_make_section (abfd, "$GST$");
564 if (!section)
565 return -1;
567 if (bfd_seek (abfd, VMS_BLOCK_SIZE * (gstvbn - 1), SEEK_SET))
569 bfd_set_error (bfd_error_file_truncated);
570 return -1;
573 if (_bfd_vms_slurp_object_records (abfd) != 0)
574 return -1;
576 section->filepos = VMS_BLOCK_SIZE * (gstvbn - 1);
577 section->size = bfd_tell (abfd) - section->filepos;
579 if (!bfd_set_section_flags (abfd, section, bfd_flags))
580 return -1;
582 abfd->flags |= HAS_SYMS;
585 return 0;
588 /* Build a new module for the specified BFD. */
590 static struct module *
591 new_module (bfd *abfd)
593 struct module *module
594 = (struct module *) bfd_zalloc (abfd, sizeof (struct module));
595 module->file_table_count = 16; /* Arbitrary. */
596 module->file_table
597 = bfd_malloc (module->file_table_count * sizeof (struct fileinfo));
598 return module;
601 /* Parse debug info for a module and internalize it. */
603 static void
604 parse_module (bfd *abfd, struct module *module, unsigned char *ptr,
605 unsigned int length)
607 unsigned char *maxptr = ptr + length, *src_ptr, *pcl_ptr;
608 unsigned int prev_linum = 0, curr_linenum = 0;
609 bfd_vma prev_pc = 0, curr_pc = 0;
610 struct srecinfo *curr_srec, *srec;
611 struct lineinfo *curr_line, *line;
612 struct funcinfo *funcinfo;
614 /* Initialize tables with zero element. */
615 curr_srec = (struct srecinfo *) bfd_zalloc (abfd, sizeof (struct srecinfo));
616 module->srec_table = curr_srec;
618 curr_line = (struct lineinfo *) bfd_zalloc (abfd, sizeof (struct lineinfo));
619 module->line_table = curr_line;
621 while (ptr < maxptr)
623 /* The first byte is not counted in the recorded length. */
624 int rec_length = bfd_getl16 (ptr) + 1;
625 int rec_type = bfd_getl16 (ptr + 2);
627 #if VMS_DEBUG
628 _bfd_vms_debug (2, "DST record: length %d, type %d\n",
629 rec_length, rec_type);
630 #endif
632 switch (rec_type)
634 case DST_S_C_MODBEG:
635 module->name
636 = _bfd_vms_save_counted_string (ptr + DST_S_B_MODBEG_NAME);
638 curr_pc = 0;
639 prev_pc = 0;
640 curr_linenum = 0;
641 prev_linum = 0;
643 #if VMS_DEBUG
644 _bfd_vms_debug (3, "module: %s\n", module->name);
645 #endif
646 break;
648 case DST_S_C_MODEND:
649 #if VMS_DEBUG
650 _bfd_vms_debug (3, "end module\n");
651 #endif
652 break;
654 case DST_S_C_RTNBEG:
655 funcinfo = (struct funcinfo *)
656 bfd_zalloc (abfd, sizeof (struct funcinfo));
657 funcinfo->name
658 = _bfd_vms_save_counted_string (ptr + DST_S_B_RTNBEG_NAME);
659 funcinfo->low = bfd_getl32 (ptr + DST_S_L_RTNBEG_ADDRESS);
660 funcinfo->next = module->func_table;
661 module->func_table = funcinfo;
663 #if VMS_DEBUG
664 _bfd_vms_debug (3, "routine: %s at 0x%x\n",
665 funcinfo->name, funcinfo->low);
666 #endif
667 break;
669 case DST_S_C_RTNEND:
670 module->func_table->high = module->func_table->low
671 + bfd_getl32 (ptr + DST_S_L_RTNEND_SIZE) - 1;
673 if (module->func_table->high > module->high)
674 module->high = module->func_table->high;
676 #if VMS_DEBUG
677 _bfd_vms_debug (3, "end routine\n");
678 #endif
679 break;
681 case DST_S_C_PROLOG:
682 #if VMS_DEBUG
683 _bfd_vms_debug (3, "prologue\n");
684 #endif
685 break;
687 case DST_S_C_EPILOG:
688 #if VMS_DEBUG
689 _bfd_vms_debug (3, "epilog\n");
690 #endif
691 break;
693 case DST_S_C_BLKBEG:
694 #if VMS_DEBUG
695 _bfd_vms_debug (3, "block\n");
696 #endif
697 break;
699 case DST_S_C_BLKEND:
700 #if VMS_DEBUG
701 _bfd_vms_debug (3, "end block\n");
702 #endif
703 break;
705 case DST_S_C_SOURCE:
706 src_ptr = ptr + DST_S_C_SOURCE_HEADER_SIZE;
708 #if VMS_DEBUG
709 _bfd_vms_debug (3, "source info\n");
710 #endif
712 while (src_ptr < ptr + rec_length)
714 int cmd = src_ptr[0], cmd_length, data;
716 switch (cmd)
718 case DST_S_C_SRC_DECLFILE:
720 unsigned int fileid
721 = bfd_getl16 (src_ptr + DST_S_W_SRC_DF_FILEID);
722 char *filename
723 = _bfd_vms_save_counted_string (src_ptr
724 + DST_S_B_SRC_DF_FILENAME);
726 while (fileid >= module->file_table_count)
728 module->file_table_count *= 2;
729 module->file_table
730 = bfd_realloc (module->file_table,
731 module->file_table_count
732 * sizeof (struct fileinfo));
735 module->file_table [fileid].name = filename;
736 module->file_table [fileid].srec = 1;
737 cmd_length = src_ptr[DST_S_B_SRC_DF_LENGTH] + 2;
738 #if VMS_DEBUG
739 _bfd_vms_debug (4, "DST_S_C_SRC_DECLFILE: %d, %s\n",
740 fileid,
741 module->file_table [fileid].name);
742 #endif
744 break;
746 case DST_S_C_SRC_DEFLINES_B:
747 /* Perform the association and set the next higher index
748 to the limit. */
749 data = src_ptr[DST_S_B_SRC_UNSBYTE];
750 srec = (struct srecinfo *)
751 bfd_zalloc (abfd, sizeof (struct srecinfo));
752 srec->line = curr_srec->line + data;
753 srec->srec = curr_srec->srec + data;
754 srec->sfile = curr_srec->sfile;
755 curr_srec->next = srec;
756 curr_srec = srec;
757 cmd_length = 2;
758 #if VMS_DEBUG
759 _bfd_vms_debug (4, "DST_S_C_SRC_DEFLINES_B: %d\n", data);
760 #endif
761 break;
763 case DST_S_C_SRC_DEFLINES_W:
764 /* Perform the association and set the next higher index
765 to the limit. */
766 data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
767 srec = (struct srecinfo *)
768 bfd_zalloc (abfd, sizeof (struct srecinfo));
769 srec->line = curr_srec->line + data;
770 srec->srec = curr_srec->srec + data,
771 srec->sfile = curr_srec->sfile;
772 curr_srec->next = srec;
773 curr_srec = srec;
774 cmd_length = 3;
775 #if VMS_DEBUG
776 _bfd_vms_debug (4, "DST_S_C_SRC_DEFLINES_W: %d\n", data);
777 #endif
778 break;
780 case DST_S_C_SRC_INCRLNUM_B:
781 data = src_ptr[DST_S_B_SRC_UNSBYTE];
782 curr_srec->line += data;
783 cmd_length = 2;
784 #if VMS_DEBUG
785 _bfd_vms_debug (4, "DST_S_C_SRC_INCRLNUM_B: %d\n", data);
786 #endif
787 break;
789 case DST_S_C_SRC_SETFILE:
790 data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
791 curr_srec->sfile = data;
792 curr_srec->srec = module->file_table[data].srec;
793 cmd_length = 3;
794 #if VMS_DEBUG
795 _bfd_vms_debug (4, "DST_S_C_SRC_SETFILE: %d\n", data);
796 #endif
797 break;
799 case DST_S_C_SRC_SETLNUM_L:
800 data = bfd_getl32 (src_ptr + DST_S_L_SRC_UNSLONG);
801 curr_srec->line = data;
802 cmd_length = 5;
803 #if VMS_DEBUG
804 _bfd_vms_debug (4, "DST_S_C_SRC_SETLNUM_L: %d\n", data);
805 #endif
806 break;
808 case DST_S_C_SRC_SETLNUM_W:
809 data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
810 curr_srec->line = data;
811 cmd_length = 3;
812 #if VMS_DEBUG
813 _bfd_vms_debug (4, "DST_S_C_SRC_SETLNUM_W: %d\n", data);
814 #endif
815 break;
817 case DST_S_C_SRC_SETREC_L:
818 data = bfd_getl32 (src_ptr + DST_S_L_SRC_UNSLONG);
819 curr_srec->srec = data;
820 module->file_table[curr_srec->sfile].srec = data;
821 cmd_length = 5;
822 #if VMS_DEBUG
823 _bfd_vms_debug (4, "DST_S_C_SRC_SETREC_L: %d\n", data);
824 #endif
825 break;
827 case DST_S_C_SRC_SETREC_W:
828 data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
829 curr_srec->srec = data;
830 module->file_table[curr_srec->sfile].srec = data;
831 cmd_length = 3;
832 #if VMS_DEBUG
833 _bfd_vms_debug (4, "DST_S_C_SRC_SETREC_W: %d\n", data);
834 #endif
835 break;
837 case DST_S_C_SRC_FORMFEED:
838 cmd_length = 1;
839 #if VMS_DEBUG
840 _bfd_vms_debug (4, "DST_S_C_SRC_FORMFEED\n");
841 #endif
842 break;
844 default:
845 (*_bfd_error_handler) (_("unknown source command %d"),
846 cmd);
847 cmd_length = 2;
848 break;
851 src_ptr += cmd_length;
853 break;
855 case DST_S_C_LINE_NUM:
856 pcl_ptr = ptr + DST_S_C_LINE_NUM_HEADER_SIZE;
858 #if VMS_DEBUG
859 _bfd_vms_debug (3, "line info\n");
860 #endif
862 while (pcl_ptr < ptr + rec_length)
864 /* The command byte is signed so we must sign-extend it. */
865 int cmd = ((signed char *)pcl_ptr)[0], cmd_length, data;
867 switch (cmd)
869 case DST_S_C_DELTA_PC_W:
870 data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
871 curr_pc += data;
872 curr_linenum += 1;
873 cmd_length = 3;
874 #if VMS_DEBUG
875 _bfd_vms_debug (4, "DST_S_C_DELTA_PC_W: %d\n", data);
876 #endif
877 break;
879 case DST_S_C_DELTA_PC_L:
880 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
881 curr_pc += data;
882 curr_linenum += 1;
883 cmd_length = 5;
884 #if VMS_DEBUG
885 _bfd_vms_debug (4, "DST_S_C_DELTA_PC_L: %d\n", data);
886 #endif
887 break;
889 case DST_S_C_INCR_LINUM:
890 data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
891 curr_linenum += data;
892 cmd_length = 2;
893 #if VMS_DEBUG
894 _bfd_vms_debug (4, "DST_S_C_INCR_LINUM: %d\n", data);
895 #endif
896 break;
898 case DST_S_C_INCR_LINUM_W:
899 data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
900 curr_linenum += data;
901 cmd_length = 3;
902 #if VMS_DEBUG
903 _bfd_vms_debug (4, "DST_S_C_INCR_LINUM_W: %d\n", data);
904 #endif
905 break;
907 case DST_S_C_INCR_LINUM_L:
908 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
909 curr_linenum += data;
910 cmd_length = 5;
911 #if VMS_DEBUG
912 _bfd_vms_debug (4, "DST_S_C_INCR_LINUM_L: %d\n", data);
913 #endif
914 break;
916 case DST_S_C_SET_LINUM_INCR:
917 (*_bfd_error_handler)
918 (_("DST_S_C_SET_LINUM_INCR not implemented"));
919 cmd_length = 2;
920 break;
922 case DST_S_C_SET_LINUM_INCR_W:
923 (*_bfd_error_handler)
924 (_("DST_S_C_SET_LINUM_INCR_W not implemented"));
925 cmd_length = 3;
926 break;
928 case DST_S_C_RESET_LINUM_INCR:
929 (*_bfd_error_handler)
930 (_("DST_S_C_RESET_LINUM_INCR not implemented"));
931 cmd_length = 1;
932 break;
934 case DST_S_C_BEG_STMT_MODE:
935 (*_bfd_error_handler)
936 (_("DST_S_C_BEG_STMT_MODE not implemented"));
937 cmd_length = 1;
938 break;
940 case DST_S_C_END_STMT_MODE:
941 (*_bfd_error_handler)
942 (_("DST_S_C_END_STMT_MODE not implemented"));
943 cmd_length = 1;
944 break;
946 case DST_S_C_SET_LINUM_B:
947 data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
948 curr_linenum = data;
949 cmd_length = 2;
950 #if VMS_DEBUG
951 _bfd_vms_debug (4, "DST_S_C_SET_LINUM_B: %d\n", data);
952 #endif
953 break;
955 case DST_S_C_SET_LINE_NUM:
956 data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
957 curr_linenum = data;
958 cmd_length = 3;
959 #if VMS_DEBUG
960 _bfd_vms_debug (4, "DST_S_C_SET_LINE_NUM: %d\n", data);
961 #endif
962 break;
964 case DST_S_C_SET_LINUM_L:
965 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
966 curr_linenum = data;
967 cmd_length = 5;
968 #if VMS_DEBUG
969 _bfd_vms_debug (4, "DST_S_C_SET_LINUM_L: %d\n", data);
970 #endif
971 break;
973 case DST_S_C_SET_PC:
974 (*_bfd_error_handler)
975 (_("DST_S_C_SET_PC not implemented"));
976 cmd_length = 2;
977 break;
979 case DST_S_C_SET_PC_W:
980 (*_bfd_error_handler)
981 (_("DST_S_C_SET_PC_W not implemented"));
982 cmd_length = 3;
983 break;
985 case DST_S_C_SET_PC_L:
986 (*_bfd_error_handler)
987 (_("DST_S_C_SET_PC_L not implemented"));
988 cmd_length = 5;
989 break;
991 case DST_S_C_SET_STMTNUM:
992 (*_bfd_error_handler)
993 (_("DST_S_C_SET_STMTNUM not implemented"));
994 cmd_length = 2;
995 break;
997 case DST_S_C_TERM:
998 data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
999 curr_pc += data;
1000 cmd_length = 2;
1001 #if VMS_DEBUG
1002 _bfd_vms_debug (4, "DST_S_C_TERM: %d\n", data);
1003 #endif
1004 break;
1006 case DST_S_C_TERM_W:
1007 data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
1008 curr_pc += data;
1009 cmd_length = 3;
1010 #if VMS_DEBUG
1011 _bfd_vms_debug (4, "DST_S_C_TERM_W: %d\n", data);
1012 #endif
1013 break;
1015 case DST_S_C_TERM_L:
1016 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
1017 curr_pc += data;
1018 cmd_length = 5;
1019 #if VMS_DEBUG
1020 _bfd_vms_debug (4, "DST_S_C_TERM_L: %d\n", data);
1021 #endif
1022 break;
1024 case DST_S_C_SET_ABS_PC:
1025 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
1026 curr_pc = data;
1027 cmd_length = 5;
1028 #if VMS_DEBUG
1029 _bfd_vms_debug (4, "DST_S_C_SET_ABS_PC: 0x%x\n", data);
1030 #endif
1031 break;
1033 default:
1034 if (cmd <= 0)
1036 curr_pc -= cmd;
1037 curr_linenum += 1;
1038 cmd_length = 1;
1039 #if VMS_DEBUG
1040 _bfd_vms_debug (4, "bump pc to 0x%llx and line to %d\n",
1041 curr_pc, curr_linenum);
1042 #endif
1044 else
1046 (*_bfd_error_handler) (_("unknown line command %d"),
1047 cmd);
1048 cmd_length = 2;
1050 break;
1053 if ((curr_linenum != prev_linum && curr_pc != prev_pc)
1054 || cmd <= 0
1055 || cmd == DST_S_C_DELTA_PC_L
1056 || cmd == DST_S_C_DELTA_PC_W)
1058 line = (struct lineinfo *)
1059 bfd_zalloc (abfd, sizeof (struct lineinfo));
1060 line->address = curr_pc;
1061 line->line = curr_linenum;
1063 curr_line->next = line;
1064 curr_line = line;
1066 prev_linum = curr_linenum;
1067 prev_pc = curr_pc;
1068 #if VMS_DEBUG
1069 _bfd_vms_debug (4, "-> correlate pc 0x%llx with line %d\n",
1070 curr_pc, curr_linenum);
1071 #endif
1074 pcl_ptr += cmd_length;
1076 break;
1078 case 0x17: /* Undocumented type used by DEC C to declare equates. */
1079 #if VMS_DEBUG
1080 _bfd_vms_debug (3, "undocumented type 0x17\n");
1081 #endif
1082 break;
1084 default:
1085 #if VMS_DEBUG
1086 _bfd_vms_debug (3, "ignoring record\n");
1087 #endif
1088 break;
1092 ptr += rec_length;
1095 /* Finalize tables with EOL marker. */
1096 srec = (struct srecinfo *) bfd_zalloc (abfd, sizeof (struct srecinfo));
1097 srec->line = (unsigned int) -1;
1098 srec->srec = (unsigned int) -1;
1099 curr_srec->next = srec;
1101 line = (struct lineinfo *) bfd_zalloc (abfd, sizeof (struct lineinfo));
1102 line->line = (unsigned int) -1;
1103 line->address = (bfd_vma) -1;
1104 curr_line->next = line;
1106 /* Advertise that this module has been parsed. This is needed
1107 because parsing can be either performed at module creation
1108 or deferred until debug info is consumed. */
1109 SET_MODULE_PARSED (module);
1112 /* Build the list of modules for the specified BFD. */
1114 static struct module *
1115 build_module_list (bfd *abfd)
1117 struct module *module, *list = NULL;
1118 asection *dmt;
1120 if ((dmt = bfd_get_section_by_name (abfd, "$DMT$")))
1122 /* We have a DMT section so this must be an image. Parse the
1123 section and build the list of modules. This is sufficient
1124 since we can compute the start address and the end address
1125 of every module from the section contents. */
1126 bfd_size_type size = bfd_get_section_size (dmt);
1127 unsigned char *ptr, *end;
1129 ptr = (unsigned char *) bfd_alloc (abfd, size);
1130 if (! ptr)
1131 return NULL;
1133 if (! bfd_get_section_contents (abfd, dmt, ptr, 0, size))
1134 return NULL;
1136 #if VMS_DEBUG
1137 _bfd_vms_debug (2, "DMT\n");
1138 #endif
1140 end = ptr + size;
1142 while (ptr < end)
1144 /* Each header declares a module with its start offset and size
1145 of debug info in the DST section, as well as the count of
1146 program sections (i.e. address spans) it contains. */
1147 int modbeg = bfd_getl32 (ptr + DBG_S_L_DMT_MODBEG);
1148 int msize = bfd_getl32 (ptr + DBG_S_L_DST_SIZE);
1149 int count = bfd_getl16 (ptr + DBG_S_W_DMT_PSECT_COUNT);
1150 ptr += DBG_S_C_DMT_HEADER_SIZE;
1152 #if VMS_DEBUG
1153 _bfd_vms_debug (3, "module: modbeg = %d, size = %d, count = %d\n",
1154 modbeg, msize, count);
1155 #endif
1157 /* We create a 'module' structure for each program section since
1158 we only support contiguous addresses in a 'module' structure.
1159 As a consequence, the actual debug info in the DST section is
1160 shared and can be parsed multiple times; that doesn't seem to
1161 cause problems in practice. */
1162 while (count-- > 0)
1164 int start = bfd_getl32 (ptr + DBG_S_L_DMT_PSECT_START);
1165 int length = bfd_getl32 (ptr + DBG_S_L_DMT_PSECT_LENGTH);
1166 module = new_module (abfd);
1167 module->modbeg = modbeg;
1168 module->size = msize;
1169 module->low = start;
1170 module->high = start + length;
1171 module->next = list;
1172 list = module;
1173 ptr += DBG_S_C_DMT_PSECT_SIZE;
1175 #if VMS_DEBUG
1176 _bfd_vms_debug (4, "section: start = 0x%x, length = %d\n",
1177 start, length);
1178 #endif
1182 else
1184 /* We don't have a DMT section so this must be an object. Parse
1185 the module right now in order to compute its start address and
1186 end address. */
1187 module = new_module (abfd);
1188 parse_module (abfd, module, PRIV (dst_section)->contents,
1189 PRIV (dst_ptr_end) - PRIV (dst_section)->contents);
1190 list = module;
1193 return list;
1196 /* Calculate and return the name of the source file and the line nearest
1197 to the wanted location in the specified module. */
1199 static bfd_boolean
1200 module_find_nearest_line (bfd *abfd, struct module *module, bfd_vma addr,
1201 const char **file, const char **func,
1202 unsigned int *line)
1204 struct funcinfo *funcinfo;
1205 struct lineinfo *lineinfo;
1206 struct srecinfo *srecinfo;
1207 bfd_boolean ret = FALSE;
1209 /* Parse this module if that was not done at module creation. */
1210 if (! IS_MODULE_PARSED (module))
1212 unsigned int size = module->size;
1213 unsigned int modbeg = PRIV (dst_section)->filepos + module->modbeg;
1214 unsigned char *buffer = (unsigned char *) bfd_malloc (module->size);
1216 if (bfd_seek (abfd, modbeg, SEEK_SET) != 0
1217 || bfd_bread (buffer, size, abfd) != size)
1219 bfd_set_error (bfd_error_no_debug_section);
1220 return FALSE;
1223 parse_module (abfd, module, buffer, size);
1224 free (buffer);
1227 /* Find out the function (if any) that contains the address. */
1228 for (funcinfo = module->func_table; funcinfo; funcinfo = funcinfo->next)
1229 if (addr >= funcinfo->low && addr <= funcinfo->high)
1231 *func = funcinfo->name;
1232 ret = TRUE;
1233 break;
1236 /* Find out the source file and the line nearest to the address. */
1237 for (lineinfo = module->line_table; lineinfo; lineinfo = lineinfo->next)
1238 if (lineinfo->next && addr < lineinfo->next->address)
1240 for (srecinfo = module->srec_table; srecinfo; srecinfo = srecinfo->next)
1241 if (srecinfo->next && lineinfo->line < srecinfo->next->line)
1243 if (srecinfo->sfile > 0)
1245 *file = module->file_table[srecinfo->sfile].name;
1246 *line = srecinfo->srec + lineinfo->line - srecinfo->line;
1248 else
1250 *file = module->name;
1251 *line = lineinfo->line;
1253 return TRUE;
1256 break;
1259 return ret;
1262 /* Provided a BFD, a section and an offset into the section, calculate and
1263 return the name of the source file and the line nearest to the wanted
1264 location. */
1266 bfd_boolean
1267 _bfd_vms_find_nearest_dst_line (bfd *abfd, asection *section,
1268 asymbol **symbols ATTRIBUTE_UNUSED,
1269 bfd_vma offset, const char **file,
1270 const char **func, unsigned int *line)
1272 struct module *module;
1274 /* What address are we looking for? */
1275 bfd_vma addr = section->vma + offset;
1277 *file = NULL;
1278 *func = NULL;
1279 *line = 0;
1281 if (PRIV (dst_section) == NULL)
1282 return FALSE;
1284 if (PRIV (modules) == NULL)
1286 PRIV (modules) = build_module_list (abfd);
1287 if (PRIV (modules) == NULL)
1288 return FALSE;
1291 for (module = PRIV (modules); module; module = module->next)
1292 if (addr >= module->low && addr <= module->high)
1293 return module_find_nearest_line (abfd, module, addr, file, func, line);
1295 return FALSE;
1298 /* Process EDBG/ETBT record.
1299 Return 0 on success, -1 on error */
1301 static int
1302 vms_slurp_debug (bfd *abfd)
1304 if (PRIV (dst_section) == NULL)
1306 /* We have no way to find out beforehand how much debug info there
1307 is in an object file, so pick an initial amount and grow it as
1308 needed later. */
1309 flagword flags = SEC_HAS_CONTENTS | SEC_DEBUGGING | SEC_RELOC;
1310 asection *section = bfd_make_section (abfd, "$DST$");
1311 if (!section)
1312 return -1;
1313 section->size = 1024;
1314 if (!bfd_set_section_flags (abfd, section, flags))
1315 return -1;
1316 section->contents = ((unsigned char *)
1317 bfd_zmalloc (section->size));
1318 if (section->contents == NULL)
1319 return -1;
1320 section->filepos = (unsigned int)-1;
1321 PRIV (dst_section) = section;
1324 PRIV (image_section) = PRIV (dst_section);
1325 PRIV (image_ptr) = PRIV (dst_section)->contents;
1327 return _bfd_vms_slurp_tir (abfd, EOBJ_S_C_ETIR);
1330 /* Process DBG/EDBG record.
1331 Return 0 on success, -1 on error. */
1334 _bfd_vms_slurp_dbg (bfd *abfd, int objtype ATTRIBUTE_UNUSED)
1336 #if VMS_DEBUG
1337 _bfd_vms_debug (2, "DBG/EDBG\n");
1338 #endif
1340 abfd->flags |= (HAS_DEBUG | HAS_LINENO);
1342 return vms_slurp_debug (abfd);
1345 /* Process TBT/ETBT record.
1346 Return 0 on success, -1 on error. */
1349 _bfd_vms_slurp_tbt (bfd *abfd, int objtype ATTRIBUTE_UNUSED)
1351 #if VMS_DEBUG
1352 _bfd_vms_debug (2, "TBT/ETBT\n");
1353 #endif
1355 abfd->flags |= HAS_LINENO;
1357 return vms_slurp_debug (abfd);
1360 /* Write DBG/EDBG record. */
1363 _bfd_vms_write_dbg (bfd *abfd ATTRIBUTE_UNUSED, int objtype ATTRIBUTE_UNUSED)
1365 #if VMS_DEBUG
1366 _bfd_vms_debug (2, "vms_write_dbg (%p, %d)\n", abfd, objtype);
1367 #endif
1369 return 0;
1372 /* Write TBT/ETBT record. */
1375 _bfd_vms_write_tbt (bfd *abfd ATTRIBUTE_UNUSED, int objtype ATTRIBUTE_UNUSED)
1377 #if VMS_DEBUG
1378 _bfd_vms_debug (2, "vms_write_tbt (%p, %d)\n", abfd, objtype);
1379 #endif
1381 return 0;