-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathb.c
186 lines (155 loc) · 6.17 KB
/
b.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* File created 14.11.2021, last modified 07.01.2022
*
* The b.c file is part of the restored LINK.COM program
* from the Hi-Tech C compiler v3.09 package.
*
* Andrey Nikitin & Mark Ogden 07.01.2022
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "link.h"
/* The following macros allow for different bit vector implementations */
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
#define VECBITS (sizeof(vec_t)*CHAR_BIT)
#define VECBYTES(bits) (bits + VECBITS - 1) / VECBITS * sizeof(vec_t)
#define SetBit(v, n) ((v)[(n)/VECBITS] |= (1 << ((n) & (VECBITS - 1))))
#define TstBit(v, n) ((1 << ((n) & (VECBITS - 1))) & (v)[(n) / VECBITS])
/**************************************************************************
17 seek_err sub-07c5h ok++ (nau) (PMO)
**************************************************************************/
void seek_err() {
fatal_err("%s: Can't seek", libraryName);
}
/**************************************************************************
18 allocModuleArrays sub_07d3h ok++ (nau) (PMO)
**************************************************************************/
void allocModuleArrays(int num_lib_files) {
/* Allocate space for the library module include flags */
/* The include flag space itself is allocated as each file is processed */
num_files = num_lib_files;
libTable = xalloc(num_files * sizeof(vec_t *));
}
/**************************************************************************
19 conv_letou32 sub_07ea ok++ (nau) (PMO)
**************************************************************************/
uint32_t conv_letou32(register uint8_t *p1) {
return *p1 + (*(p1 + 1) << 8) + (*(p1 + 2) << 0x10) + (*(p1 + 3) << 0x18);
}
/**************************************************************************
20 conv_btou16 sub_082a ok++ (nau) (PMO)
**************************************************************************/
uint16_t conv_btou16(register uint8_t *p1) {
return *p1 + (*(p1 + 1) << 8);
}
/**************************************************************************
21 openLibrary sub_084a ok++ (nau) (PMO)
**************************************************************************/
void openLibrary() {
libraryName = fname_obj;
fname_obj = 0;
#ifdef CPM
if ((libraryFp = fopen(libraryName, "rb")) == 0)
#else
if ((libraryFp = fopen(libraryName, "rb")) == 0 &&
(libraryFp = fopen(mkLibPath(libraryName), "rb")) == 0)
#endif
fatal_err("%s: Can't open", libraryName);
if (fread(libBuf, 1, 4, libraryFp) != 4)
unexp_eof();
num_modules = conv_btou16(libBuf + 2);
size_symbols = conv_btou16(libBuf);
/* / libPass1() */
(libHandlers[linker_pass])();
/* \ libPass2() */
libraryName = 0;
fclose(libraryFp);
}
/**************************************************************************
22 libPass1 ok++ (PMO)
**************************************************************************/
void libPass1() {
/* Allocate uint8_t values to store a flag for whether the module within
* a library is needed or not
*/
libTable[num_lib_files] = xalloc(VECBYTES(num_modules));
if (fseek(libraryFp, 4, SEEK_SET) == -1 ||
fseek(moduleFp, (long)size_symbols + 4, SEEK_SET) == -1)
seek_err();
visitModules(scanModule);
}
/**************************************************************************
23 scanModule ok++ (PMO)
**************************************************************************/
void scanModule(int modIdx) {
moduleNeeded = false;
if (TstBit(libTable[num_lib_files], modIdx)) {
if (fseek(libraryFp, symSize, SEEK_CUR) == -1)
seek_err();
} else {
visitSymbols(chkModuleNeeded); /* m1: */
if (moduleNeeded) {
SetBit(libTable[num_lib_files], modIdx);
moduleLoaded = true; /* Not actually used */
doObjFile();
}
}
if (!moduleNeeded && fseek(moduleFp, moduleSize, SEEK_CUR) == -1)
seek_err();
}
/**************************************************************************
24 chkModuleNeeded: ok++ (PMO)
**************************************************************************/
void chkModuleNeeded(char *p1, uint8_t p2) {
register sym_t *st;
if (moduleNeeded) /* Already determined */
return;
if (p2 != 0) /* Symbol is not a global definition */
return;
/* Check if it resolves an external */
if ((st = getSymbol(p1, 0)) == 0 || (st->flags & 0xF) != SF_EXTERN)
return;
moduleNeeded = true;
}
/**************************************************************************
25 libPass2: ok++ (PMO)
**************************************************************************/
void libPass2() {
if (key_M)
printf("\n%s\n", libraryName);
if (fseek(moduleFp, (long)size_symbols + 4, SEEK_CUR) == -1)
seek_err();
visitModules(doModule);
}
/**************************************************************************
26 doModule ok++ (PMO)
**************************************************************************/
void doModule(int modIdx) {
/* Process or skip the module, based on flag set in libTable */
if (TstBit(libTable[num_lib_files], modIdx))
doObjFile();
else if (fseek(moduleFp, moduleSize, SEEK_CUR) == -1)
seek_err();
if (fseek(libraryFp, symSize, SEEK_CUR) == -1)
seek_err();
}
/**************************************************************************
27 visitModules sub_0b38h ok++ (PMO)
**************************************************************************/
void visitModules(vmfuncptr funptr) {
int cntModules;
for (cntModules = 0; cntModules != num_modules; ++cntModules) {
if (fread(libBuf, 12, 1, libraryFp) != 1)
unexp_eof();
symSize = conv_btou16(libBuf);
symCnt = conv_btou16(libBuf + 2);
moduleSize = conv_letou32(libBuf + 4);
readName(fname_obj = (char *)libBuf + 12);
funptr(cntModules);
fname_obj = 0;
}
}