-
Notifications
You must be signed in to change notification settings - Fork 0
/
l80.c
297 lines (239 loc) · 6.38 KB
/
l80.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright (c) 2021 Brian Callahan <bcallah@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BIN_MAX 0xff00
/*
* Struct holding name and matching calculating addresses.
*/
typedef struct collection
{
char *symbol;
unsigned int addr;
struct collection *next;
} collection_t;
static collection_t *head;
/*
* Error handling.
*/
static int
error(const char *fmt, ...)
{
va_list ap;
(void) fputs("ld: error: ", stderr);
va_start(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
(void) fputc('\n', stderr);
return 1;
}
/*
* Pass 1: Collect symbol names.
*/
static int
collect1(int argc, char *argv[])
{
FILE *fp;
collection_t *curr, *new;
char symbol[16];
int ch, i, j;
unsigned long addr = 0x100;
for (i = 2; i < argc; i++) {
if ((fp = fopen(argv[i], "r")) == NULL)
return error("could not open input file: %s", argv[i]);
while ((ch = fgetc(fp)) != EOF) {
switch (ch) {
case '\0':
if ((ch = fgetc(fp)) == EOF)
return error("invalid data byte");
/* Increment address counter. */
if (++addr > USHRT_MAX)
return error("final binary exceeds 65,280 bytes");
break;
case '\001':
/* Get symbol name. */
j = 0;
(void) memset(symbol, 0, sizeof(symbol));
while ((ch = fgetc(fp)) != '\001') {
if (ch == EOF)
return error("unterminated symbol");
if (j < sizeof(symbol) - 1)
symbol[j++] = ch;
}
/* Make sure there is actually a symbol here. */
if (strlen(symbol) == 0)
return error("empty symbol");
/* Make sure this symbol isn't a duplicate. */
curr = head;
while (curr != NULL) {
if (!strcmp(symbol, curr->symbol))
return error("duplicate symbol: %s", symbol);
curr = curr->next;
}
/* Put symbol in collection. */
curr = head;
while (curr->next != NULL)
curr = curr->next;
if ((new = malloc(sizeof(collection_t))) == NULL)
return error("could not create symbol entry");
new->symbol = strdup(symbol);
new->addr = addr;
new->next = NULL;
curr->next = new;
break;
case '\002':
/* Ignore references during pass 1. */
while ((ch = fgetc(fp)) != '\002') {
if (ch == EOF)
return error("unterminated symbol");
}
/* But still increment address counter. */
addr += 2;
if (addr > USHRT_MAX)
return error("final binary exceeds 65,280 bytes");
break;
case '\003':
/* Do nothing for now. */
break;
case '\032':
/* CP/M EOF. */
return 0;
default:
/* This should never happen. */
return error("unknown control byte: %d", ch);
}
}
(void) fclose(fp);
}
return 0;
}
static int
process2(int argc, char *argv[], FILE *fq)
{
FILE *fp;
collection_t *curr;
char symbol[16];
int ch, found, i, j;
unsigned int bin = 0;
for (i = 2; i < argc; i++) {
if ((fp = fopen(argv[i], "r")) == NULL)
return error("could not open input file: %s", argv[i]);
while ((ch = fgetc(fp)) != EOF) {
switch (ch) {
case '\0':
/* Write out data byte. */
if ((ch = fgetc(fp)) == EOF)
return error("invalid data byte");
(void) fputc(ch, fq);
if (bin++ == BIN_MAX)
return error("binary too large");
break;
case '\001':
/* Ignore declarations during pass 2. */
while ((ch = fgetc(fp)) != '\001') {
if (ch == EOF)
return error("unterminated delcaration");
}
break;
case '\002':
/* Get and write out symbol reference. */
curr = head;
found = 0;
j = 0;
(void) memset(symbol, 0, sizeof(symbol));
while ((ch = fgetc(fp)) != '\002') {
if (ch == EOF)
return error("unterminated reference");
if (j < sizeof(symbol) - 1)
symbol[j++] = ch;
}
while (curr != NULL) {
if (!strcmp(symbol, curr->symbol)) {
found = 1;
break;
}
curr = curr->next;
}
if (found == 0)
return error("undefined reference: %s", symbol);
(void) fputc(curr->addr & 0xff, fq);
if (bin++ == BIN_MAX)
return error("binary too large");
(void) fputc((curr->addr >> 8) & 0xff, fq);
if (bin++ == BIN_MAX)
return error("binary too large");
break;
case '\003':
/* Do nothing for now. */
break;
case '\032':
/* CP/M EOF. */
return 0;
default:
/* This should never happen. */
return error("unknown control byte: %d", ch);
}
}
(void) fclose(fp);
}
return 0;
}
int
main(int argc, char *argv[])
{
FILE *fp;
collection_t *curr;
char *ending;
char bin[13]; /* 8 + 3 + DOT */
int i, ret;
/* Make sure we have the correct number of arguments. */
if (argc < 3) {
fprintf(stderr, "usage: ld binary file1.obj [file2.obj ...]\n");
return 1;
}
/* Check endings of all input files. */
for (i = 2; i < argc; i++) {
ending = strrchr(argv[i], '.');
if (strcmp(ending, ".obj") && strcmp(ending, ".OBJ")) {
if (strcmp(ending, ".lib") && strcmp(ending, ".LIB"))
return error("input files must end in \".obj\" or \".lib\"");
}
}
(void) memset(bin, 0, sizeof(bin));
(void) strncpy(bin, argv[1], sizeof(bin) - 1);
if (strlen(bin) > 8)
return error("output name \"%s.com\" too long", argv[1]);
(void) strncat(bin, ".com", sizeof(bin) - 1);
/* Preload collection with initial empty symbol. */
if ((curr = malloc(sizeof(collection_t))) == NULL)
return error("could not add symbol");
curr->symbol = "@";
curr->addr = 0;
curr->next = NULL;
head = curr;
if ((fp = fopen(bin, "w+")) == NULL)
return error("could not open output file: %s", bin);
if ((ret = collect1(argc, argv)) == 0)
ret = process2(argc, argv, fp);
(void) fclose(fp);
if (ret != 0)
(void) unlink(bin);
return ret;
}