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 <sparc/a.out.h>
79
#include <sparc/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
 
75
typedef struct exec EXEC_STRUCT;
104
typedef struct exec EXEC_STRUCT;
76
 
105
 
77
typedef struct nlist SYMBOL_STRUCT;
106
typedef struct nlist SYMBOL_STRUCT;
78
 
107
 
79
#define INIT_FUNC_NAME "___TDF_main"
108
#define INIT_FUNC_NAME "___TDF_main"
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
{
-
 
89
  fprintf(stderr,"*fail* : %s\n",message);
-
 
90
  exit(1);
-
 
91
}
-
 
92
 
-
 
93
 
-
 
94
/*#define symbol_is_function(x) (x.n_type & N_TEXT)*/
-
 
95
#define is_intitialisation_function(x) (!strncmp(x+1,"__I.TDF",7))
-
 
96
#define is_function(x) 1
-
 
97
 
-
 
98
 
-
 
99
void print_ass_preamble
-
 
100
    PROTO_N((of))
-
 
101
    PROTO_T(FILE *of)
-
 
102
{
116
{
103
  fprintf(of,".global ");
-
 
104
  fprintf(of,INIT_FUNC_NAME);
-
 
105
  fprintf(of,"\n");
-
 
106
  fprintf(of,"\t.seg\t\"text\"\n");
-
 
107
  fprintf(of,"\t.align\t4\n");
-
 
108
  fprintf(of,INIT_FUNC_NAME);
-
 
109
  fprintf(of,":\n");
-
 
110
  fprintf(of,"\tsave\t%%sp,-64,%%sp\n");
117
  fprintf(stderr, "*fail* : %s\n", message);
111
  return;
118
  exit(1);
112
}
119
}
113
 
120
 
-
 
121
 
-
 
122
/*#define symbol_is_function(x) (x.n_type & N_TEXT)*/
-
 
123
#define is_intitialisation_function(x)	(!strncmp(x + 1, "__I.TDF", 7))
-
 
124
#define is_function(x)1
-
 
125
 
-
 
126
 
-
 
127
void
114
void print_ass_postlude
128
print_ass_preamble(FILE *of)
-
 
129
{
-
 
130
  fprintf(of, ".global ");
-
 
131
  fprintf(of, INIT_FUNC_NAME);
115
    PROTO_N((of))
132
  fprintf(of, "\n");
-
 
133
  fprintf(of, "\t.seg\t\"text\"\n");
-
 
134
  fprintf(of, "\t.align\t4\n");
-
 
135
  fprintf(of, INIT_FUNC_NAME);
-
 
136
  fprintf(of, ":\n");
-
 
137
  fprintf(of, "\tsave\t%%sp, -64, %%sp\n");
-
 
138
  return;
-
 
139
}
-
 
140
 
-
 
141
void
116
    PROTO_T(FILE *of)
142
print_ass_postlude(FILE *of)
117
{
143
{
118
  fprintf(of,"\tret\n\trestore\n");
144
  fprintf(of, "\tret\n\trestore\n");
119
}
145
}
120
 
146
 
121
void construct_init_proc
147
void
122
    PROTO_N((fp,symtab_pos,stringtab_pos,header,outname))
-
 
123
    PROTO_T(FILE *fp X long symtab_pos X long stringtab_pos X
148
construct_init_proc(FILE *fp, long symtab_pos, long stringtab_pos,
124
	    EXEC_STRUCT header X char *outname)
149
		    EXEC_STRUCT header, char *outname)
125
{
150
{
126
  int i;
151
  int i;
127
  char str[256];
152
  char str[256];
128
  long keeppos;
153
  long keeppos;
129
  FILE *outfile;
154
  FILE *outfile;
130
  if(!(outfile = open_file_write(outname))){
155
  if (!(outfile = open_file_write(outname))) {
131
    fail("Cannot Open output file");
156
    fail("Cannot Open output file");
132
  }
157
  }
133
  fseek(fp,symtab_pos,0);
158
  fseek(fp, symtab_pos, 0);
134
  print_ass_preamble(outfile);
159
  print_ass_preamble(outfile);
135
  for(i=0;i<header.a_syms/sizeof(SYMBOL_STRUCT);++i){
160
  for (i = 0; i < header.a_syms / sizeof(SYMBOL_STRUCT); ++i) {
136
    fread(&symbol,sizeof(SYMBOL_STRUCT),1,fp);
161
    fread(&symbol, sizeof(SYMBOL_STRUCT), 1, fp);
137
    if(is_function(symbol)){
162
    if (is_function(symbol)) {
138
      keeppos = ftell(fp);
163
      keeppos = ftell(fp);
139
      fseek(fp,stringtab_pos+symbol.n_un.n_strx,0);
164
      fseek(fp, stringtab_pos + symbol.n_un.n_strx, 0);
140
      fgets(str,256,fp);
165
      fgets(str, 256, fp);
141
      if(is_intitialisation_function(str)){
166
      if (is_intitialisation_function(str)) {
142
	fprintf(outfile,"\tcall\t%s,0\n\tnop\n",str);
167
	fprintf(outfile, "\tcall\t%s, 0\n\tnop\n", str);
143
      }
168
      }
144
      fseek(fp,keeppos,0);
169
      fseek(fp, keeppos, 0);
145
    }
170
    }
146
  }
171
  }
147
  print_ass_postlude(outfile);
172
  print_ass_postlude(outfile);
148
  return;
173
  return;
149
}
174
}
150
 
175
 
151
void process_flag
176
void
152
    PROTO_N((option))
-
 
153
    PROTO_T(char *option)
177
process_flag(char *option)
154
{
178
{
155
  switch(option[1]) {
179
  switch (option[1]) {
156
    case 'V':
180
    case 'V':
157
    case 'v': {
181
    case 'v': {
158
      (void)fprintf(stderr,"Dynamic initialisation linker V%d.%d\n",
182
     (void)fprintf(stderr, "Dynamic initialisation linker V%d.%d\n",
159
		    MAJOR_VERSION,MINOR_VERSION);
183
		    MAJOR_VERSION, MINOR_VERSION);
160
      break;
184
      break;
161
    }
185
    }
162
    default: {
186
    default: {
163
      (void)fprintf(stderr,"Error: unknown option %s\n",option);
187
     (void)fprintf(stderr, "Error: unknown option %s\n", option);
164
    }
188
    }
165
  }
189
  }
166
  return;
190
  return;
167
}
191
}
168
 
192
 
169
 
193
 
170
int main
194
int
171
    PROTO_N((argc,argv))
-
 
172
    PROTO_T(int argc X char **argv)
195
main(int argc, char **argv)
173
{
196
{
174
  FILE *fp;
197
  FILE *fp;
175
  long symtab_pos,stringtab_pos;
198
  long symtab_pos, stringtab_pos;
176
  int i;
199
  int i;
177
  int symsize;
200
  int symsize;
178
  char str[80];
201
  char str[80];
179
  long keeppos;
202
  long keeppos;
180
  int num_flags = 0;
203
  int num_flags = 0;
181
  
204
 
182
  for(i=1;i<argc;++i) {
205
  for (i = 1; i < argc; ++i) {
183
    if(argv[i][0] == '-') {
206
    if (argv[i][0] == '-') {
184
      num_flags++;
207
      num_flags++;
185
      process_flag(argv[i]);
208
      process_flag(argv[i]);
186
    }
209
    }
187
  }
210
  }
188
  if(argc - num_flags < 3) {
211
  if (argc - num_flags < 3) {
189
    fprintf(stderr,"*fail*: syntax is %s [-options] <input filename> <output filename>\n",argv[0]);
212
    fprintf(stderr, "*fail*: syntax is %s [-options] <input filename> <output filename>\n", argv[0]);
190
    exit(1);
213
    exit(1);
191
  }
214
  }
192
  fp = open_file_read(argv[1+num_flags]);
215
  fp = open_file_read(argv[1 + num_flags]);
193
 
216
 
194
  fread(&header,sizeof(EXEC_STRUCT),1,fp);
217
  fread(&header, sizeof(EXEC_STRUCT), 1, fp);
195
  if(N_BADMAG(header)){
218
  if (N_BADMAG(header)) {
196
    fail("Bad magic number\n");
219
    fail("Bad magic number\n");
197
  }
220
  }
198
  symtab_pos = N_SYMOFF(header);
221
  symtab_pos = N_SYMOFF(header);
199
  
222
 
200
  stringtab_pos = N_STROFF(header);
223
  stringtab_pos = N_STROFF(header);
201
  construct_init_proc(fp,symtab_pos,stringtab_pos,header,argv[2+num_flags]);
224
  construct_init_proc(fp, symtab_pos, stringtab_pos, header,
-
 
225
		      argv[2 + num_flags]);
202
  return 0;
226
  return 0;
203
}
227
}