forked from Macroassembler-AS/asl-releases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalink.c
643 lines (524 loc) · 16.3 KB
/
alink.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/* alink.c */
/****************************************************************************/
/* AS-Portierung */
/* */
/* Linking of AS Code Files */
/* */
/* History: 2. 7.2000 begun */
/* 4. 7.2000 read symbols */
/* 6. 7.2000 start real relocation */
/* 7. 7.2000 simple relocations */
/* 30.10.2000 added 1-byte relocations, verbosity levels */
/* 14. 1.2001 silenced warnings about unused parameters */
/* */
/****************************************************************************/
#include "stdinc.h"
#include <string.h>
#include <ctype.h>
#include "version.h"
#include "endian.h"
#include "bpemu.h"
#include "strutil.h"
#include "nls.h"
#include "nlmessages.h"
#include "stringlists.h"
#include "cmdarg.h"
#include "toolutils.h"
#include "ioerrs.h"
#include "alink.rsc"
/****************************************************************************/
/* Macros */
/****************************************************************************/
/* Types */
typedef struct sPart
{
struct sPart *Next;
int FileNum, RecNum;
LargeWord CodeStart, CodeLen;
Byte Gran, Segment;
Boolean MustReloc;
PRelocInfo RelocInfo;
} TPart, *PPart;
/****************************************************************************/
/* Variables */
static Boolean DoubleErr;
static int Verbose;
static LongWord UndefErr;
static String TargName;
static CMDProcessed ParUnprocessed;
static PPart PartList, PartLast;
static FILE *TargFile;
static Byte *Buffer;
LongInt BufferSize;
static LargeWord SegStarts[PCMax + 1];
/****************************************************************************/
/* increase buffer if necessary */
static void BumpBuffer(LongInt MinSize)
{
if (MinSize > BufferSize)
{
Buffer = (Byte*) ((BufferSize == 0) ? malloc(sizeof(Byte) * MinSize)
: realloc(Buffer, sizeof(Byte) * MinSize));
BufferSize = MinSize;
}
}
/****************************************************************************/
/* reading/patching in buffer */
static LargeWord GetValue(LongInt Type, LargeWord Offset)
{
switch (Type & ~(RelocFlagSUB | RelocFlagPage))
{
case RelocTypeL8:
return MRead1L(Buffer + Offset);
case RelocTypeL16:
return MRead2L(Buffer + Offset);
case RelocTypeB16:
return MRead2B(Buffer + Offset);
case RelocTypeL32:
return MRead4L(Buffer + Offset);
case RelocTypeB32:
return MRead4B(Buffer + Offset);
#ifdef HAS64
case RelocTypeL64:
return MRead8L(Buffer + Offset);
case RelocTypeB64:
return MRead8B(Buffer + Offset);
#endif
}
fprintf(stderr, "unknown relocation type: 0x");
fprintf(stderr, LongIntFormat, Type);
fprintf(stderr, "\n");
exit(3);
}
static void PutValue(LargeWord Value, LongInt Type, LargeWord Offset)
{
switch (Type & ~(RelocFlagSUB | RelocFlagPage))
{
case RelocTypeL8:
MWrite1L(Buffer + Offset, Value);
break;
case RelocTypeL16:
MWrite2L(Buffer + Offset, Value);
break;
case RelocTypeB16:
MWrite2B(Buffer + Offset, Value);
break;
case RelocTypeL32:
MWrite4L(Buffer + Offset, Value);
break;
case RelocTypeB32:
MWrite4B(Buffer + Offset, Value);
break;
#ifdef HAS64
case RelocTypeL64:
MWrite8L(Buffer + Offset, Value);
break;
case RelocTypeB64:
MWrite8B(Buffer + Offset, Value);
break;
#endif
default:
fprintf(stderr, "unknown relocation type: 0x");
fprintf(stderr, LongIntFormat, Type);
fprintf(stderr, "\n");
exit(3);
}
}
/****************************************************************************/
/* get the value of an exported symbol */
static Boolean GetExport(char *Name, LargeWord *Result)
{
PPart PartRun;
LongInt z;
for (PartRun = PartList; PartRun; PartRun = PartRun->Next)
for (z = 0; z < PartRun->RelocInfo->ExportCount; z++)
if (!strcmp(Name, PartRun->RelocInfo->ExportEntries[z].Name))
{
*Result = PartRun->RelocInfo->ExportEntries[z].Value;
return True;
}
return False;
}
/****************************************************************************/
/* read the symbol exports/relocations from a file */
static void ReadSymbols(int Index)
{
FILE *f;
String SrcName;
Byte Header, CPU, Gran, Segment;
PPart PNew;
LongWord Addr;
Word Len;
int cnt;
/* open this file - we're only reading */
strmaxcpy(SrcName, ParamStr[Index], 255);
DelSuffix(SrcName); AddSuffix(SrcName, getmessage(Num_Suffix));
if (Verbose >= 2)
printf("%s '%s'...\n", getmessage(Num_InfoMsgGetSyms), SrcName);
f = fopen(SrcName, OPENRDMODE);
if (!f)
ChkIO(SrcName);
/* check magic */
if (!Read2(f, &Len)) ChkIO(SrcName);
if (Len != FileMagic)
FormatError(SrcName, getmessage(Num_FormatInvHeaderMsg));
/* step through records */
cnt = 0;
while (!feof(f))
{
/* read a record's header */
ReadRecordHeader(&Header, &CPU, &Segment, &Gran, SrcName, f);
/* for absolute records, only store address position */
/* for relocatable records, also read following relocation record */
if ((Header == FileHeaderDataRec) || (Header == FileHeaderRDataRec)
|| (Header == FileHeaderRelocRec) || (Header == FileHeaderRRelocRec))
{
/* build up record */
PNew = (PPart) malloc(sizeof(TPart));
PNew->Next = NULL;
PNew->FileNum = Index;
PNew->RecNum = cnt++;
PNew->Gran = Gran;
PNew->Segment = Segment;
if (!Read4(f, &Addr))
ChkIO(SrcName);
PNew->CodeStart = Addr;
if (!Read2(f, &Len))
ChkIO(SrcName);
PNew->CodeLen = Len;
PNew->MustReloc = ((Header == FileHeaderRelocRec)
|| (Header == FileHeaderRRelocRec));
/* skip code */
if (fseek(f, Len, SEEK_CUR) != 0)
ChkIO(SrcName);
/* relocatable record must be followed by relocation data */
if ((Header == FileHeaderRDataRec) || (Header == FileHeaderRRelocRec))
{
LongInt z;
LargeWord Dummy;
ReadRecordHeader(&Header, &CPU, &Segment, &Gran, SrcName, f);
if (Header != FileHeaderRelocInfo)
FormatError(SrcName, getmessage(Num_FormatRelocInfoMissing));
PNew->RelocInfo = ReadRelocInfo(f);
if (!PNew->RelocInfo)
ChkIO(SrcName);
/* check for double-defined symbols */
for (z = 0; z < PNew->RelocInfo->ExportCount; z++)
if (GetExport(PNew->RelocInfo->ExportEntries[z].Name, &Dummy))
{
fprintf(stderr, "%s: %s '%s'\n",
SrcName, getmessage(Num_DoubleDefSymbol),
PNew->RelocInfo->ExportEntries[z].Name);
DoubleErr = True;
}
}
else
PNew->RelocInfo = NULL;
/* put into list */
if (!PartList)
PartList = PNew;
else
PartLast->Next = PNew;
PartLast = PNew;
}
/* end of file ? */
else if (Header == FileHeaderEnd)
break;
/* skip everything else */
else
SkipRecord(Header, SrcName, f);
}
/* close again */
fclose(f);
}
/****************************************************************************/
/* do the relocation */
static void ProcessFile(int Index)
{
FILE *f;
String SrcName;
PPart PartRun;
Byte Header, CPU, Gran, Segment;
LongInt Addr, z;
LargeWord Value, RelocVal, NRelocVal;
Word Len, Magic;
LongWord SumLen;
PRelocEntry PReloc;
Boolean UndefFlag, Found;
/* open this file - we're only reading */
strmaxcpy(SrcName, ParamStr[Index], 255);
DelSuffix(SrcName); AddSuffix(SrcName, getmessage(Num_Suffix));
if (Verbose >= 2)
printf("%s '%s'...", getmessage(Num_InfoMsgOpenSrc), SrcName);
else if (Verbose >= 1)
printf("%s", SrcName);
f = fopen(SrcName, OPENRDMODE);
if (!f)
ChkIO(SrcName);
/* check magic */
if (!Read2(f, &Magic))
ChkIO(SrcName);
if (Magic != FileMagic)
FormatError(SrcName, getmessage(Num_FormatInvHeaderMsg));
/* due to the way we built the part list, all parts of a file are
sequentially in the list. Therefore we only have to look for
the first part of this file in the list and know the remainders
will follow sequentially. */
for (PartRun = PartList; PartRun; PartRun = PartRun->Next)
if (PartRun->FileNum >= Index)
break;
/* now step through the records */
SumLen = 0;
while (!feof(f))
{
/* get header */
ReadRecordHeader(&Header, &CPU, &Segment, &Gran, SrcName, f);
/* records without relocation info do not need any processing - just
pass them through */
if ((Header == FileHeaderDataRec) || (Header == FileHeaderRelocRec))
{
if (!Read4(f, &Addr))
ChkIO(SrcName);
if (!Read2(f, &Len))
ChkIO(SrcName);
BumpBuffer(Len);
if (fread(Buffer, 1, Len, f) != Len)
ChkIO(SrcName);
WriteRecordHeader(&Header, &CPU, &Segment, &Gran, TargName, TargFile);
if (!Write4(TargFile, &Addr))
ChkIO(TargName);
if (!Write2(TargFile, &Len))
ChkIO(TargName);
if (fwrite(Buffer, 1, Len, TargFile) != Len)
ChkIO(TargName);
if (PartRun)
PartRun = PartRun->Next;
SumLen += Len;
}
/* records with relocation: basically the same, plus the real work...
the appended relocation info will be skipped in the next loop run. */
else if ((Header == FileHeaderRDataRec) || (Header == FileHeaderRRelocRec))
{
if (!Read4(f, &Addr))
ChkIO(SrcName);
if (!Read2(f, &Len))
ChkIO(SrcName);
BumpBuffer(Len);
if (fread(Buffer, 1, Len, f) != Len)
ChkIO(SrcName);
UndefFlag = False;
for (z = 0; z < PartRun->RelocInfo->RelocCount; z++)
{
PReloc = PartRun->RelocInfo->RelocEntries + z;
Found = True;
if (!strcmp(PReloc->Name, RelName_SegStart))
Value = PartRun->CodeStart;
else
Found = GetExport(PReloc->Name, &Value);
if (Found)
{
if (Verbose >= 2)
printf("%s 0x%x...", getmessage(Num_InfoMsgReading), (int)PReloc->Addr);
RelocVal = GetValue(PReloc->Type, PReloc->Addr - PartRun->CodeStart);
NRelocVal = (PReloc->Type & RelocFlagSUB) ? RelocVal - Value : RelocVal + Value;
PutValue(NRelocVal, PReloc->Type, PReloc->Addr - PartRun->CodeStart);
}
else
{
fprintf(stderr, "%s: %s(%s)\n", getmessage(Num_UndefSymbol),
PReloc->Name, SrcName);
UndefFlag = True;
UndefErr++;
}
}
if (!UndefFlag)
{
Header = FileHeaderDataRec;
WriteRecordHeader(&Header, &CPU, &Segment, &Gran, TargName, TargFile);
Addr = PartRun->CodeStart;
if (!Write4(TargFile, &Addr))
ChkIO(TargName);
if (!Write2(TargFile, &Len))
ChkIO(TargName);
if (fwrite(Buffer, 1, Len, TargFile) != Len)
ChkIO(TargName);
if (PartRun)
PartRun = PartRun->Next;
}
SumLen += Len;
}
/* all done? */
else if (Header == FileHeaderEnd)
break;
else
SkipRecord(Header, SrcName, f);
}
if (Verbose >= 1)
{
printf("(");
printf(Integ32Format, SumLen);
printf(" %s)\n", getmessage((SumLen == 1) ? Num_Byte : Num_Bytes));
}
}
/****************************************************************************/
/* command line processing */
static CMDResult CMD_Verbose(Boolean Negate, const char *Arg)
{
UNUSED(Arg);
if (Negate)
{
if (Verbose)
Verbose--;
}
else
Verbose++;
return CMDOK;
}
static void ParamError(Boolean InEnv, char *Arg)
{
printf("%s%s\n%s\n",
getmessage((InEnv) ? Num_ErrMsgInvEnvParam : Num_ErrMsgInvParam),
Arg, getmessage(Num_ErrMsgProgTerm));
exit(1);
}
#define ALINKParamCnt (sizeof(ALINKParams) / sizeof(*ALINKParams))
static CMDRec ALINKParams[] =
{
{"v", CMD_Verbose}
};
/****************************************************************************/
int main(int argc, char **argv)
{
String Ver;
int z;
Word LMagic;
Byte LHeader;
PPart PartRun;
LargeInt Diff;
/* save command line arguments for processing */
ParamStr = argv;
ParamCount = argc - 1;
/* the initialization orgy... */
nls_init();
NLS_Initialize();
endian_init();
bpemu_init();
strutil_init();
Buffer = NULL;
BufferSize = 0;
/* open message catalog */
nlmessages_init("alink.msg", *argv, MsgId1, MsgId2); ioerrs_init(*argv);
cmdarg_init(*argv);
toolutils_init(*argv);
sprintf(Ver,"ALINK/C V%s",Version);
WrCopyRight(Ver);
/* no commandline arguments -->print help */
if (ParamCount == 0)
{
char *ph1, *ph2;
errno = 0;
printf("%s%s%s\n", getmessage(Num_InfoMessHead1), GetEXEName(),
getmessage(Num_InfoMessHead2));
ChkIO(OutName);
for (ph1 = getmessage(Num_InfoMessHelp), ph2 = strchr(ph1,'\n'); ph2; ph1 = ph2+1, ph2 = strchr(ph1,'\n'))
{
*ph2 = '\0';
printf("%s\n", ph1);
*ph2 = '\n';
}
exit(1);
}
/* preinit commandline variables */
Verbose = 0;
/* process arguments */
ProcessCMD(ALINKParams, ALINKParamCnt, ParUnprocessed, "ALINKCMD", ParamError);
if ((Verbose >= 1) && (ParamCount != 0))
printf("\n");
/* extract target file */
if (ProcessedEmpty(ParUnprocessed))
{
errno = 0;
printf("%s\n", getmessage(Num_ErrMsgTargMissing));
ChkIO(OutName);
exit(1);
}
for (z = ParamCount; z > 0; z--)
if (ParUnprocessed[z])
break;
strmaxcpy(TargName, ParamStr[z], 255);
DelSuffix(TargName);
AddSuffix(TargName, getmessage(Num_Suffix));
ParUnprocessed[z] = False;
/* walk over source file(s): */
if (ProcessedEmpty(ParUnprocessed))
{
errno = 0;
printf("%s\n", getmessage(Num_ErrMsgSrcMissing));
ChkIO(OutName);
exit(1);
}
/* read symbol info from all files */
DoubleErr = False;
PartList = NULL;
for (z = 1; z <= ParamCount; z++)
if (ParUnprocessed[z])
ReadSymbols(z);
/* double-defined symbols? */
if (DoubleErr)
return 1;
/* arrange relocatable segments in memory, relocate global symbols */
for (PartRun = PartList; PartRun; PartRun = PartRun->Next)
if (PartRun->MustReloc)
{
Diff = SegStarts[PartRun->Segment] - PartRun->CodeStart;
PartRun->CodeStart += Diff;
if (Verbose >= 2)
printf("%s 0x%x\n", getmessage(Num_InfoMsgLocating), (int)PartRun->CodeStart);
if (PartRun->RelocInfo)
{
PExportEntry ExpRun, ExpEnd;
PRelocEntry RelRun, RelEnd;
ExpRun = PartRun->RelocInfo->ExportEntries;
ExpEnd = ExpRun + PartRun->RelocInfo->ExportCount;
for (; ExpRun < ExpEnd; ExpRun++)
if (ExpRun->Flags & RelFlag_Relative)
ExpRun->Value += Diff;
RelRun = PartRun->RelocInfo->RelocEntries;
RelEnd = RelRun + PartRun->RelocInfo->RelocCount;
for (; RelRun < RelEnd; RelRun++)
RelRun->Addr += Diff;
}
SegStarts[PartRun->Segment] += PartRun->CodeLen / PartRun->Gran;
}
/* open target file */
TargFile = fopen(TargName, OPENWRMODE);
if (!TargFile)
ChkIO(TargName);
LMagic = FileMagic;
if (!Write2(TargFile, &LMagic))
ChkIO(TargName);
/* do relocations, underwhile write target file */
UndefErr = 0;
for (z = 1; z <= ParamCount; z++)
if (ParUnprocessed[z])
ProcessFile(z);
/* write final creator record */
LHeader = FileHeaderEnd;
if (fwrite(&LHeader, 1, 1, TargFile) != 1)
ChkIO(TargName);
sprintf(Ver,"ALINK %s/%s-%s", Version, ARCHPRNAME, ARCHSYSNAME);
if (fwrite(Ver, 1, strlen(Ver), TargFile) != strlen(Ver))
ChkIO(TargName);
/* close target file and erase if undefined symbols */
fclose(TargFile);
if ((UndefErr > 0) || (Magic != 0))
unlink(TargName);
if (UndefErr > 0)
{
fprintf(stderr, "\n");
fprintf(stderr, LongIntFormat, UndefErr);
fprintf(stderr, " %s\n", getmessage((UndefErr == 1) ? Num_SumUndefSymbol : Num_SumUndefSymbols));
return 1;
}
return 0;
}