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