123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- {
- 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,script,
- fmodule,i_msdos,
- link,aasmbase,cpuinfo;
- 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;
- {****************************************************************************
- 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 paratargetdbg in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
- LinkRes.Add('debug dwarf');
- { 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 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;
- {*****************************************************************************
- Initialize
- *****************************************************************************}
- initialization
- {$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.
|