Add -Wshadow to the gcc command line options used when compiling the binutils.
[binutils.git] / bfd / vms-hdr.c
blob49e680962a73aef0713708e3fdc09fa11aa80fe6
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 /* Strip path and suffix information. */
241 char *fname, *fout, *fptr;
243 fptr = bfd_get_filename (abfd);
244 fname = strdup (fptr);
246 /* Strip VMS path. */
247 fout = strrchr (fname, ']');
248 if (fout == NULL)
249 fout = strchr (fname, ':');
250 if (fout != NULL)
251 fout++;
252 else
253 fout = fname;
255 /* Strip UNIX path. */
256 fptr = strrchr (fout, '/');
257 if (fptr != NULL)
258 fout = fptr + 1;
260 /* Strip suffix. */
261 fptr = strrchr (fout, '.');
262 if (fptr != 0)
263 *fptr = 0;
265 /* Convert to upper case and truncate at 31 characters.
266 (VMS object file format restricts module name length to 31). */
267 fptr = fout;
268 while (*fptr != 0)
270 *fptr = TOUPPER (*fptr);
271 fptr++;
272 if (*fptr == ';' || (fptr - fout) >= 31)
273 *fptr = 0;
275 _bfd_vms_output_counted (abfd, fout);
276 free (fname);
278 else
279 _bfd_vms_output_counted (abfd, "NONAME");
281 _bfd_vms_output_counted (abfd, BFD_VERSION_STRING);
282 _bfd_vms_output_dump (abfd, get_vms_time_string (), EMH_DATE_LENGTH);
283 _bfd_vms_output_fill (abfd, 0, EMH_DATE_LENGTH);
284 _bfd_vms_output_flush (abfd);
286 /* LMN. */
287 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_LNM);
288 snprintf (version, sizeof (version), "GAS BFD v%s", BFD_VERSION_STRING);
289 _bfd_vms_output_dump (abfd, (unsigned char *)version, strlen (version));
290 _bfd_vms_output_flush (abfd);
292 /* SRC. */
293 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_SRC);
295 for (symnum = 0; symnum < abfd->symcount; symnum++)
297 symbol = abfd->outsymbols[symnum];
299 if (symbol->flags & BSF_FILE)
301 if (CONST_STRNEQ ((char *)symbol->name, "<CASE:"))
303 PRIV (flag_hash_long_names) = symbol->name[6] - '0';
304 PRIV (flag_show_after_trunc) = symbol->name[7] - '0';
306 if (had_file)
307 break;
308 had_case = 1;
309 continue;
312 _bfd_vms_output_dump (abfd, (unsigned char *) symbol->name,
313 (int) strlen (symbol->name));
314 if (had_case)
315 break;
316 had_file = 1;
320 if (symnum == abfd->symcount)
321 _bfd_vms_output_dump (abfd, (unsigned char *) STRING_COMMA_LEN ("noname"));
323 _bfd_vms_output_flush (abfd);
325 /* TTL. */
326 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_TTL);
327 _bfd_vms_output_dump (abfd, (unsigned char *) STRING_COMMA_LEN ("TTL"));
328 _bfd_vms_output_flush (abfd);
330 /* CPR. */
331 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_CPR);
332 _bfd_vms_output_dump (abfd,
333 (unsigned char *)"GNU BFD ported by Klaus Kämpf 1994-1996",
334 39);
335 _bfd_vms_output_flush (abfd);
337 return 0;
340 /* Process EOM/EEOM record
341 return 0 on success, -1 on error. */
344 _bfd_vms_slurp_eom (bfd *abfd, int objtype)
346 unsigned char *vms_rec;
348 #if VMS_DEBUG
349 vms_debug(2, "EOM/EEOM\n");
350 #endif
352 vms_rec = PRIV (vms_rec);
354 if ((objtype == OBJ_S_C_EOM)
355 || (objtype == OBJ_S_C_EOMW))
358 else
360 PRIV (eom_data).eom_l_total_lps
361 = bfd_getl32 (vms_rec + EEOM_S_L_TOTAL_LPS);
362 PRIV (eom_data).eom_w_comcod = bfd_getl16 (vms_rec + EEOM_S_W_COMCOD);
363 if (PRIV (eom_data).eom_w_comcod > 1)
365 (*_bfd_error_handler) (_("Object module NOT error-free !\n"));
366 bfd_set_error (bfd_error_bad_value);
367 return -1;
369 PRIV (eom_data).eom_has_transfer = FALSE;
370 if (PRIV (rec_size) > 10)
372 PRIV (eom_data).eom_has_transfer = TRUE;
373 PRIV (eom_data).eom_b_tfrflg = vms_rec[EEOM_S_B_TFRFLG];
374 PRIV (eom_data).eom_l_psindx
375 = bfd_getl32 (vms_rec + EEOM_S_L_PSINDX);
376 PRIV (eom_data).eom_l_tfradr
377 = bfd_getl32 (vms_rec + EEOM_S_L_TFRADR);
379 abfd->start_address = PRIV (eom_data).eom_l_tfradr;
382 return 0;
385 /* Write eom record for bfd abfd. */
388 _bfd_vms_write_eom (bfd *abfd, int objtype)
390 #if VMS_DEBUG
391 vms_debug (2, "vms_write_eom (%p, %d)\n", abfd, objtype);
392 #endif
394 _bfd_vms_output_begin (abfd, objtype, -1);
395 _bfd_vms_output_long (abfd, (unsigned long) (PRIV (vms_linkage_index) >> 1));
396 _bfd_vms_output_byte (abfd, 0); /* Completion code. */
397 _bfd_vms_output_byte (abfd, 0); /* Fill byte. */
399 if (bfd_get_start_address (abfd) != (bfd_vma)-1)
401 asection *section;
403 section = bfd_get_section_by_name (abfd, ".link");
404 if (section == 0)
406 bfd_set_error (bfd_error_nonrepresentable_section);
407 return -1;
409 _bfd_vms_output_short (abfd, 0);
410 _bfd_vms_output_long (abfd, (unsigned long) (section->index));
411 _bfd_vms_output_long (abfd,
412 (unsigned long) bfd_get_start_address (abfd));
413 _bfd_vms_output_long (abfd, 0);
416 _bfd_vms_output_end (abfd);
417 return 0;
420 /* Read & process IHD/EIHD record.
421 Return 0 on success, -1 on error */
423 _bfd_vms_slurp_ihd (bfd *abfd, unsigned int *isd_offset,
424 unsigned int *ihs_offset)
426 unsigned int imgtype, size;
427 bfd_vma symvva;
429 #if VMS_DEBUG
430 vms_debug (8, "_bfd_vms_slurp_ihd\n");
431 #endif
433 size = bfd_getl32 (PRIV (vms_rec) + EIHD_S_L_SIZE);
434 imgtype = bfd_getl32 (PRIV (vms_rec) + EIHD_S_L_IMGTYPE);
436 if (imgtype == EIHD_S_K_EXE)
437 abfd->flags |= EXEC_P;
439 symvva = bfd_getl64 (PRIV (vms_rec) + EIHD_S_Q_SYMVVA);
440 if (symvva != 0)
442 PRIV (symvva) = symvva;
443 abfd->flags |= DYNAMIC;
446 *isd_offset = bfd_getl32 (PRIV (vms_rec) + EIHD_S_L_ISDOFF);
447 *ihs_offset = bfd_getl32 (PRIV (vms_rec) + EIHD_S_L_SYMDBGOFF);
449 #if VMS_DEBUG
450 vms_debug (4, "EIHD record size %d imgtype %d symvva 0x%llx isd %d ihs %d\n",
451 size, imgtype, symvva, *isd_offset, *ihs_offset);
452 #endif
454 return 0;
457 /* Read & process ISD/EISD record
458 return 0 on success, -1 on error */
461 _bfd_vms_slurp_isd (bfd *abfd, unsigned int offset)
463 int section_count = 0;
464 unsigned char *p;
465 unsigned int rec_size;
467 #if VMS_DEBUG
468 vms_debug (8, "_bfd_vms_slurp_isd\n");
469 #endif
471 for (p = PRIV (vms_rec) + offset;
472 (rec_size = bfd_getl32 (p + EISD_S_L_EISDSIZE)) != 0;
473 p += rec_size)
475 unsigned long long vaddr = bfd_getl64 (p + EISD_S_Q_VIR_ADDR);
476 unsigned int size = bfd_getl32 (p + EISD_S_L_SECSIZE);
477 unsigned int flags = bfd_getl32 (p + EISD_S_L_FLAGS);
478 unsigned int vbn = bfd_getl32 (p + EISD_S_L_VBN);
479 char *name = NULL;
480 asection *section;
481 flagword bfd_flags;
483 #if VMS_DEBUG
484 vms_debug (4, "EISD record at 0x%x size 0x%x addr 0x%x bfd_flags 0x%x block %d\n",
485 p - PRIV (vms_rec), size, vaddr, flags, vbn);
486 #endif
488 /* VMS combines psects from .obj files into isects in the .exe. This
489 process doesn't preserve enough information to reliably determine
490 what's in each section without examining the data. This is
491 especially true of DWARF debug sections. */
492 bfd_flags = SEC_ALLOC;
494 if (flags & EISD_S_M_EXE)
495 bfd_flags |= SEC_CODE | SEC_HAS_CONTENTS | SEC_LOAD;
497 if (flags & EISD_S_M_NONSHRADR)
498 bfd_flags |= SEC_DATA | SEC_HAS_CONTENTS | SEC_LOAD;
500 if (!(flags & EISD_S_M_WRT))
501 bfd_flags |= SEC_READONLY;
503 if (flags & EISD_S_M_DZRO)
504 bfd_flags |= SEC_DATA;
506 if (flags & EISD_S_M_FIXUPVEC)
507 bfd_flags |= SEC_DATA | SEC_HAS_CONTENTS | SEC_LOAD;
509 if (flags & EISD_S_M_CRF)
510 bfd_flags |= SEC_HAS_CONTENTS | SEC_LOAD;
512 if (flags & EISD_S_M_GBL)
514 name = _bfd_vms_save_counted_string (p + EISD_S_T_GBLNAM);
515 bfd_flags |= SEC_COFF_SHARED_LIBRARY;
516 bfd_flags &= ~(SEC_ALLOC | SEC_LOAD);
518 else
520 name = (char*) bfd_alloc (abfd, 32);
521 sprintf (name, "$LOCAL_%03d$", section_count++);
524 section = bfd_make_section (abfd, name);
526 if (!section)
527 return -1;
529 section->filepos = vbn ? VMS_BLOCK_SIZE * (vbn - 1) : (unsigned int)-1;
530 section->size = size;
531 section->vma = vaddr;
533 if (!bfd_set_section_flags (abfd, section, bfd_flags))
534 return -1;
537 return 0;
540 /* Read & process IHS/EIHS record
541 return 0 on success, -1 on error */
543 _bfd_vms_slurp_ihs (bfd *abfd, unsigned int offset)
545 unsigned char *p = PRIV (vms_rec) + offset;
546 unsigned int gstvbn = bfd_getl32 (p + EIHS_S_L_GSTVBN);
547 unsigned int gstsize ATTRIBUTE_UNUSED = bfd_getl32 (p + EIHS_S_L_GSTSIZE);
548 unsigned int dstvbn = bfd_getl32 (p + EIHS_S_L_DSTVBN);
549 unsigned int dstsize = bfd_getl32 (p + EIHS_S_L_DSTSIZE);
550 unsigned int dmtvbn = bfd_getl32 (p + EIHS_S_L_DMTVBN);
551 unsigned int dmtbytes = bfd_getl32 (p + EIHS_S_L_DMTBYTES);
552 asection *section;
554 #if VMS_DEBUG
555 vms_debug (8, "_bfd_vms_slurp_ihs\n");
556 vms_debug (4, "EIHS record gstvbn %d gstsize %d dstvbn %d dstsize %d dmtvbn %d dmtbytes %d\n",
557 gstvbn, gstsize, dstvbn, dstsize, dmtvbn, dmtbytes);
558 #endif
560 if (dstvbn)
562 flagword bfd_flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
564 section = bfd_make_section (abfd, "$DST$");
565 if (!section)
566 return -1;
568 section->size = dstsize;
569 section->filepos = VMS_BLOCK_SIZE * (dstvbn - 1);
571 if (!bfd_set_section_flags (abfd, section, bfd_flags))
572 return -1;
574 PRIV (dst_section) = section;
575 abfd->flags |= (HAS_DEBUG | HAS_LINENO);
578 if (dmtvbn)
580 flagword bfd_flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
582 section = bfd_make_section (abfd, "$DMT$");
583 if (!section)
584 return -1;
586 section->size = dmtbytes;
587 section->filepos = VMS_BLOCK_SIZE * (dmtvbn - 1);
589 if (!bfd_set_section_flags (abfd, section, bfd_flags))
590 return -1;
593 if (gstvbn)
595 flagword bfd_flags = SEC_HAS_CONTENTS;
597 section = bfd_make_section (abfd, "$GST$");
598 if (!section)
599 return -1;
601 if (bfd_seek (abfd, VMS_BLOCK_SIZE * (gstvbn - 1), SEEK_SET))
603 bfd_set_error (bfd_error_file_truncated);
604 return -1;
607 if (_bfd_vms_slurp_object_records (abfd) != 0)
608 return -1;
610 section->filepos = VMS_BLOCK_SIZE * (gstvbn - 1);
611 section->size = bfd_tell (abfd) - section->filepos;
613 if (!bfd_set_section_flags (abfd, section, bfd_flags))
614 return -1;
616 abfd->flags |= HAS_SYMS;
619 return 0;
622 /* Build a new module for the specified BFD. */
624 static struct module *
625 new_module (bfd *abfd)
627 struct module *module
628 = (struct module *) bfd_zalloc (abfd, sizeof (struct module));
629 module->file_table_count = 16; /* Arbitrary. */
630 module->file_table
631 = bfd_malloc (module->file_table_count * sizeof (struct fileinfo));
632 return module;
635 /* Parse debug info for a module and internalize it. */
637 static void
638 parse_module (bfd *abfd, struct module *module, unsigned char *ptr,
639 unsigned int length)
641 unsigned char *maxptr = ptr + length, *src_ptr, *pcl_ptr;
642 unsigned int prev_linum = 0, curr_linenum = 0;
643 bfd_vma prev_pc = 0, curr_pc = 0;
644 struct srecinfo *curr_srec, *srec;
645 struct lineinfo *curr_line, *line;
646 struct funcinfo *funcinfo;
648 /* Initialize tables with zero element. */
649 curr_srec = (struct srecinfo *) bfd_zalloc (abfd, sizeof (struct srecinfo));
650 module->srec_table = curr_srec;
652 curr_line = (struct lineinfo *) bfd_zalloc (abfd, sizeof (struct lineinfo));
653 module->line_table = curr_line;
655 while (ptr < maxptr)
657 /* The first byte is not counted in the recorded length. */
658 int rec_length = bfd_getl16 (ptr) + 1;
659 int rec_type = bfd_getl16 (ptr + 2);
661 #if VMS_DEBUG
662 _bfd_vms_debug (2, "DST record: length %d, type %d\n",
663 rec_length, rec_type);
664 #endif
666 switch (rec_type)
668 case DST_S_C_MODBEG:
669 module->name
670 = _bfd_vms_save_counted_string (ptr + DST_S_B_MODBEG_NAME);
672 curr_pc = 0;
673 prev_pc = 0;
674 curr_linenum = 0;
675 prev_linum = 0;
677 #if VMS_DEBUG
678 _bfd_vms_debug (3, "module: %s\n", module->name);
679 #endif
680 break;
682 case DST_S_C_MODEND:
683 #if VMS_DEBUG
684 _bfd_vms_debug (3, "end module\n");
685 #endif
686 break;
688 case DST_S_C_RTNBEG:
689 funcinfo = (struct funcinfo *)
690 bfd_zalloc (abfd, sizeof (struct funcinfo));
691 funcinfo->name
692 = _bfd_vms_save_counted_string (ptr + DST_S_B_RTNBEG_NAME);
693 funcinfo->low = bfd_getl32 (ptr + DST_S_L_RTNBEG_ADDRESS);
694 funcinfo->next = module->func_table;
695 module->func_table = funcinfo;
697 #if VMS_DEBUG
698 _bfd_vms_debug (3, "routine: %s at 0x%x\n",
699 funcinfo->name, funcinfo->low);
700 #endif
701 break;
703 case DST_S_C_RTNEND:
704 module->func_table->high = module->func_table->low
705 + bfd_getl32 (ptr + DST_S_L_RTNEND_SIZE) - 1;
707 if (module->func_table->high > module->high)
708 module->high = module->func_table->high;
710 #if VMS_DEBUG
711 _bfd_vms_debug (3, "end routine\n");
712 #endif
713 break;
715 case DST_S_C_PROLOG:
716 #if VMS_DEBUG
717 _bfd_vms_debug (3, "prologue\n");
718 #endif
719 break;
721 case DST_S_C_EPILOG:
722 #if VMS_DEBUG
723 _bfd_vms_debug (3, "epilog\n");
724 #endif
725 break;
727 case DST_S_C_BLKBEG:
728 #if VMS_DEBUG
729 _bfd_vms_debug (3, "block\n");
730 #endif
731 break;
733 case DST_S_C_BLKEND:
734 #if VMS_DEBUG
735 _bfd_vms_debug (3, "end block\n");
736 #endif
737 break;
739 case DST_S_C_SOURCE:
740 src_ptr = ptr + DST_S_C_SOURCE_HEADER_SIZE;
742 #if VMS_DEBUG
743 _bfd_vms_debug (3, "source info\n");
744 #endif
746 while (src_ptr < ptr + rec_length)
748 int cmd = src_ptr[0], cmd_length, data;
750 switch (cmd)
752 case DST_S_C_SRC_DECLFILE:
754 unsigned int fileid
755 = bfd_getl16 (src_ptr + DST_S_W_SRC_DF_FILEID);
756 char *filename
757 = _bfd_vms_save_counted_string (src_ptr
758 + DST_S_B_SRC_DF_FILENAME);
760 while (fileid >= module->file_table_count)
762 module->file_table_count *= 2;
763 module->file_table
764 = bfd_realloc (module->file_table,
765 module->file_table_count
766 * sizeof (struct fileinfo));
769 module->file_table [fileid].name = filename;
770 module->file_table [fileid].srec = 1;
771 cmd_length = src_ptr[DST_S_B_SRC_DF_LENGTH] + 2;
772 #if VMS_DEBUG
773 _bfd_vms_debug (4, "DST_S_C_SRC_DECLFILE: %d, %s\n",
774 fileid,
775 module->file_table [fileid].name);
776 #endif
778 break;
780 case DST_S_C_SRC_DEFLINES_B:
781 /* Perform the association and set the next higher index
782 to the limit. */
783 data = src_ptr[DST_S_B_SRC_UNSBYTE];
784 srec = (struct srecinfo *)
785 bfd_zalloc (abfd, sizeof (struct srecinfo));
786 srec->line = curr_srec->line + data;
787 srec->srec = curr_srec->srec + data;
788 srec->sfile = curr_srec->sfile;
789 curr_srec->next = srec;
790 curr_srec = srec;
791 cmd_length = 2;
792 #if VMS_DEBUG
793 _bfd_vms_debug (4, "DST_S_C_SRC_DEFLINES_B: %d\n", data);
794 #endif
795 break;
797 case DST_S_C_SRC_DEFLINES_W:
798 /* Perform the association and set the next higher index
799 to the limit. */
800 data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
801 srec = (struct srecinfo *)
802 bfd_zalloc (abfd, sizeof (struct srecinfo));
803 srec->line = curr_srec->line + data;
804 srec->srec = curr_srec->srec + data,
805 srec->sfile = curr_srec->sfile;
806 curr_srec->next = srec;
807 curr_srec = srec;
808 cmd_length = 3;
809 #if VMS_DEBUG
810 _bfd_vms_debug (4, "DST_S_C_SRC_DEFLINES_W: %d\n", data);
811 #endif
812 break;
814 case DST_S_C_SRC_INCRLNUM_B:
815 data = src_ptr[DST_S_B_SRC_UNSBYTE];
816 curr_srec->line += data;
817 cmd_length = 2;
818 #if VMS_DEBUG
819 _bfd_vms_debug (4, "DST_S_C_SRC_INCRLNUM_B: %d\n", data);
820 #endif
821 break;
823 case DST_S_C_SRC_SETFILE:
824 data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
825 curr_srec->sfile = data;
826 curr_srec->srec = module->file_table[data].srec;
827 cmd_length = 3;
828 #if VMS_DEBUG
829 _bfd_vms_debug (4, "DST_S_C_SRC_SETFILE: %d\n", data);
830 #endif
831 break;
833 case DST_S_C_SRC_SETLNUM_L:
834 data = bfd_getl32 (src_ptr + DST_S_L_SRC_UNSLONG);
835 curr_srec->line = data;
836 cmd_length = 5;
837 #if VMS_DEBUG
838 _bfd_vms_debug (4, "DST_S_C_SRC_SETLNUM_L: %d\n", data);
839 #endif
840 break;
842 case DST_S_C_SRC_SETLNUM_W:
843 data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
844 curr_srec->line = data;
845 cmd_length = 3;
846 #if VMS_DEBUG
847 _bfd_vms_debug (4, "DST_S_C_SRC_SETLNUM_W: %d\n", data);
848 #endif
849 break;
851 case DST_S_C_SRC_SETREC_L:
852 data = bfd_getl32 (src_ptr + DST_S_L_SRC_UNSLONG);
853 curr_srec->srec = data;
854 module->file_table[curr_srec->sfile].srec = data;
855 cmd_length = 5;
856 #if VMS_DEBUG
857 _bfd_vms_debug (4, "DST_S_C_SRC_SETREC_L: %d\n", data);
858 #endif
859 break;
861 case DST_S_C_SRC_SETREC_W:
862 data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
863 curr_srec->srec = data;
864 module->file_table[curr_srec->sfile].srec = data;
865 cmd_length = 3;
866 #if VMS_DEBUG
867 _bfd_vms_debug (4, "DST_S_C_SRC_SETREC_W: %d\n", data);
868 #endif
869 break;
871 case DST_S_C_SRC_FORMFEED:
872 cmd_length = 1;
873 #if VMS_DEBUG
874 _bfd_vms_debug (4, "DST_S_C_SRC_FORMFEED\n");
875 #endif
876 break;
878 default:
879 (*_bfd_error_handler) (_("unknown source command %d"),
880 cmd);
881 cmd_length = 2;
882 break;
885 src_ptr += cmd_length;
887 break;
889 case DST_S_C_LINE_NUM:
890 pcl_ptr = ptr + DST_S_C_LINE_NUM_HEADER_SIZE;
892 #if VMS_DEBUG
893 _bfd_vms_debug (3, "line info\n");
894 #endif
896 while (pcl_ptr < ptr + rec_length)
898 /* The command byte is signed so we must sign-extend it. */
899 int cmd = ((signed char *)pcl_ptr)[0], cmd_length, data;
901 switch (cmd)
903 case DST_S_C_DELTA_PC_W:
904 data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
905 curr_pc += data;
906 curr_linenum += 1;
907 cmd_length = 3;
908 #if VMS_DEBUG
909 _bfd_vms_debug (4, "DST_S_C_DELTA_PC_W: %d\n", data);
910 #endif
911 break;
913 case DST_S_C_DELTA_PC_L:
914 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
915 curr_pc += data;
916 curr_linenum += 1;
917 cmd_length = 5;
918 #if VMS_DEBUG
919 _bfd_vms_debug (4, "DST_S_C_DELTA_PC_L: %d\n", data);
920 #endif
921 break;
923 case DST_S_C_INCR_LINUM:
924 data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
925 curr_linenum += data;
926 cmd_length = 2;
927 #if VMS_DEBUG
928 _bfd_vms_debug (4, "DST_S_C_INCR_LINUM: %d\n", data);
929 #endif
930 break;
932 case DST_S_C_INCR_LINUM_W:
933 data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
934 curr_linenum += data;
935 cmd_length = 3;
936 #if VMS_DEBUG
937 _bfd_vms_debug (4, "DST_S_C_INCR_LINUM_W: %d\n", data);
938 #endif
939 break;
941 case DST_S_C_INCR_LINUM_L:
942 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
943 curr_linenum += data;
944 cmd_length = 5;
945 #if VMS_DEBUG
946 _bfd_vms_debug (4, "DST_S_C_INCR_LINUM_L: %d\n", data);
947 #endif
948 break;
950 case DST_S_C_SET_LINUM_INCR:
951 (*_bfd_error_handler)
952 (_("DST_S_C_SET_LINUM_INCR not implemented"));
953 cmd_length = 2;
954 break;
956 case DST_S_C_SET_LINUM_INCR_W:
957 (*_bfd_error_handler)
958 (_("DST_S_C_SET_LINUM_INCR_W not implemented"));
959 cmd_length = 3;
960 break;
962 case DST_S_C_RESET_LINUM_INCR:
963 (*_bfd_error_handler)
964 (_("DST_S_C_RESET_LINUM_INCR not implemented"));
965 cmd_length = 1;
966 break;
968 case DST_S_C_BEG_STMT_MODE:
969 (*_bfd_error_handler)
970 (_("DST_S_C_BEG_STMT_MODE not implemented"));
971 cmd_length = 1;
972 break;
974 case DST_S_C_END_STMT_MODE:
975 (*_bfd_error_handler)
976 (_("DST_S_C_END_STMT_MODE not implemented"));
977 cmd_length = 1;
978 break;
980 case DST_S_C_SET_LINUM_B:
981 data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
982 curr_linenum = data;
983 cmd_length = 2;
984 #if VMS_DEBUG
985 _bfd_vms_debug (4, "DST_S_C_SET_LINUM_B: %d\n", data);
986 #endif
987 break;
989 case DST_S_C_SET_LINE_NUM:
990 data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
991 curr_linenum = data;
992 cmd_length = 3;
993 #if VMS_DEBUG
994 _bfd_vms_debug (4, "DST_S_C_SET_LINE_NUM: %d\n", data);
995 #endif
996 break;
998 case DST_S_C_SET_LINUM_L:
999 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
1000 curr_linenum = data;
1001 cmd_length = 5;
1002 #if VMS_DEBUG
1003 _bfd_vms_debug (4, "DST_S_C_SET_LINUM_L: %d\n", data);
1004 #endif
1005 break;
1007 case DST_S_C_SET_PC:
1008 (*_bfd_error_handler)
1009 (_("DST_S_C_SET_PC not implemented"));
1010 cmd_length = 2;
1011 break;
1013 case DST_S_C_SET_PC_W:
1014 (*_bfd_error_handler)
1015 (_("DST_S_C_SET_PC_W not implemented"));
1016 cmd_length = 3;
1017 break;
1019 case DST_S_C_SET_PC_L:
1020 (*_bfd_error_handler)
1021 (_("DST_S_C_SET_PC_L not implemented"));
1022 cmd_length = 5;
1023 break;
1025 case DST_S_C_SET_STMTNUM:
1026 (*_bfd_error_handler)
1027 (_("DST_S_C_SET_STMTNUM not implemented"));
1028 cmd_length = 2;
1029 break;
1031 case DST_S_C_TERM:
1032 data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
1033 curr_pc += data;
1034 cmd_length = 2;
1035 #if VMS_DEBUG
1036 _bfd_vms_debug (4, "DST_S_C_TERM: %d\n", data);
1037 #endif
1038 break;
1040 case DST_S_C_TERM_W:
1041 data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
1042 curr_pc += data;
1043 cmd_length = 3;
1044 #if VMS_DEBUG
1045 _bfd_vms_debug (4, "DST_S_C_TERM_W: %d\n", data);
1046 #endif
1047 break;
1049 case DST_S_C_TERM_L:
1050 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
1051 curr_pc += data;
1052 cmd_length = 5;
1053 #if VMS_DEBUG
1054 _bfd_vms_debug (4, "DST_S_C_TERM_L: %d\n", data);
1055 #endif
1056 break;
1058 case DST_S_C_SET_ABS_PC:
1059 data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
1060 curr_pc = data;
1061 cmd_length = 5;
1062 #if VMS_DEBUG
1063 _bfd_vms_debug (4, "DST_S_C_SET_ABS_PC: 0x%x\n", data);
1064 #endif
1065 break;
1067 default:
1068 if (cmd <= 0)
1070 curr_pc -= cmd;
1071 curr_linenum += 1;
1072 cmd_length = 1;
1073 #if VMS_DEBUG
1074 _bfd_vms_debug (4, "bump pc to 0x%llx and line to %d\n",
1075 curr_pc, curr_linenum);
1076 #endif
1078 else
1080 (*_bfd_error_handler) (_("unknown line command %d"),
1081 cmd);
1082 cmd_length = 2;
1084 break;
1087 if ((curr_linenum != prev_linum && curr_pc != prev_pc)
1088 || cmd <= 0
1089 || cmd == DST_S_C_DELTA_PC_L
1090 || cmd == DST_S_C_DELTA_PC_W)
1092 line = (struct lineinfo *)
1093 bfd_zalloc (abfd, sizeof (struct lineinfo));
1094 line->address = curr_pc;
1095 line->line = curr_linenum;
1097 curr_line->next = line;
1098 curr_line = line;
1100 prev_linum = curr_linenum;
1101 prev_pc = curr_pc;
1102 #if VMS_DEBUG
1103 _bfd_vms_debug (4, "-> correlate pc 0x%llx with line %d\n",
1104 curr_pc, curr_linenum);
1105 #endif
1108 pcl_ptr += cmd_length;
1110 break;
1112 case 0x17: /* Undocumented type used by DEC C to declare equates. */
1113 #if VMS_DEBUG
1114 _bfd_vms_debug (3, "undocumented type 0x17\n");
1115 #endif
1116 break;
1118 default:
1119 #if VMS_DEBUG
1120 _bfd_vms_debug (3, "ignoring record\n");
1121 #endif
1122 break;
1126 ptr += rec_length;
1129 /* Finalize tables with EOL marker. */
1130 srec = (struct srecinfo *) bfd_zalloc (abfd, sizeof (struct srecinfo));
1131 srec->line = (unsigned int) -1;
1132 srec->srec = (unsigned int) -1;
1133 curr_srec->next = srec;
1135 line = (struct lineinfo *) bfd_zalloc (abfd, sizeof (struct lineinfo));
1136 line->line = (unsigned int) -1;
1137 line->address = (bfd_vma) -1;
1138 curr_line->next = line;
1140 /* Advertise that this module has been parsed. This is needed
1141 because parsing can be either performed at module creation
1142 or deferred until debug info is consumed. */
1143 SET_MODULE_PARSED (module);
1146 /* Build the list of modules for the specified BFD. */
1148 static struct module *
1149 build_module_list (bfd *abfd)
1151 struct module *module, *list = NULL;
1152 asection *dmt;
1154 if ((dmt = bfd_get_section_by_name (abfd, "$DMT$")))
1156 /* We have a DMT section so this must be an image. Parse the
1157 section and build the list of modules. This is sufficient
1158 since we can compute the start address and the end address
1159 of every module from the section contents. */
1160 bfd_size_type size = bfd_get_section_size (dmt);
1161 unsigned char *ptr, *end;
1163 ptr = (unsigned char *) bfd_alloc (abfd, size);
1164 if (! ptr)
1165 return NULL;
1167 if (! bfd_get_section_contents (abfd, dmt, ptr, 0, size))
1168 return NULL;
1170 #if VMS_DEBUG
1171 _bfd_vms_debug (2, "DMT\n");
1172 #endif
1174 end = ptr + size;
1176 while (ptr < end)
1178 /* Each header declares a module with its start offset and size
1179 of debug info in the DST section, as well as the count of
1180 program sections (i.e. address spans) it contains. */
1181 int modbeg = bfd_getl32 (ptr + DBG_S_L_DMT_MODBEG);
1182 int msize = bfd_getl32 (ptr + DBG_S_L_DST_SIZE);
1183 int count = bfd_getl16 (ptr + DBG_S_W_DMT_PSECT_COUNT);
1184 ptr += DBG_S_C_DMT_HEADER_SIZE;
1186 #if VMS_DEBUG
1187 _bfd_vms_debug (3, "module: modbeg = %d, size = %d, count = %d\n",
1188 modbeg, msize, count);
1189 #endif
1191 /* We create a 'module' structure for each program section since
1192 we only support contiguous addresses in a 'module' structure.
1193 As a consequence, the actual debug info in the DST section is
1194 shared and can be parsed multiple times; that doesn't seem to
1195 cause problems in practice. */
1196 while (count-- > 0)
1198 int start = bfd_getl32 (ptr + DBG_S_L_DMT_PSECT_START);
1199 int length = bfd_getl32 (ptr + DBG_S_L_DMT_PSECT_LENGTH);
1200 module = new_module (abfd);
1201 module->modbeg = modbeg;
1202 module->size = msize;
1203 module->low = start;
1204 module->high = start + length;
1205 module->next = list;
1206 list = module;
1207 ptr += DBG_S_C_DMT_PSECT_SIZE;
1209 #if VMS_DEBUG
1210 _bfd_vms_debug (4, "section: start = 0x%x, length = %d\n",
1211 start, length);
1212 #endif
1216 else
1218 /* We don't have a DMT section so this must be an object. Parse
1219 the module right now in order to compute its start address and
1220 end address. */
1221 module = new_module (abfd);
1222 parse_module (abfd, module, PRIV (dst_section)->contents,
1223 PRIV (dst_ptr_end) - PRIV (dst_section)->contents);
1224 list = module;
1227 return list;
1230 /* Calculate and return the name of the source file and the line nearest
1231 to the wanted location in the specified module. */
1233 static bfd_boolean
1234 module_find_nearest_line (bfd *abfd, struct module *module, bfd_vma addr,
1235 const char **file, const char **func,
1236 unsigned int *line)
1238 struct funcinfo *funcinfo;
1239 struct lineinfo *lineinfo;
1240 struct srecinfo *srecinfo;
1241 bfd_boolean ret = FALSE;
1243 /* Parse this module if that was not done at module creation. */
1244 if (! IS_MODULE_PARSED (module))
1246 unsigned int size = module->size;
1247 unsigned int modbeg = PRIV (dst_section)->filepos + module->modbeg;
1248 unsigned char *buffer = (unsigned char *) bfd_malloc (module->size);
1250 if (bfd_seek (abfd, modbeg, SEEK_SET) != 0
1251 || bfd_bread (buffer, size, abfd) != size)
1253 bfd_set_error (bfd_error_no_debug_section);
1254 return FALSE;
1257 parse_module (abfd, module, buffer, size);
1258 free (buffer);
1261 /* Find out the function (if any) that contains the address. */
1262 for (funcinfo = module->func_table; funcinfo; funcinfo = funcinfo->next)
1263 if (addr >= funcinfo->low && addr <= funcinfo->high)
1265 *func = funcinfo->name;
1266 ret = TRUE;
1267 break;
1270 /* Find out the source file and the line nearest to the address. */
1271 for (lineinfo = module->line_table; lineinfo; lineinfo = lineinfo->next)
1272 if (lineinfo->next && addr < lineinfo->next->address)
1274 for (srecinfo = module->srec_table; srecinfo; srecinfo = srecinfo->next)
1275 if (srecinfo->next && lineinfo->line < srecinfo->next->line)
1277 if (srecinfo->sfile > 0)
1279 *file = module->file_table[srecinfo->sfile].name;
1280 *line = srecinfo->srec + lineinfo->line - srecinfo->line;
1282 else
1284 *file = module->name;
1285 *line = lineinfo->line;
1287 return TRUE;
1290 break;
1293 return ret;
1296 /* Provided a BFD, a section and an offset into the section, calculate and
1297 return the name of the source file and the line nearest to the wanted
1298 location. */
1300 bfd_boolean
1301 _bfd_vms_find_nearest_dst_line (bfd *abfd, asection *section,
1302 asymbol **symbols ATTRIBUTE_UNUSED,
1303 bfd_vma offset, const char **file,
1304 const char **func, unsigned int *line)
1306 struct module *module;
1308 /* What address are we looking for? */
1309 bfd_vma addr = section->vma + offset;
1311 *file = NULL;
1312 *func = NULL;
1313 *line = 0;
1315 if (PRIV (dst_section) == NULL)
1316 return FALSE;
1318 if (PRIV (modules) == NULL)
1320 PRIV (modules) = build_module_list (abfd);
1321 if (PRIV (modules) == NULL)
1322 return FALSE;
1325 for (module = PRIV (modules); module; module = module->next)
1326 if (addr >= module->low && addr <= module->high)
1327 return module_find_nearest_line (abfd, module, addr, file, func, line);
1329 return FALSE;
1332 /* Process EDBG/ETBT record.
1333 Return 0 on success, -1 on error */
1335 static int
1336 vms_slurp_debug (bfd *abfd)
1338 if (PRIV (dst_section) == NULL)
1340 /* We have no way to find out beforehand how much debug info there
1341 is in an object file, so pick an initial amount and grow it as
1342 needed later. */
1343 flagword flags = SEC_HAS_CONTENTS | SEC_DEBUGGING | SEC_RELOC;
1344 asection *section = bfd_make_section (abfd, "$DST$");
1345 if (!section)
1346 return -1;
1347 section->size = 1024;
1348 if (!bfd_set_section_flags (abfd, section, flags))
1349 return -1;
1350 section->contents = ((unsigned char *)
1351 bfd_zmalloc (section->size));
1352 if (section->contents == NULL)
1353 return -1;
1354 section->filepos = (unsigned int)-1;
1355 PRIV (dst_section) = section;
1358 PRIV (image_section) = PRIV (dst_section);
1359 PRIV (image_ptr) = PRIV (dst_section)->contents;
1361 return _bfd_vms_slurp_tir (abfd, EOBJ_S_C_ETIR);
1364 /* Process DBG/EDBG record.
1365 Return 0 on success, -1 on error. */
1368 _bfd_vms_slurp_dbg (bfd *abfd, int objtype ATTRIBUTE_UNUSED)
1370 #if VMS_DEBUG
1371 _bfd_vms_debug (2, "DBG/EDBG\n");
1372 #endif
1374 abfd->flags |= (HAS_DEBUG | HAS_LINENO);
1376 return vms_slurp_debug (abfd);
1379 /* Process TBT/ETBT record.
1380 Return 0 on success, -1 on error. */
1383 _bfd_vms_slurp_tbt (bfd *abfd, int objtype ATTRIBUTE_UNUSED)
1385 #if VMS_DEBUG
1386 _bfd_vms_debug (2, "TBT/ETBT\n");
1387 #endif
1389 abfd->flags |= HAS_LINENO;
1391 return vms_slurp_debug (abfd);
1394 /* Write DBG/EDBG record. */
1397 _bfd_vms_write_dbg (bfd *abfd ATTRIBUTE_UNUSED, int objtype ATTRIBUTE_UNUSED)
1399 #if VMS_DEBUG
1400 _bfd_vms_debug (2, "vms_write_dbg (%p, objtype)\n", abfd, objtype);
1401 #endif
1403 return 0;
1406 /* Write TBT/ETBT record. */
1409 _bfd_vms_write_tbt (bfd *abfd ATTRIBUTE_UNUSED, int objtype ATTRIBUTE_UNUSED)
1411 #if VMS_DEBUG
1412 _bfd_vms_debug (2, "vms_write_tbt (%p, %d)\n", abfd, objtype);
1413 #endif
1415 return 0;