Locale changes from Bruno Haible <haible@clisp.cons.org>.
[deliverable/binutils-gdb.git] / bfd / vms-hdr.c
CommitLineData
252b5132
RH
1/* vms-hdr.c -- BFD back-end for VMS/VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
524f76c9 3 Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
252b5132
RH
4
5 HDR record handling functions
6 EMH record handling functions
7 and
8 EOM record handling functions
9 EEOM record handling functions
10
11 Written by Klaus K"ampf (kkaempf@rmi.de)
12
13This program is free software; you can redistribute it and/or modify
14it under the terms of the GNU General Public License as published by
15the Free Software Foundation; either version 2 of the License, or
16(at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program; if not, write to the Free Software
25Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26
252b5132
RH
27#include "bfd.h"
28#include "sysdep.h"
29#include "bfdlink.h"
3882b010 30#include "safe-ctype.h"
252b5132
RH
31#include "libbfd.h"
32
33#include "vms.h"
34
23ccc829
NC
35#ifdef HAVE_ALLOCA_H
36#include <alloca.h>
37#endif
524f76c9
AJ
38
39static unsigned char *get_vms_time_string PARAMS ((void));
40
41
252b5132
RH
42/*---------------------------------------------------------------------------*/
43
252b5132
RH
44/* Read & process emh record
45 return 0 on success, -1 on error */
46
47int
48_bfd_vms_slurp_hdr (abfd, objtype)
49 bfd *abfd;
50 int objtype;
51{
52 unsigned char *ptr;
53 unsigned char *vms_rec;
54 int subtype;
55
56 vms_rec = PRIV(vms_rec);
57
58#if VMS_DEBUG
59 vms_debug(2, "HDR/EMH\n");
60#endif
61
62 switch (objtype)
63 {
64 case OBJ_S_C_HDR:
65 subtype = vms_rec[1];
66 break;
67 case EOBJ_S_C_EMH:
68 subtype = bfd_getl16 (vms_rec + 4) + EVAX_OFFSET;
69 break;
70 default:
71 subtype = -1;
72 }
73
74#if VMS_DEBUG
75 vms_debug(3, "subtype %d\n", subtype);
76#endif
77
78 switch (subtype)
79 {
80
81 case MHD_S_C_MHD:
82 /*
83 * module header
84 */
85 PRIV(hdr_data).hdr_b_strlvl = vms_rec[2];
86 PRIV(hdr_data).hdr_l_recsiz = bfd_getl16 (vms_rec + 3);
87 PRIV(hdr_data).hdr_t_name = _bfd_vms_save_counted_string (vms_rec + 5);
88 ptr = vms_rec + 5 + vms_rec[5] + 1;
89 PRIV(hdr_data).hdr_t_version = _bfd_vms_save_counted_string (ptr);
90 ptr += *ptr + 1;
91 PRIV(hdr_data).hdr_t_date = _bfd_vms_save_sized_string (ptr, 17);
92
93 break;
94
95 case MHD_S_C_LNM:
96 /*
97 *
98 */
99 PRIV(hdr_data).hdr_c_lnm = _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-2));
100 break;
101
102 case MHD_S_C_SRC:
103 /*
104 *
105 */
106 PRIV(hdr_data).hdr_c_src = _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-2));
107 break;
108
109 case MHD_S_C_TTL:
110 /*
111 *
112 */
113 PRIV(hdr_data).hdr_c_ttl = _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-2));
114 break;
115
116 case MHD_S_C_CPR:
117 /*
118 *
119 */
120 break;
121
122 case MHD_S_C_MTC:
123 /*
124 *
125 */
126 break;
127
128 case MHD_S_C_GTX:
129 /*
130 *
131 */
132 break;
133
134 case EMH_S_C_MHD + EVAX_OFFSET:
135 /*
136 * module header
137 */
138 PRIV(hdr_data).hdr_b_strlvl = vms_rec[6];
139 PRIV(hdr_data).hdr_l_arch1 = bfd_getl32 (vms_rec + 8);
140 PRIV(hdr_data).hdr_l_arch2 = bfd_getl32 (vms_rec + 12);
141 PRIV(hdr_data).hdr_l_recsiz = bfd_getl32 (vms_rec + 16);
142 PRIV(hdr_data).hdr_t_name =
143 _bfd_vms_save_counted_string (vms_rec + 20);
144 ptr = vms_rec + 20 + vms_rec[20] + 1;
145 PRIV(hdr_data).hdr_t_version =
146 _bfd_vms_save_counted_string (ptr);
147 ptr += *ptr + 1;
148 PRIV(hdr_data).hdr_t_date =
149 _bfd_vms_save_sized_string (ptr, 17);
150
151 break;
152
153 case EMH_S_C_LNM + EVAX_OFFSET:
154 /*
155 *
156 */
157 PRIV(hdr_data).hdr_c_lnm =
158 _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-6));
159 break;
160
161 case EMH_S_C_SRC + EVAX_OFFSET:
162 /*
163 *
164 */
165 PRIV(hdr_data).hdr_c_src =
166 _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-6));
167 break;
168
169 case EMH_S_C_TTL + EVAX_OFFSET:
170 /*
171 *
172 */
173 PRIV(hdr_data).hdr_c_ttl =
174 _bfd_vms_save_sized_string (vms_rec, PRIV(rec_length-6));
175 break;
176
177 case EMH_S_C_CPR + EVAX_OFFSET:
178 /*
179 *
180 */
181 break;
182
183 case EMH_S_C_MTC + EVAX_OFFSET:
184 /*
185 *
186 */
187 break;
188
189 case EMH_S_C_GTX + EVAX_OFFSET:
190 /*
191 *
192 */
193 break;
194
195 default:
196 bfd_set_error (bfd_error_wrong_format);
197 return -1;
198
199 } /* switch */
200
201 return 0;
202}
203
252b5132
RH
204/*-----------------------------------------------------------------------------*/
205/* Output routines. */
206
252b5132
RH
207/* Manufacure a VMS like time on a unix based system.
208 stolen from obj-vms.c */
209
210static unsigned char *
211get_vms_time_string ()
212{
213 static unsigned char tbuf[18];
214#ifndef VMS
215#include <time.h>
216
217 char *pnt;
218 time_t timeb;
219 time (&timeb);
220 pnt = ctime (&timeb);
221 pnt[3] = 0;
222 pnt[7] = 0;
223 pnt[10] = 0;
224 pnt[16] = 0;
225 pnt[24] = 0;
226 sprintf (tbuf, "%2s-%3s-%s %s", pnt + 8, pnt + 4, pnt + 20, pnt + 11);
227#else
228#include <starlet.h>
229 struct
230 {
231 int Size;
232 unsigned char *Ptr;
233 } Descriptor;
234 Descriptor.Size = 17;
235 Descriptor.Ptr = tbuf;
236 SYS$ASCTIM (0, &Descriptor, 0, 0);
237#endif /* not VMS */
238
239#if VMS_DEBUG
240 vms_debug (6, "vmstimestring:'%s'\n", tbuf);
241#endif
242
243 return tbuf;
244}
245
252b5132
RH
246/* write object header for bfd abfd */
247
248int
249_bfd_vms_write_hdr (abfd, objtype)
250 bfd *abfd;
251 int objtype;
252{
253 asymbol *symbol;
5f771d47 254 unsigned int symnum;
252b5132
RH
255 int had_case = 0;
256 int had_file = 0;
257
252b5132
RH
258#if VMS_DEBUG
259 vms_debug (2, "vms_write_hdr (%p)\n", abfd);
260#endif
261
262 _bfd_vms_output_alignment (abfd, 2);
263
264 /* MHD */
265
266 if (objtype == OBJ_S_C_HDR)
267 {
268 }
269 else
270 {
271 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_MHD);
272 _bfd_vms_output_short (abfd, EOBJ_S_C_STRLVL);
273 _bfd_vms_output_long (abfd, 0);
274 _bfd_vms_output_long (abfd, 0);
275 _bfd_vms_output_long (abfd, MAX_OUTREC_SIZE);
276 }
277
278 if (bfd_get_filename (abfd) != 0)
279 {
280 /* strip path and suffix information */
281
282 char *fname, *fout, *fptr;
283
284 fptr = bfd_get_filename (abfd);
285 fname = (char *) alloca (strlen (fptr) + 1);
286 strcpy (fname, fptr);
287 fout = strrchr (fname, ']');
288 if (fout == 0)
289 fout = strchr (fname, ':');
290 if (fout != 0)
291 fout++;
292 else
293 fout = fname;
294
295 /* strip .obj suffix */
296
297 fptr = strrchr (fname, '.');
298 if ((fptr != 0)
299 && (strcasecmp (fptr, ".OBJ") == 0))
300 *fptr = 0;
301
302 fptr = fout;
303 while (*fptr != 0)
304 {
3882b010 305 *fptr = TOUPPER (*fptr);
252b5132
RH
306 fptr++;
307 if ((*fptr == ';')
308 || ((fptr - fout) > 31))
309 *fptr = 0;
310 }
311 _bfd_vms_output_counted (abfd, fout);
312 }
313 else
314 _bfd_vms_output_counted (abfd, "NONAME");
315
316 _bfd_vms_output_counted (abfd, BFD_VERSION);
317 _bfd_vms_output_dump (abfd, get_vms_time_string (), 17);
318 _bfd_vms_output_fill (abfd, 0, 17);
319 _bfd_vms_output_flush (abfd);
320
321 /* LMN */
322
323 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_LNM);
324 _bfd_vms_output_dump (abfd, (unsigned char *)"GAS proGIS", 10);
325 _bfd_vms_output_flush (abfd);
326
327 /* SRC */
328
329 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_SRC);
330
331 for (symnum = 0; symnum < abfd->symcount; symnum++)
332 {
333 symbol = abfd->outsymbols[symnum];
334
335 if (symbol->flags & BSF_FILE)
336 {
252b5132
RH
337 if (strncmp ((char *)symbol->name, "<CASE:", 6) == 0)
338 {
339 PRIV(flag_hash_long_names) = symbol->name[6] - '0';
340 PRIV(flag_show_after_trunc) = symbol->name[7] - '0';
341
342 if (had_file)
343 break;
344 had_case = 1;
345 continue;
346 }
347
dc810e39
AM
348 _bfd_vms_output_dump (abfd, (unsigned char *) symbol->name,
349 (int) strlen (symbol->name));
252b5132
RH
350 if (had_case)
351 break;
352 had_file = 1;
353 }
354 }
355
356 if (symnum == abfd->symcount)
357 _bfd_vms_output_dump (abfd, (unsigned char *)"noname", 6);
358
359 _bfd_vms_output_flush (abfd);
360
361 /* TTL */
362
363 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_TTL);
364 _bfd_vms_output_dump (abfd, (unsigned char *)"TTL", 3);
365 _bfd_vms_output_flush (abfd);
366
367 /* CPR */
368
369 _bfd_vms_output_begin (abfd, EOBJ_S_C_EMH, EMH_S_C_CPR);
370 _bfd_vms_output_dump (abfd,