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