aboutsummaryrefslogtreecommitdiff
path: root/bfd/vms-hdr.c
blob: e4ee546dd0e754fdaac2910363bb64e29deddbb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* vms-hdr.c -- BFD back-end for VMS/VAX (openVMS/VAX) and
   EVAX (openVMS/Alpha) files.
   Copyright 1996, 1997, 1998, 1999 Free Software Foundation, Inc.

   HDR record handling functions
   EMH record handling functions
   and
   EOM record handling functions
   EEOM record handling functions

   Written by Klaus K"ampf (kkaempf@rmi.de)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <ctype.h>

#include "bfd.h"
#include "sysdep.h"
#include "bfdlink.h"
#include "libbfd.h"

#include "vms.h"

/*---------------------------------------------------------------------------*/


/* Read & process emh record
   return 0 on success, -1 on error  */

int
_bfd_vms_slurp_hdr (abfd, objtype)
     bfd *abfd;
     int objtype;
{
  unsigned char *ptr;
  unsigned char *vms_rec;
  int subtype;

  vms_rec = PRIV(vms_rec);

#if VMS_DEBUG
  vms_debug(2, "HDR/EMH\n");
#endif

  switch (objtype)
    {
      case OBJ_S_C_HDR:
	subtype = vms_rec[1];
	break;
      case EOBJ_S_C_EMH:
	subtype = bfd_getl16 (vms_rec + 4) + EVAX_OFFSET;
	break;
      default:
	subtype = -1;
    }

#if VMS_DEBUG
  vms_debug(3, "subtype %d\n", subtype);
#endif

  switch (subtype)
    {

      case MHD_S_C_MHD:
	/*
	 * module header
	 */
	PRIV(hdr_data).hdr_b_strlvl = vms_rec[2];
	PRIV(hdr_data).hdr_l_recsiz = bfd_getl16 (vms_rec + 3);
	PRIV(hdr_data).hdr_t_name = _bfd_vms_save_counted_string (vms_rec + 5);
	ptr = vms_rec + 5 + vms_rec[5] + 1;
	PRIV(hdr_data).hdr_t_version = _bfd_vms_save_counted_string (ptr);
	ptr += *ptr + 1;
	PRIV(hdr_data).hdr_t_date = _bfd_vms_save_sized_string (ptr, 17);

      break;

      case MHD_S_C_LNM:
	/*
	 *
	 */
	PRIV(hdr_data).hdr_c_lnm = _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-2));
      break;

      case MHD_S_C_SRC:
	/*
	 *
	 */
	PRIV(hdr_data).hdr_c_src = _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-2));
      break;

      case MHD_S_C_TTL:
	/*
	 *
	 */
	PRIV(hdr_data).hdr_c_ttl = _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-2));
      break;

      case MHD_S_C_CPR:
	/*
	 *
	 */
      break;

      case MHD_S_C_MTC:
	/*
	 *
	 */
      break;

      case MHD_S_C_GTX:
	/*
	 *
	 */
      break;

      case EMH_S_C_MHD + EVAX_OFFSET:
	/*
	 * module header
	 */
	PRIV(hdr_data).hdr_b_strlvl = vms_rec[6];
	PRIV(hdr_data).hdr_l_arch1 = bfd_getl32 (vms_rec + 8);
	PRIV(hdr_data).hdr_l_arch2 = bfd_getl32 (vms_rec + 12);
	PRIV(hdr_data).hdr_l_recsiz = bfd_getl32 (vms_rec + 16);
	PRIV(hdr_data).hdr_t_name =
	  _bfd_vms_save_counted_string (vms_rec + 20);
	ptr = vms_rec + 20 + vms_rec[20] + 1;
	PRIV(hdr_data).hdr_t_version =
	  _bfd_vms_save_counted_string (ptr);
	ptr += *ptr + 1;
	PRIV(hdr_data).hdr_t_date =
	  _bfd_vms_save_sized_string (ptr, 17);

      break;

      case EMH_S_C_LNM + EVAX_OFFSET:
	/*
	 *
	 */
	PRIV(hdr_data).hdr_c_lnm =
	  _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-6));
      break;

      case EMH_S_C_SRC + EVAX_OFFSET:
	/*
	 *
	 */
	PRIV(hdr_data).hdr_c_src =
	  _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-6));
      break;

      case EMH_S_C_TTL + EVAX_OFFSET:
	/*
	 *
	 */
	PRIV(hdr_data).hdr_c_ttl =
	  _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-6));
      break;

      case EMH_S_C_CPR + EVAX_OFFSET:
	/*
	 *
	 */
      break;

      case EMH_S_C_MTC + EVAX_OFFSET:
	/*
	 *
	 */
      break;

      case EMH_S_C_GTX + EVAX_OFFSET:
	/*
	 *
	 */
      break;

      default:
	bfd_set_error (bfd_error_wrong_format);
      return -1;

    } /* switch */

  return 0;
}


/*-----------------------------------------------------------------------------*/
/* Output routines.  */


/* Manufacure a VMS like time on a unix based system.
   stolen from obj-vms.c  */

static unsigned char *
get_vms_time_string ()
{
  static unsigned char tbuf[18];
#ifndef VMS
#include <time.h>

  char *pnt;
  time_t timeb;
  time (&timeb);
  pnt = ctime (&timeb);
  pnt[3] = 0;
  pnt[7] = 0;
  pnt[10] = 0;
  pnt[16] = 0;
  pnt[24] = 0;
  sprintf (tbuf, "%2s-%3s-%s %s", pnt + 8, pnt + 4, pnt + 20, pnt + 11);
#else
#include <starlet.h>
  struct
  {
    int Size;
    unsigned char *Ptr;
  } Descriptor;
  Descriptor.Size = 17;
  Descriptor.Ptr = tbuf;
  SYS$ASCTIM (0, &Descriptor, 0, 0);
#endif /* not VMS */

#if VMS_DEBUG
  vms_debug (6, "vmstimestring:'%s'\n", tbuf);
#endif

  return tbuf;
}


/* write object header for bfd abfd  */

int
_bfd_vms_write_hdr (abfd, objtype)
     bfd *abfd;
     int objtype;
{
  asymbol *symbol;
  int symnum;
  int had_case = 0;
  int had_file = 0;


#if VMS_DEBUG
  vms_debug (2, "vms_write_hdr (%p)\n", abfd);
#endif

  _bfd_vms_output_alignment (abfd, 2);

  /* MHD */

  if (objtype == OBJ_S_C_HDR)
    {
    }
  else
    {
      _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_MHD);
      _bfd_vms_output_short (abfd, EOBJ_S_C_STRLVL);
      _bfd_vms_output_long (abfd, 0);
      _bfd_vms_output_long (abfd, 0);
      _bfd_vms_output_long (abfd, MAX_OUTREC_SIZE);
    }

  if (bfd_get_filename (abfd) != 0)
    {
      /* strip path and suffix information */

      char *fname, *fout, *fptr;

      fptr = bfd_get_filename (abfd);
      fname = (char *) alloca (strlen (fptr) + 1);
      strcpy (fname, fptr);
      fout = strrchr (fname, ']');
      if (fout == 0)
	fout = strchr (fname, ':');
      if (fout != 0)
	fout++;
      else
	fout = fname;

      /* strip .obj suffix  */

      fptr = strrchr (fname, '.');
      if ((fptr != 0)
	  && (strcasecmp (fptr, ".OBJ") == 0))
	*fptr = 0;

      fptr = fout;
      while (*fptr != 0)
	{
	  if (islower (*fptr))
	    *fptr = toupper (*fptr);
	  fptr++;
	  if ((*fptr == ';')
	     || ((fptr - fout) > 31))
	    *fptr = 0;
	}
      _bfd_vms_output_counted (abfd, fout);
    }
  else
    _bfd_vms_output_counted (abfd, "NONAME");

  _bfd_vms_output_counted (abfd, BFD_VERSION);
  _bfd_vms_output_dump (abfd, get_vms_time_string (), 17);
  _bfd_vms_output_fill (abfd, 0, 17);
  _bfd_vms_output_flush (abfd);

  /* LMN */

  _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_LNM);
  _bfd_vms_output_dump (abfd, (unsigned char *)"GAS proGIS", 10);
  _bfd_vms_output_flush (abfd);

  /* SRC */

  _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_SRC);

  for (symnum = 0; symnum < abfd->symcount; symnum++)
    {
      symbol = abfd->outsymbols[symnum];

      if (symbol->flags & BSF_FILE)
	{
	  if (strncmp ((char *)symbol->name, "<CASE:", 6) == 0)
	    {
	      PRIV(flag_hash_long_names) = symbol->name[6] - '0';
	      PRIV(flag_show_after_trunc) = symbol->name[7] - '0';

	      if (had_file)
		break;
	      had_case = 1;
	      continue;
	    }

	  _bfd_vms_output_dump (abfd, (unsigned char *)symbol->name, strlen (symbol->name));
	  if (had_case)
	    break;
	  had_file = 1;
	}
    }

  if (symnum == abfd->symcount)
    _bfd_vms_output_dump (abfd, (unsigned char *)"noname", 6);

  _bfd_vms_output_flush (abfd);

  /* TTL */

  _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_TTL);
  _bfd_vms_output_dump (abfd, (unsigned char *)"TTL", 3);
  _bfd_vms_output_flush (abfd);

  /* CPR */

  _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_CPR);
  _bfd_vms_output_dump (abfd,
			 (unsigned char *)"GNU BFD ported by Klaus Kämpf 1994-1996",
			 39);
  _bfd_vms_output_flush (abfd);

  return 0;
}

/*-----------------------------------------------------------------------------*/

/* Process EOM/EEOM record
   return 0 on success, -1 on error  */

int
_bfd_vms_slurp_eom (abfd, objtype)
     bfd *abfd;
     int objtype;
{
  unsigned char *vms_rec;

#if VMS_DEBUG
  vms_debug(2, "EOM/EEOM\n");
#endif

  vms_rec = PRIV(vms_rec);

  if ((objtype == OBJ_S_C_EOM) 
     || (objtype == OBJ_S_C_EOMW))
    {
    }
  else
    {
      PRIV(eom_data).eom_l_total_lps = bfd_getl32 (vms_rec + 4);
      PRIV(eom_data).eom_b_comcod = *(vms_rec + 8);
      if (PRIV(eom_data).eom_b_comcod > 1)
	{
	  (*_bfd_error_handler) (_("Object module NOT error-free !\n"));
	  bfd_set_error (bfd_error_bad_value);
	  return -1;
	}
      PRIV(eom_data).eom_has_transfer = false;
      if (PRIV(rec_size) > 10)
	{
	   PRIV(eom_data).eom_has_transfer = true;
	   PRIV(eom_data).eom_b_tfrflg = *(vms_rec + 9);
	   PRIV(eom_data).eom_l_psindx = bfd_getl32 (vms_rec + 12);
	   PRIV(eom_data).eom_l_tfradr = bfd_getl32 (vms_rec + 16);

	   abfd->start_address = PRIV(eom_data).eom_l_tfradr;
	}
    }
  return 0;
}


/* Write eom record for bfd abfd  */

int
_bfd_vms_write_eom (abfd, objtype)
     bfd *abfd;
     int objtype;
{
#if VMS_DEBUG
  vms_debug (2, "vms_write_eom (%p, %d)\n", abfd, objtype);
#endif

  _bfd_vms_output_begin (abfd, objtype, -1);
  _bfd_vms_output_long (abfd, (unsigned long)(PRIV(vms_linkage_index) >> 1));
  _bfd_vms_output_byte (abfd, 0);	/* completion code */
  _bfd_vms_output_byte (abfd, 0);	/* fill byte */

  if (bfd_get_start_address (abfd) != (bfd_vma)-1)
    {
      asection *section;

      section = bfd_get_section_by_name (abfd, ".link");
      if (section == 0)
	{
	  bfd_set_error (bfd_error_nonrepresentable_section);
	  return -1;
	}
      _bfd_vms_output_short (abfd, 0);
      _bfd_vms_output_long (abfd, (unsigned long)(section->index));
      _bfd_vms_output_long (abfd,
			     (unsigned long) bfd_get_start_address (abfd));
      _bfd_vms_output_long (abfd, 0);
    }

  _bfd_vms_output_end (abfd);
  return 0;
}