source: trunk/tools/AliasDef/AliasDef.cpp@ 9838

Last change on this file since 9838 was 9838, checked in by bird, 23 years ago

Initial coding.

File size: 9.3 KB
Line 
1/* $Id: AliasDef.cpp,v 1.1 2003-02-23 03:02:14 bird Exp $ */
2/*
3 * aliasdef - Create aliases for unmangled GCC stdcall symbols.
4 *
5 * Copyright (c) 1999-2003 knut st. osmundsen
6 *
7 */
8
9/*******************************************************************************
10* Header Files *
11*******************************************************************************/
12//#include <os2.h>
13#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
16
17#include "kTypes.h"
18#include "kError.h"
19#include "kFile.h"
20#include "kFileInterfaces.h"
21#include "kFileFormatBase.h"
22#include "kFileDef.h"
23
24/*******************************************************************************
25* Internal Functions *
26*******************************************************************************/
27static void syntax(void);
28static long processFile(const char *pszInput, const char *pszOutput, KBOOL fNoPublic);
29
30
31/**
32 * Main function.
33 * @returns 0 on success.
34 * @param argc Number of arguments
35 * @param argv Array of arguments
36 */
37int main(int argc, char **argv)
38{
39 int argi;
40 KBOOL fFatal = FALSE;
41 long lRc = 0;
42 char *pszInput = NULL;
43 char *pszOutput = NULL;
44 KBOOL fNoPublic = FALSE;
45
46 /**************************************************************************
47 * parse arguments.
48 * options: -h or -? help
49 * -n no public.
50 **************************************************************************/
51 if (argc == 1)
52 syntax();
53 argi = 1;
54 while (argi < argc && !fFatal)
55 {
56 if(argv[argi][0] == '-' || argv[argi][0] == '/')
57 {
58 switch (argv[argi][1])
59 {
60 case 'n':
61 case 'N':
62 fNoPublic = TRUE;
63 break;
64
65 case '?':
66 case 'h':
67 case 'H':
68 syntax();
69 return 0;
70
71 default:
72 kFile::StdErr.printf("incorrect parameter. (argi=%d, argv[argi]=%s)\n", argi, argv[argi]);
73 fFatal = TRUE;
74 break;
75 }
76 }
77 else
78 {
79 if (pszInput == NULL)
80 pszInput = argv[argi];
81 else if (pszOutput == NULL)
82 pszOutput = argv[argi];
83 else
84 {
85 kFile::StdErr.printf("To many files are specified!\n");
86 fFatal = TRUE;
87 }
88 }
89 argi++;
90 }
91
92 if (pszInput == NULL)
93 {
94 fFatal = TRUE;
95 kFile::StdErr.printf("Missing input file.\n");
96 }
97 else if (pszOutput == NULL)
98 {
99 fFatal = TRUE;
100 kFile::StdErr.printf("Missing output file.\n");
101 }
102
103 if (!fFatal)
104 lRc = processFile(pszInput, pszOutput, fNoPublic);
105 else
106 lRc = -1;
107
108 return (int)lRc;
109}
110
111
112/**
113 * Print syntax/help message.
114 */
115static void syntax(void)
116{
117 kFile::StdOut.printf(
118 "\n"
119 "AliasDef - Creates alias based on .def like file\n"
120 "------------------------------------------------\n"
121 "syntax: AliasDef.exe [-h|-?] [-S] <infile> <outfile>\n"
122 " -h or -? Syntax help. (this)\n"
123 " -n No public definition of the alias.\n"
124 " infile Name of input file (.def file).\n"
125 " outfile Name of output file (OMF object).\n"
126 "\n"
127 "Notes:\n"
128 " The input file is an .def file. EXPORTS is used on this form:\n"
129 " <alias> = <internalname>\n"
130 );
131}
132
133
134/**
135 *
136 * @returns 0 on success, error code on error.
137 * @param pszInput Input filename.
138 * @param pszOuput Output filename.
139 * @sketch Open input file
140 * try create a kFileDef object from inputfile.
141 * Open output file.
142 * Generate output file.
143 * @remark
144 */
145static long processFile(const char *pszInput, const char *pszOutput, KBOOL fNoPublic)
146{
147 long lRc = 0;
148
149
150 try
151 {
152 kFile * pInput = new kFile(pszInput);
153 try
154 {
155 kFileDef * pDefFile = new kFileDef(pInput);
156 try
157 {
158 kFile * pOutput = new kFile(pszOutput, FALSE);
159 kExportEntry export;
160 #pragma pack(1)
161 struct OMFRec
162 {
163 unsigned char chType;
164 unsigned short cb; /* excludes the header! */
165 char ach[1024];
166 } rec;
167 #pragma pack()
168
169 /* THEADR */
170 rec.chType = 0x80;
171 strcpy(&rec.ach[1], pszInput);
172 rec.ach[0] = (char)strlen(pszInput);
173 rec.cb = (short)(rec.ach[0] + 2);
174 pOutput->write(&rec, rec.cb + 3);
175
176
177 /* Exports */
178 if (pDefFile->exportFindFirst(&export))
179 {
180 //int iExt = 0;
181 do
182 {
183 /* validate export struct */
184 if (export.achName[0] == '\0')
185 {
186 kFile::StdErr.printf(
187 "Warning export name is missing.\n"
188 "info:\texport.achIntName=%s\n\texport.achName=%s\n\texport.ulOrdinal=%ld\n",
189 export.achIntName, export.achName, export.ulOrdinal);
190 continue;
191 }
192
193 int cch = strlen(export.achName);
194 int cchInt = strlen(export.achIntName);
195 #if 1 //aliased approach
196 #if 0
197 /* emit extdef record */
198 rec.chType = 0x8c;
199 strcpy(&rec.ach[1], export.achIntName);
200 rec.ach[0] = cchInt;
201 rec.ach[1 + cchInt + 1] = 0;
202 rec.cb = (short)(cchInt + 3);
203 pOutput->write(&rec, rec.cb + 3);
204 #endif
205
206 /* emit alias record */
207 rec.chType = 0xc6;
208 strcpy(&rec.ach[1], export.achName);
209 rec.ach[0] = (char)cch;
210 strcpy(&rec.ach[1 + cch + 1], export.achIntName);
211 rec.ach[1 + cch] = (char)cchInt;
212 rec.cb = (short)(cch + cchInt + 3);
213 pOutput->write(&rec, rec.cb + 3);
214
215 if (!fNoPublic)
216 {
217 /* emit pubdef record */
218 rec.chType = 0x90;
219 rec.ach[0] = 0;
220 rec.ach[1] = 0;
221 rec.ach[2] = 0;
222 rec.ach[3] = 0;
223 rec.ach[4] = (char)cch;
224 strcpy(&rec.ach[5], export.achName);
225 rec.ach[5 + cch + 0] = 0;
226 rec.ach[5 + cch + 1] = 0;
227 rec.ach[5 + cch + 2] = 0;
228 rec.ach[5 + cch + 3] = 0;
229 rec.cb = cch + 4 + 5;
230 pOutput->write(&rec, rec.cb + 3);
231 }
232
233
234 #else //weak approach.
235
236 /* emit extdef record */
237 rec.chType = 0x8c;
238 strcpy(&rec.ach[1], export.achName);
239 rec.ach[0] = cch;
240 strcpy(&rec.ach[1 + cch + 2], export.achIntName);
241 rec.ach[1 + cch + 1] = cchInt;
242 rec.ach[1 + cch + 2 + cchInt + 1] = 0;
243 rec.cb = 1 + cch + 2 + cchInt + 2;
244 pOutput->write(&rec, rec.cb + 3);
245
246 /* emit weak comment record */
247 rec.chType = 0x88;
248 rec.ach[0] = 0;
249 rec.ach[1] = 0xa8;
250 rec.ach[2] = ++iExt;
251 rec.ach[3] = ++iExt;
252 rec.ach[4] = 0;
253 rec.cb = 5;
254 pOutput->write(&rec, rec.cb + 3);
255
256
257 #endif
258 } while (pDefFile->exportFindNext(&export));
259
260 pOutput->setSize();
261 }
262
263 /* MODEND32 */
264 rec.chType = 0x8b;
265 rec.cb = 2;
266 rec.ach[0] = 1;
267 rec.ach[1] = 0;
268 pOutput->write(&rec, rec.cb + 3);
269
270 delete pOutput;
271 }
272 catch (kError err)
273 {
274 kFile::StdErr.printf("error creating output file, '%s', errorcode 0x%x\n", pszOutput, err.getErrno());
275 lRc = -4;
276 }
277 delete pDefFile;
278 }
279 catch (kError err)
280 {
281 kFile::StdErr.printf("%s is not a valid def file, errorcode 0x%x\n", pszInput, err.getErrno());
282 lRc = -3;
283 }
284 //delete pInput; - not needed done by ~kFileFormatBase!
285 }
286 catch (kError err)
287 {
288 kFile::StdErr.printf( "error openining inputfile, '%s', errorcode 0x%x\n", pszInput, err.getErrno());
289 lRc = -2;
290 }
291
292 return lRc;
293}
294
Note: See TracBrowser for help on using the repository browser.