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 <sun3/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 *);
86
#define open_file_read(x)	open_file(x, "r")
87
#define open_file_write(x)	open_file(x, "w")
2 7u83 88
 
89
 
7 7u83 90
FILE *
91
open_file(char *filename, char *option)
2 7u83 92
{
93
  FILE * ret_fp;
7 7u83 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, ".text\n");
131
  fprintf(of, "\t.even\n");
132
  fprintf(of, ".globl %s\n", INIT_FUNC_NAME);
133
  fprintf(of, "%s:\n", INIT_FUNC_NAME);
2 7u83 134
  return;
135
}
136
 
7 7u83 137
void
138
print_ass_postlude(FILE *of)
2 7u83 139
{
7 7u83 140
  fprintf(of, "\trts\n");
2 7u83 141
}
142
 
143
void construct_init_proc
7 7u83 144
(FILE *fp, long symtab_pos, long stringtab_pos, EXEC_STRUCT header, char *outname)
2 7u83 145
{
146
  int i;
147
  char str[256];
148
  long keeppos;
149
  FILE *outfile;
7 7u83 150
  if (!(outfile = open_file_write(outname))) {
2 7u83 151
    fail("Cannot Open output file");
152
  }
7 7u83 153
  fseek(fp, symtab_pos, 0);
2 7u83 154
  print_ass_preamble(outfile);
7 7u83 155
  for (i = 0; i < header.a_syms / sizeof(SYMBOL_STRUCT); ++i) {
156
    fread(&symbol, sizeof(SYMBOL_STRUCT), 1, fp);
157
    if (is_function(symbol)) {
2 7u83 158
      keeppos = ftell(fp);
7 7u83 159
      fseek(fp, stringtab_pos + symbol.n_un.n_strx, 0);
160
      fgets(str, 256, fp);
161
      if (is_intitialisation_function(str)) {
162
	fprintf(outfile, "\tjbsr\t%s\n", str);
2 7u83 163
      }
7 7u83 164
      fseek(fp, keeppos, 0);
2 7u83 165
    }
166
  }
167
  print_ass_postlude(outfile);
168
  return;
169
}
170
 
7 7u83 171
void
172
process_flag(char *option)
2 7u83 173
{
7 7u83 174
  switch (option[1]) {
2 7u83 175
    case 'V':
176
    case 'v': {
7 7u83 177
     (void)fprintf(stderr, "Dynamic initialisation linker V%d.%d\n",
178
		    MAJOR_VERSION, MINOR_VERSION);
2 7u83 179
      break;
180
    }
181
    default: {
7 7u83 182
     (void)fprintf(stderr, "Error: unknown option %s\n", option);
2 7u83 183
    }
184
  }
185
  return;
186
}
187
 
188
 
7 7u83 189
int
190
main(int argc, char **argv)
2 7u83 191
{
192
  FILE *fp;
7 7u83 193
  long symtab_pos, stringtab_pos;
2 7u83 194
  int i;
195
  int symsize;
196
  char str[80];
197
  long keeppos;
198
  int num_flags = 0;
7 7u83 199
 
200
  for (i = 1; i < argc; ++i) {
201
    if (argv[i][0] == '-') {
2 7u83 202
      num_flags++;
203
      process_flag(argv[i]);
204
    }
205
  }
7 7u83 206
  if (argc - num_flags < 3) {
207
    fprintf(stderr, "*fail*: syntax is %s [-options] <input filename> <output filename>\n", argv[0]);
2 7u83 208
    exit(1);
209
  }
7 7u83 210
  fp = open_file_read(argv[1 + num_flags]);
2 7u83 211
 
7 7u83 212
  fread(&header, sizeof(EXEC_STRUCT), 1, fp);
213
  if (N_BADMAG(header)) {
2 7u83 214
    fail("Bad magic number\n");
215
  }
216
  symtab_pos = N_SYMOFF(header);
7 7u83 217
 
2 7u83 218
  stringtab_pos = N_STROFF(header);
7 7u83 219
  construct_init_proc(fp, symtab_pos, stringtab_pos, header,
220
		      argv[2 + num_flags]);
2 7u83 221
  return 0;
222
}