Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    Copyright (c) 1993 Open Software Foundation, Inc.
3
 
4
 
5
    All Rights Reserved
6
 
7
 
8
    Permission to use, copy, modify, and distribute this software
9
    and its documentation for any purpose and without fee is hereby
10
    granted, provided that the above copyright notice appears in all
11
    copies and that both the copyright notice and this permission
12
    notice appear in supporting documentation.
13
 
14
 
15
    OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING
16
    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17
    PARTICULAR PURPOSE.
18
 
19
 
20
    IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
21
    CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
22
    LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
23
    NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24
    WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25
*/
26
 
27
/*
28
    		 Crown Copyright (c) 1997
29
 
30
    This TenDRA(r) Computer Program is subject to Copyright
31
    owned by the United Kingdom Secretary of State for Defence
32
    acting through the Defence Evaluation and Research Agency
33
    (DERA).  It is made available to Recipients with a
34
    royalty-free licence for its use, reproduction, transfer
35
    to other parties and amendment for any purpose not excluding
36
    product development provided that any such use et cetera
37
    shall be deemed to be acceptance of the following conditions:-
38
 
39
        (1) Its Recipients shall ensure that this Notice is
40
        reproduced upon any copies or amended versions of it;
41
 
42
        (2) Any amended version of it shall be clearly marked to
43
        show both the nature of and the organisation responsible
44
        for the relevant amendment or amendments;
45
 
46
        (3) Its onward transfer from a recipient to another
47
        party shall be deemed to be that party's acceptance of
48
        these conditions;
49
 
50
        (4) DERA gives no warranty or assurance as to its
51
        quality or suitability for any purpose and DERA accepts
52
        no liability whatsoever in relation to any use to which
53
        it may be put.
54
*/
55
 
56
#include <stdio.h>
57
#include <stdlib.h>
58
#include <filehdr.h>
59
#include <syms.h>
60
#include "ossg.h"
61
 
62
#define IS_BAD_MAGIC_NUMBER(x) ((x!=0x1df)?1:0)
63
void output_assembler_file PROTO_S ((void));
64
void scan_object_file PROTO_S ((void));
65
void found_one PROTO_S ((char *));
66
void fail PROTO_S ((char *));
67
int main PROTO_S ((int,char **));
68
FILE *open_file PROTO_S ((char *,char *));
69
 
70
char **names;
71
long names_size = 0;
72
long names_found = 0;
73
char *output_file;
74
char *input_file;
75
 
76
int main PROTO_N ((argc,argv)) PROTO_T (int argc X char ** argv)
77
{
78
  int report_version = 0;
79
  if (!strcmp(argv[1],"-V") || !strcmp(argv[1],"-v"))
80
  {
81
    printf("dyninit for powertrans version 1.1\n");
82
    report_version = 1; 
83
  }
84
 
85
  if ((argc - report_version)!=3)
86
  {
87
    if(report_version) exit(0);
88
    fail("Wrong number of arguments: <object_file> <assembler_file>");
89
  }
90
  input_file = argv[1+report_version];
91
  output_file = argv[2+report_version];
92
  scan_object_file();
93
  return 0;
94
}
95
 
96
FILE *open_file PROTO_N ((filename,option)) PROTO_T (char * filename X char *option)
97
{
98
  FILE * ret_fp;
99
  if(ret_fp = fopen(filename,option)){
100
    return ret_fp;
101
  }
102
  else{
103
    fprintf(stderr,"Cannot open file <%s>\n",filename);
104
    exit(1);
105
  }
106
}
107
 
108
void fail PROTO_N ((message)) PROTO_T (char * message)
109
{
110
  fprintf(stderr,"*fail* : %s\n",message);
111
  exit(1);
112
}
113
 
114
 
115
void found_one PROTO_N ((str)) PROTO_T (char *str)
116
{
117
  if (names_size == names_found)
118
  {
119
    if (names_size==0)
120
    {
121
      names = (char **)malloc(20*sizeof(char *));
122
      names_size = 20;
123
    }
124
    else
125
    {
126
      names_size +=20;
127
      names = (char **)realloc(names,names_size*sizeof(char *));
128
    }
129
  }
130
  names[names_found] = (char *)malloc(strlen(str)+1);
131
  strcpy(names[names_found],str);
132
  names_found++;
133
  return;
134
}
135
 
136
void scan_object_file PROTO_Z ()
137
{
138
  int i;
139
  struct filehdr header;
140
  struct syment symbol;
141
  FILE *fp = open_file(input_file,"r");
142
 
143
  fread(&header,sizeof(struct filehdr),1,fp);
144
 
145
  if (IS_BAD_MAGIC_NUMBER(header.f_magic))
146
  {
147
    fail("Bad magic number");
148
  }
149
  if (header.f_nsyms==0)
150
  {
151
    fail("No symbol table info");
152
  }
153
 
154
  fseek(fp,header.f_symptr,0);
155
  for (i=0; i<header.f_nsyms;i++)
156
  {
157
    long keeppos;
158
 
159
    fread(&symbol,SYMESZ,1,fp);
160
    if (symbol.n_sclass==C_EXT && symbol._n._n_n._n_zeroes==0)
161
    {
162
      char str[256];
163
      long offset = symbol._n._n_n._n_offset + header.f_symptr + (SYMESZ*header.f_nsyms);
164
      long keeppos = ftell(fp);
165
 
166
      fseek(fp,offset,0);
167
      fgets(str,256,fp);
168
      if (strncmp(str,"__I.TDF",7)==0)
169
      {
170
        found_one(str);
171
      }
172
      fseek(fp,keeppos,0);
173
    }
174
  }
175
  fclose(fp);
176
 
177
  output_assembler_file();
178
}
179
 
180
void output_assembler_file PROTO_Z ()
181
{
182
  int i;
183
  FILE *of = fopen(output_file,"w");
184
 
185
  for (i=0;i<names_found;i++)
186
  {
187
    fprintf (of,"\t.extern %s\n",names[i]);
188
    fprintf (of,"\t.extern .%s\n",names[i]);
189
  }
190
  fprintf(of,"\t.globl\t__main\n");
191
  fprintf(of,"\t.globl\t.__main\n");
192
  fprintf(of,"\t.extern\t__main\n");
193
  fprintf(of,"\t.extern\t.__main\n\n");
194
  fprintf(of,"\t.toc\n");
195
 
196
  for (i=0;i<names_found;i++)
197
  {
198
    fprintf (of,"\tT.%s:\t.tc\t%s[TC],%s\n",names[i],names[i],names[i]);
199
  }
200
 
201
  fprintf(of,"\t.csect\t[DS]\n");
202
  fprintf(of,"__main:\n");
203
  fprintf(of,"\t.long\t.__main,TOC[tc0],0\n");
204
  fprintf(of,"\t.csect\t[PR]\n");
205
 
206
  fprintf(of,".__main:\n");
207
  fprintf(of,"\tmflr\t0\n");
208
  fprintf(of,"\tst\t0,8(1)\n");
209
  fprintf(of,"\tai\t1,1,-56\n");
210
 
211
  for (i=0;i<names_found;i++)
212
  {
213
    fprintf (of,"\tbl\t.%s\n",names[i]);
214
    fprintf(of,"\tcror\t15,15,15\n");
215
  }
216
 
217
  fprintf(of,"\tai\t1,1,56\n");
218
  fprintf(of,"\tl\t0,8(1)\n");
219
  fprintf(of,"\tmtlr\t0\n");
220
  fprintf(of,"\tbr\n");
221
 
222
  fclose(of);
223
 
224
}