Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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