Subversion Repositories tendra.SVN

Rev

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

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
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
 
1
/*
32
/*
2
    		 Crown Copyright (c) 1997
33
    		 Crown Copyright (c) 1997
3
    
34
 
4
    This TenDRA(r) Computer Program is subject to Copyright
35
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
36
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
37
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
38
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
39
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
40
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
41
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
42
    shall be deemed to be acceptance of the following conditions:-
12
    
43
 
13
        (1) Its Recipients shall ensure that this Notice is
44
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
45
        reproduced upon any copies or amended versions of it;
15
    
46
 
16
        (2) Any amended version of it shall be clearly marked to
47
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
48
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
49
        for the relevant amendment or amendments;
19
    
50
 
20
        (3) Its onward transfer from a recipient to another
51
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
52
        party shall be deemed to be that party's acceptance of
22
        these conditions;
53
        these conditions;
23
    
54
 
24
        (4) DERA gives no warranty or assurance as to its
55
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
56
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
57
        no liability whatsoever in relation to any use to which
27
        it may be put.
58
        it may be put.
28
*/
59
*/
Line 30... Line 61...
30
 
61
 
31
/*
62
/*
32
  dyninit
63
  dyninit
33
 
64
 
34
  This program extracts all the initialisation procedures from a SunOS
65
  This program extracts all the initialisation procedures from a SunOS
35
  object file.  It then writes out a .s file containing a function 
66
  object file.  It then writes out a .s file containing a function
36
  (___TDF_main) which contains calls to each of these procedures.
67
  (___TDF_main) which contains calls to each of these procedures.
37
  Initialisation procedures are identified by the ___I.TDF prefix.
68
  Initialisation procedures are identified by the ___I.TDF prefix.
38
  Note: the actual prefix starts with two '_'s but SunOS inserts
69
  Note: the actual prefix starts with two '_'s but SunOS inserts
39
  an extra one.
70
  an extra one.
40
 
71
 
Line 44... Line 75...
44
 
75
 
45
*/
76
*/
46
 
77
 
47
#include <stdio.h>
78
#include <stdio.h>
48
#include <sun3/a.out.h>
79
#include <sun3/a.out.h>
49
#include "ossg.h"
-
 
50
 
80
 
51
#define MAJOR_VERSION		1
81
#define MAJOR_VERSION		1
52
#define MINOR_VERSION		1
82
#define MINOR_VERSION		1
53
 
83
 
54
void fail PROTO_S((char *));
84
void fail(char *);
55
FILE *open_file PROTO_S((char *,char *));
85
FILE *open_file(char *, char *);
56
#define open_file_read(x)	open_file(x,"r")
86
#define open_file_read(x)	open_file(x, "r")
57
#define open_file_write(x)	open_file(x,"w")
87
#define open_file_write(x)	open_file(x, "w")
58
 
88
 
59
 
89
 
60
FILE *open_file
90
FILE *
61
    PROTO_N((filename,option))
-
 
62
    PROTO_T(char *filename X char *option)
91
open_file(char *filename, char *option)
63
{
92
{
64
  FILE * ret_fp;
93
  FILE * ret_fp;
65
  if(ret_fp = fopen(filename,option)){
94
  if (ret_fp = fopen(filename, option)) {
66
    return ret_fp;
95
    return ret_fp;
67
  }
96
  }
68
  else{
97
  else{
69
    fprintf(stderr,"Cannot open file <%s>\n",filename);
98
    fprintf(stderr, "Cannot open file <%s>\n", filename);
70
    exit(1);
99
    exit(1);
71
  }
100
  }
72
}
101
}
73
 
102
 
74
 
103
 
Line 80... Line 109...
80
 
109
 
81
EXEC_STRUCT header;
110
EXEC_STRUCT header;
82
SYMBOL_STRUCT symbol;
111
SYMBOL_STRUCT symbol;
83
 
112
 
84
 
113
 
85
void fail
114
void
86
    PROTO_N((message))
-
 
87
    PROTO_T(char *message)
115
fail(char *message)
88
{
116
{
89
  fprintf(stderr,"*fail* : %s\n",message);
117
  fprintf(stderr, "*fail* : %s\n", message);
90
  exit(1);
118
  exit(1);
91
}
119
}
92
 
120
 
93
 
121
 
94
/*#define symbol_is_function(x) (x.n_type & N_TEXT)*/
122
/*#define symbol_is_function(x) (x.n_type & N_TEXT)*/
95
#define is_intitialisation_function(x) (!strncmp(x+1,"__I.TDF",7))
123
#define is_intitialisation_function(x)	(!strncmp(x + 1, "__I.TDF", 7))
96
#define is_function(x) 1
124
#define is_function(x)	1
97
 
125
 
98
 
126
 
99
void print_ass_preamble
127
void
100
    PROTO_N((of))
-
 
101
    PROTO_T(FILE *of)
128
print_ass_preamble(FILE *of)
102
{
129
{
103
  fprintf(of,".text\n");
130
  fprintf(of, ".text\n");
104
  fprintf(of,"\t.even\n");
131
  fprintf(of, "\t.even\n");
105
  fprintf(of,".globl %s\n",INIT_FUNC_NAME);
132
  fprintf(of, ".globl %s\n", INIT_FUNC_NAME);
106
  fprintf(of,"%s:\n",INIT_FUNC_NAME);
133
  fprintf(of, "%s:\n", INIT_FUNC_NAME);
107
  return;
134
  return;
108
}
135
}
109
 
136
 
110
void print_ass_postlude
137
void
111
    PROTO_N((of))
-
 
112
    PROTO_T(FILE *of)
138
print_ass_postlude(FILE *of)
113
{
139
{
114
  fprintf(of,"\trts\n");
140
  fprintf(of, "\trts\n");
115
}
141
}
116
 
142
 
117
void construct_init_proc
143
void construct_init_proc
118
    PROTO_N((fp,symtab_pos,stringtab_pos,header,outname))
144
(FILE *fp, long symtab_pos, long stringtab_pos, EXEC_STRUCT header, char *outname)
119
    PROTO_T(FILE *fp X long symtab_pos X long stringtab_pos X
-
 
120
	    EXEC_STRUCT header X char *outname)
-
 
121
{
145
{
122
  int i;
146
  int i;
123
  char str[256];
147
  char str[256];
124
  long keeppos;
148
  long keeppos;
125
  FILE *outfile;
149
  FILE *outfile;
126
  if(!(outfile = open_file_write(outname))){
150
  if (!(outfile = open_file_write(outname))) {
127
    fail("Cannot Open output file");
151
    fail("Cannot Open output file");
128
  }
152
  }
129
  fseek(fp,symtab_pos,0);
153
  fseek(fp, symtab_pos, 0);
130
  print_ass_preamble(outfile);
154
  print_ass_preamble(outfile);
131
  for(i=0;i<header.a_syms/sizeof(SYMBOL_STRUCT);++i){
155
  for (i = 0; i < header.a_syms / sizeof(SYMBOL_STRUCT); ++i) {
132
    fread(&symbol,sizeof(SYMBOL_STRUCT),1,fp);
156
    fread(&symbol, sizeof(SYMBOL_STRUCT), 1, fp);
133
    if(is_function(symbol)){
157
    if (is_function(symbol)) {
134
      keeppos = ftell(fp);
158
      keeppos = ftell(fp);
135
      fseek(fp,stringtab_pos+symbol.n_un.n_strx,0);
159
      fseek(fp, stringtab_pos + symbol.n_un.n_strx, 0);
136
      fgets(str,256,fp);
160
      fgets(str, 256, fp);
137
      if(is_intitialisation_function(str)){
161
      if (is_intitialisation_function(str)) {
138
	fprintf(outfile,"\tjbsr\t%s\n",str);
162
	fprintf(outfile, "\tjbsr\t%s\n", str);
139
      }
163
      }
140
      fseek(fp,keeppos,0);
164
      fseek(fp, keeppos, 0);
141
    }
165
    }
142
  }
166
  }
143
  print_ass_postlude(outfile);
167
  print_ass_postlude(outfile);
144
  return;
168
  return;
145
}
169
}
146
 
170
 
147
void process_flag
171
void
148
    PROTO_N((option))
-
 
149
    PROTO_T(char *option)
172
process_flag(char *option)
150
{
173
{
151
  switch(option[1]) {
174
  switch (option[1]) {
152
    case 'V':
175
    case 'V':
153
    case 'v': {
176
    case 'v': {
154
      (void)fprintf(stderr,"Dynamic initialisation linker V%d.%d\n",
177
     (void)fprintf(stderr, "Dynamic initialisation linker V%d.%d\n",
155
		    MAJOR_VERSION,MINOR_VERSION);
178
		    MAJOR_VERSION, MINOR_VERSION);
156
      break;
179
      break;
157
    }
180
    }
158
    default: {
181
    default: {
159
      (void)fprintf(stderr,"Error: unknown option %s\n",option);
182
     (void)fprintf(stderr, "Error: unknown option %s\n", option);
160
    }
183
    }
161
  }
184
  }
162
  return;
185
  return;
163
}
186
}
164
 
187
 
165
 
188
 
166
int main
189
int
167
    PROTO_N((argc,argv))
-
 
168
    PROTO_T(int argc X char **argv)
190
main(int argc, char **argv)
169
{
191
{
170
  FILE *fp;
192
  FILE *fp;
171
  long symtab_pos,stringtab_pos;
193
  long symtab_pos, stringtab_pos;
172
  int i;
194
  int i;
173
  int symsize;
195
  int symsize;
174
  char str[80];
196
  char str[80];
175
  long keeppos;
197
  long keeppos;
176
  int num_flags = 0;
198
  int num_flags = 0;
177
  
199
 
178
  for(i=1;i<argc;++i) {
200
  for (i = 1; i < argc; ++i) {
179
    if(argv[i][0] == '-') {
201
    if (argv[i][0] == '-') {
180
      num_flags++;
202
      num_flags++;
181
      process_flag(argv[i]);
203
      process_flag(argv[i]);
182
    }
204
    }
183
  }
205
  }
184
  if(argc - num_flags < 3) {
206
  if (argc - num_flags < 3) {
185
    fprintf(stderr,"*fail*: syntax is %s [-options] <input filename> <output filename>\n",argv[0]);
207
    fprintf(stderr, "*fail*: syntax is %s [-options] <input filename> <output filename>\n", argv[0]);
186
    exit(1);
208
    exit(1);
187
  }
209
  }
188
  fp = open_file_read(argv[1+num_flags]);
210
  fp = open_file_read(argv[1 + num_flags]);
189
 
211
 
190
  fread(&header,sizeof(EXEC_STRUCT),1,fp);
212
  fread(&header, sizeof(EXEC_STRUCT), 1, fp);
191
  if(N_BADMAG(header)){
213
  if (N_BADMAG(header)) {
192
    fail("Bad magic number\n");
214
    fail("Bad magic number\n");
193
  }
215
  }
194
  symtab_pos = N_SYMOFF(header);
216
  symtab_pos = N_SYMOFF(header);
195
  
217
 
196
  stringtab_pos = N_STROFF(header);
218
  stringtab_pos = N_STROFF(header);
197
  construct_init_proc(fp,symtab_pos,stringtab_pos,header,argv[2+num_flags]);
219
  construct_init_proc(fp, symtab_pos, stringtab_pos, header,
-
 
220
		      argv[2 + num_flags]);
198
  return 0;
221
  return 0;
199
}
222
}