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
|
{
Copyright (c) 1998-2002 by Peter Vreman
This unit implements support import,export,link routines
for the (i8086) MS-DOS target
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit t_msdos;
{$i fpcdefs.inc}
{$define USE_LINKER_WLINK}
interface
implementation
uses
SysUtils,
cutils,cfileutl,cclasses,
globtype,globals,systems,verbose,cscript,
fmodule,i_msdos,
link,cpuinfo,
aasmbase,aasmcnst,symbase,symdef,
omfbase,ogbase,ogomf,owomflib;
type
{ Borland TLINK support }
TExternalLinkerMsDosTLink=class(texternallinker)
private
Function WriteResponseFile(isdll:boolean) : Boolean;
public
constructor Create;override;
procedure SetDefaultInfo;override;
function MakeExecutable:boolean;override;
end;
{ the ALINK linker from http://alink.sourceforge.net/ }
TExternalLinkerMsDosALink=class(texternallinker)
private
Function WriteResponseFile(isdll:boolean) : Boolean;
public
constructor Create;override;
procedure SetDefaultInfo;override;
function MakeExecutable:boolean;override;
end;
{ the (Open) Watcom linker }
TExternalLinkerMsDosWLink=class(texternallinker)
private
Function WriteResponseFile(isdll:boolean) : Boolean;
Function PostProcessExecutable(const fn:string) : Boolean;
public
constructor Create;override;
procedure SetDefaultInfo;override;
function MakeExecutable:boolean;override;
end;
{ TInternalLinkerMsDos }
TInternalLinkerMsDos=class(tinternallinker)
private
function GetTotalSizeForSegmentClass(aExeOutput: TExeOutput; const SegClass: string): QWord;
protected
function GetCodeSize(aExeOutput: TExeOutput): QWord;override;
function GetDataSize(aExeOutput: TExeOutput): QWord;override;
function GetBssSize(aExeOutput: TExeOutput): QWord;override;
procedure DefaultLinkScript;override;
public
constructor create;override;
end;
{ tmsdostai_typedconstbuilder }
tmsdostai_typedconstbuilder = class(ttai_lowleveltypedconstbuilder)
protected
procedure add_link_ordered_symbol(sym: tasmsymbol; const secname: TSymStr); override;
public
class function get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; options: ttcasmlistoptions; out secname: TSymStr): boolean; override;
class function is_smartlink_vectorized_dead_strip: boolean; override;
end;
{****************************************************************************
tmsdostai_typedconstbuilder
****************************************************************************}
procedure tmsdostai_typedconstbuilder.add_link_ordered_symbol(sym: tasmsymbol; const secname: TSymStr);
begin
if (tf_smartlink_library in target_info.flags) and is_smartlink_vectorized_dead_strip then
begin
with current_module.linkorderedsymbols do
if (Last=nil) or (TCmdStrListItem(Last).Str<>secname) then
current_module.linkorderedsymbols.concat(secname);
end;
end;
class function tmsdostai_typedconstbuilder.get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; options: ttcasmlistoptions; out secname: TSymStr): boolean;
begin
result:=(tf_smartlink_library in target_info.flags) and is_smartlink_vectorized_dead_strip;
if not result then
exit;
if tcalo_vectorized_dead_strip_start in options then
secname:='1_START'
else if tcalo_vectorized_dead_strip_item in options then
secname:='2_ITEM'
else if tcalo_vectorized_dead_strip_end in options then
secname:='3_END'
else
secname:='4_INV';
secname:=make_mangledname(basename,st,secname);
end;
class function tmsdostai_typedconstbuilder.is_smartlink_vectorized_dead_strip: boolean;
begin
{$ifdef USE_LINKER_WLINK}
result:=inherited or (tf_smartlink_library in target_info.flags);
{$else}
result:=inherited and not (cs_link_extern in current_settings.globalswitches);
{$endif USE_LINKER_WLINK}
end;
{****************************************************************************
TExternalLinkerMsDosTLink
****************************************************************************}
Constructor TExternalLinkerMsDosTLink.Create;
begin
Inherited Create;
{ allow duplicated libs (PM) }
SharedLibFiles.doubles:=true;
StaticLibFiles.doubles:=true;
end;
procedure TExternalLinkerMsDosTLink.SetDefaultInfo;
begin
with Info do
begin
ExeCmd[1]:='tlink $OPT $RES';
end;
end;
Function TExternalLinkerMsDosTLink.WriteResponseFile(isdll:boolean) : Boolean;
Var
linkres : TLinkRes;
s : string;
begin
WriteResponseFile:=False;
{ Open link.res file }
LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
{ Add all options to link.res instead of passing them via command line:
DOS command line is limited to 126 characters! }
{ add objectfiles, start with prt0 always }
LinkRes.Add(GetShortName(FindObjectFile('prt0','',false)) + ' +');
while not ObjectFiles.Empty do
begin
s:=ObjectFiles.GetFirst;
if s<>'' then
LinkRes.Add(GetShortName(s) + ' +');
end;
LinkRes.Add(', ' + maybequoted(current_module.exefilename));
{ Write and Close response }
linkres.writetodisk;
LinkRes.Free;
WriteResponseFile:=True;
end;
function TExternalLinkerMsDosTLink.MakeExecutable:boolean;
var
binstr,
cmdstr : TCmdStr;
success : boolean;
begin
if not(cs_link_nolink in current_settings.globalswitches) then
Message1(exec_i_linking,current_module.exefilename);
{ Write used files and libraries and our own tlink script }
WriteResponsefile(false);
{ Call linker }
SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
Replace(cmdstr,'$OPT',Info.ExtraOptions);
success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
{ Remove ReponseFile }
if (success) and not(cs_link_nolink in current_settings.globalswitches) then
DeleteFile(outputexedir+Info.ResName);
MakeExecutable:=success; { otherwise a recursive call to link method }
end;
{****************************************************************************
TExternalLinkerMsDosALink
****************************************************************************}
{ TExternalLinkerMsDosALink }
function TExternalLinkerMsDosALink.WriteResponseFile(isdll: boolean): Boolean;
Var
linkres : TLinkRes;
s : string;
begin
WriteResponseFile:=False;
{ Open link.res file }
LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
{ Add all options to link.res instead of passing them via command line:
DOS command line is limited to 126 characters! }
{ add objectfiles, start with prt0 always }
LinkRes.Add(maybequoted(FindObjectFile('prt0','',false)));
while not ObjectFiles.Empty do
begin
s:=ObjectFiles.GetFirst;
if s<>'' then
LinkRes.Add(maybequoted(s));
end;
LinkRes.Add('-oEXE');
LinkRes.Add('-o ' + maybequoted(current_module.exefilename));
{ Write and Close response }
linkres.writetodisk;
LinkRes.Free;
WriteResponseFile:=True;
end;
constructor TExternalLinkerMsDosALink.Create;
begin
Inherited Create;
{ allow duplicated libs (PM) }
SharedLibFiles.doubles:=true;
StaticLibFiles.doubles:=true;
end;
procedure TExternalLinkerMsDosALink.SetDefaultInfo;
begin
with Info do
begin
ExeCmd[1]:='alink $OPT $RES';
end;
end;
function TExternalLinkerMsDosALink.MakeExecutable: boolean;
var
binstr,
cmdstr : TCmdStr;
success : boolean;
begin
if not(cs_link_nolink in current_settings.globalswitches) then
Message1(exec_i_linking,current_module.exefilename);
{ Write used files and libraries and our own tlink script }
WriteResponsefile(false);
{ Call linker }
SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
Replace(cmdstr,'$OPT',Info.ExtraOptions);
success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
{ Remove ReponseFile }
if (success) and not(cs_link_nolink in current_settings.globalswitches) then
DeleteFile(outputexedir+Info.ResName);
MakeExecutable:=success; { otherwise a recursive call to link method }
end;
{****************************************************************************
TExternalLinkerMsDosWLink
****************************************************************************}
{ TExternalLinkerMsDosWLink }
function TExternalLinkerMsDosWLink.WriteResponseFile(isdll: boolean): Boolean;
Var
linkres : TLinkRes;
s : string;
begin
WriteResponseFile:=False;
{ Open link.res file }
LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
{ Add all options to link.res instead of passing them via command line:
DOS command line is limited to 126 characters! }
LinkRes.Add('option quiet');
if cs_debuginfo in current_settings.moduleswitches then
begin
if target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
LinkRes.Add('debug dwarf')
else if target_dbg.id=dbg_codeview then
LinkRes.Add('debug codeview')
else
LinkRes.Add('debug watcom all');
if cs_link_separate_dbg_file in current_settings.globalswitches then
LinkRes.Add('option symfile');
end;
{ add objectfiles, start with prt0 always }
case current_settings.x86memorymodel of
mm_tiny: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0t','',false)));
mm_small: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0s','',false)));
mm_medium: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0m','',false)));
mm_compact: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0c','',false)));
mm_large: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0l','',false)));
mm_huge: LinkRes.Add('file ' + maybequoted(FindObjectFile('prt0h','',false)));
end;
while not ObjectFiles.Empty do
begin
s:=ObjectFiles.GetFirst;
if s<>'' then
LinkRes.Add('file ' + maybequoted(s));
end;
while not StaticLibFiles.Empty do
begin
s:=StaticLibFiles.GetFirst;
if s<>'' then
LinkRes.Add('library '+MaybeQuoted(s));
end;
if apptype=app_com then
LinkRes.Add('format dos com')
else
LinkRes.Add('format dos');
if current_settings.x86memorymodel=mm_tiny then
LinkRes.Add('order clname CODE clname DATA clname BSS')
else
LinkRes.Add('order clname CODE clname FAR_DATA clname BEGDATA segment _NULL segment _AFTERNULL clname DATA clname BSS clname STACK clname HEAP');
if (cs_link_map in current_settings.globalswitches) then
LinkRes.Add('option map='+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
LinkRes.Add('name ' + maybequoted(current_module.exefilename));
{ Write and Close response }
linkres.writetodisk;
LinkRes.Free;
WriteResponseFile:=True;
end;
constructor TExternalLinkerMsDosWLink.Create;
begin
Inherited Create;
{ allow duplicated libs (PM) }
SharedLibFiles.doubles:=true;
StaticLibFiles.doubles:=true;
end;
procedure TExternalLinkerMsDosWLink.SetDefaultInfo;
begin
with Info do
begin
ExeCmd[1]:='wlink $OPT $RES';
end;
end;
function TExternalLinkerMsDosWLink.MakeExecutable: boolean;
var
binstr,
cmdstr : TCmdStr;
success : boolean;
begin
if not(cs_link_nolink in current_settings.globalswitches) then
Message1(exec_i_linking,current_module.exefilename);
{ Write used files and libraries and our own tlink script }
WriteResponsefile(false);
{ Call linker }
SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
Replace(cmdstr,'$OPT',Info.ExtraOptions);
success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
{ Post process }
if success then
success:=PostProcessExecutable(current_module.exefilename);
{ Remove ReponseFile }
if (success) and not(cs_link_nolink in current_settings.globalswitches) then
DeleteFile(outputexedir+Info.ResName);
MakeExecutable:=success; { otherwise a recursive call to link method }
end;
{ In far data memory models, this function sets the MaxAlloc value in the DOS MZ
header according to the difference between HeapMin and HeapMax. We have to do
this manually, because WLink sets MaxAlloc to $FFFF and there seems to be no
way to specify a different value with a linker option. }
function TExternalLinkerMsDosWLink.PostProcessExecutable(const fn: string): Boolean;
var
f: file;
minalloc,maxalloc: Word;
heapmin_paragraphs, heapmax_paragraphs: Integer;
begin
{ nothing to do in the near data memory models }
if current_settings.x86memorymodel in x86_near_data_models then
exit(true);
{ .COM files are not supported in the far data memory models }
if apptype=app_com then
internalerror(2014062501);
{ open file }
assign(f,fn);
{$push}{$I-}
reset(f,1);
if ioresult<>0 then
Message1(execinfo_f_cant_open_executable,fn);
{ read minalloc }
seek(f,$A);
BlockRead(f,minalloc,2);
if source_info.endian<>target_info.endian then
minalloc:=SwapEndian(minalloc);
{ calculate the additional number of paragraphs needed }
heapmin_paragraphs:=(heapsize + 15) div 16;
heapmax_paragraphs:=(maxheapsize + 15) div 16;
maxalloc:=min(minalloc-heapmin_paragraphs+heapmax_paragraphs,$FFFF);
{ write maxalloc }
seek(f,$C);
if source_info.endian<>target_info.endian then
maxalloc:=SwapEndian(maxalloc);
BlockWrite(f,maxalloc,2);
close(f);
{$pop}
if ioresult<>0 then;
Result:=true;
end;
{****************************************************************************
TInternalLinkerMsDos
****************************************************************************}
function TInternalLinkerMsDos.GetTotalSizeForSegmentClass(
aExeOutput: TExeOutput; const SegClass: string): QWord;
var
objseclist: TFPObjectList;
objsec: TOmfObjSection;
i: Integer;
begin
Result:=0;
objseclist:=TMZExeOutput(aExeOutput).MZFlatContentSection.ObjSectionList;
for i:=0 to objseclist.Count-1 do
begin
objsec:=TOmfObjSection(objseclist[i]);
if objsec.ClassName=SegClass then
Inc(Result,objsec.Size);
end;
end;
function TInternalLinkerMsDos.GetCodeSize(aExeOutput: TExeOutput): QWord;
begin
Result:=GetTotalSizeForSegmentClass(aExeOutput,'CODE');
end;
function TInternalLinkerMsDos.GetDataSize(aExeOutput: TExeOutput): QWord;
begin
Result:=GetTotalSizeForSegmentClass(aExeOutput,'DATA')+
GetTotalSizeForSegmentClass(aExeOutput,'FAR_DATA');
end;
function TInternalLinkerMsDos.GetBssSize(aExeOutput: TExeOutput): QWord;
begin
Result:=GetTotalSizeForSegmentClass(aExeOutput,'BSS');
end;
procedure TInternalLinkerMsDos.DefaultLinkScript;
var
s: TCmdStr;
begin
{ add objectfiles, start with prt0 always }
case current_settings.x86memorymodel of
mm_tiny: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0t','',false)));
mm_small: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0s','',false)));
mm_medium: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0m','',false)));
mm_compact: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0c','',false)));
mm_large: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0l','',false)));
mm_huge: LinkScript.Concat('READOBJECT ' + maybequoted(FindObjectFile('prt0h','',false)));
end;
while not ObjectFiles.Empty do
begin
s:=ObjectFiles.GetFirst;
if s<>'' then
LinkScript.Concat('READOBJECT ' + maybequoted(s));
end;
LinkScript.Concat('GROUP');
while not StaticLibFiles.Empty do
begin
s:=StaticLibFiles.GetFirst;
if s<>'' then
LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s));
end;
LinkScript.Concat('ENDGROUP');
LinkScript.Concat('EXESECTION .MZ_flat_content');
if current_settings.x86memorymodel=mm_tiny then
begin
LinkScript.Concat(' OBJSECTION _TEXT||CODE');
LinkScript.Concat(' OBJSECTION *||CODE');
LinkScript.Concat(' OBJSECTION *||DATA');
LinkScript.Concat(' SYMBOL _edata');
LinkScript.Concat(' OBJSECTION *||BSS');
LinkScript.Concat(' SYMBOL _end');
end
else
begin
LinkScript.Concat(' OBJSECTION _TEXT||CODE');
LinkScript.Concat(' OBJSECTION *||CODE');
LinkScript.Concat(' OBJSECTION *||FAR_DATA');
LinkScript.Concat(' OBJSECTION _NULL||BEGDATA');
LinkScript.Concat(' OBJSECTION _AFTERNULL||BEGDATA');
LinkScript.Concat(' OBJSECTION *||BEGDATA');
LinkScript.Concat(' OBJSECTION *||DATA');
LinkScript.Concat(' SYMBOL _edata');
LinkScript.Concat(' OBJSECTION *||BSS');
LinkScript.Concat(' SYMBOL _end');
LinkScript.Concat(' OBJSECTION *||STACK');
LinkScript.Concat(' OBJSECTION *||HEAP');
end;
LinkScript.Concat('ENDEXESECTION');
if (cs_debuginfo in current_settings.moduleswitches) and
(target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4]) then
begin
LinkScript.Concat('EXESECTION .debug_info');
LinkScript.Concat(' OBJSECTION .DEBUG_INFO||DWARF');
LinkScript.Concat('ENDEXESECTION');
LinkScript.Concat('EXESECTION .debug_abbrev');
LinkScript.Concat(' OBJSECTION .DEBUG_ABBREV||DWARF');
LinkScript.Concat('ENDEXESECTION');
LinkScript.Concat('EXESECTION .debug_line');
LinkScript.Concat(' OBJSECTION .DEBUG_LINE||DWARF');
LinkScript.Concat('ENDEXESECTION');
LinkScript.Concat('EXESECTION .debug_aranges');
LinkScript.Concat(' OBJSECTION .DEBUG_ARANGES||DWARF');
LinkScript.Concat('ENDEXESECTION');
end;
LinkScript.Concat('ENTRYNAME ..start');
end;
constructor TInternalLinkerMsDos.create;
begin
inherited create;
CArObjectReader:=TOmfLibObjectReader;
CExeOutput:=TMZExeOutput;
CObjInput:=TOmfObjInput;
end;
{*****************************************************************************
Initialize
*****************************************************************************}
initialization
ctai_typedconstbuilder:=tmsdostai_typedconstbuilder;
RegisterLinker(ld_int_msdos,TInternalLinkerMsDos);
{$if defined(USE_LINKER_TLINK)}
RegisterLinker(ld_msdos,TExternalLinkerMsDosTLink);
{$elseif defined(USE_LINKER_ALINK)}
RegisterLinker(ld_msdos,TExternalLinkerMsDosALink);
{$elseif defined(USE_LINKER_WLINK)}
RegisterLinker(ld_msdos,TExternalLinkerMsDosWLink);
{$else}
{$fatal no linker defined}
{$endif}
RegisterTarget(system_i8086_msdos_info);
end.
|