Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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