link.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. {
  2. $Id$
  3. Copyright (c) 1998 by the FPC development team
  4. This unit handles the linker and binder calls for programs and
  5. libraries
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. Unit link;
  20. Interface
  21. uses cobjects;
  22. Type
  23. TLinker = Object
  24. LinkToC, { Should we link to the C libs? }
  25. Strip : Boolean; { Strip symbols ? }
  26. ObjectFiles,
  27. SharedLibFiles,
  28. StaticLibFiles : TStringContainer;
  29. ExeName, { FileName of the exe to be created }
  30. SharedLibName,
  31. StaticLibName, { FileName of the lib to be created }
  32. LinkOptions : string; { Additional options to the linker }
  33. DynamicLinker : String[80]; { What Dynamic linker ? }
  34. LinkResName : String[32]; { Name of response file }
  35. { Methods }
  36. Constructor Init;
  37. Destructor Done;
  38. Procedure SetExeName(const s:string);
  39. Procedure SetLibName(const s:string);
  40. function FindObjectFile(s : string) : string;
  41. function FindLibraryFile(s:string;const ext:string) : string;
  42. Procedure AddObject(const S : String);
  43. Procedure AddStaticLibrary(const S : String);
  44. Procedure AddSharedLibrary(const S : String);
  45. Function FindLinker:String; { Find linker, sets Name }
  46. Function DoExec(const command,para:string;info,useshell:boolean):boolean;
  47. Function WriteResponseFile:Boolean;
  48. Function MakeExecutable:boolean;
  49. Procedure MakeStaticLibrary(const path:string);
  50. Procedure MakeSharedLibrary;
  51. end;
  52. PLinker=^TLinker;
  53. Var
  54. Linker : TLinker;
  55. Implementation
  56. uses
  57. Script,globals,systems,verbose
  58. {$ifdef linux}
  59. ,linux
  60. {$endif}
  61. ,dos;
  62. {$ifndef linux}
  63. Procedure Shell(command:string);
  64. { This is already defined in the linux.ppu for linux, need for the *
  65. expansion under linux }
  66. var
  67. comspec : string;
  68. begin
  69. comspec:=getenv('COMSPEC');
  70. Exec(comspec,' /C '+command);
  71. end;
  72. {$endif}
  73. Constructor TLinker.Init;
  74. begin
  75. ObjectFiles.Init;
  76. SharedLibFiles.Init;
  77. StaticLibFiles.Init;
  78. ObjectFiles.Doubles:=False;
  79. SharedLibFiles.Doubles:=False;
  80. StaticLibFiles.Doubles:=False;
  81. LinkToC:=False;
  82. Strip:=false;
  83. LinkOptions:='';
  84. ExeName:='';
  85. SharedLibName:='';
  86. StaticLibName:='';
  87. LibrarySearchPath:='';
  88. ObjectSearchPath:='';
  89. {$ifdef linux}
  90. DynamicLinker:='/lib/ld-linux.so.1';
  91. {$else}
  92. DynamicLinker:='';
  93. {$endif}
  94. LinkResName:='link.res';
  95. end;
  96. Destructor TLinker.Done;
  97. begin
  98. end;
  99. Procedure TLinker.SetExeName(const s:string);
  100. var
  101. path : dirstr;
  102. name : namestr;
  103. ext : extstr;
  104. begin
  105. FSplit(s,path,name,ext);
  106. ExeName:=Path+Name+target_os.ExeExt;
  107. end;
  108. Procedure TLinker.SetLibName(const s:string);
  109. var
  110. path : dirstr;
  111. name : namestr;
  112. ext : extstr;
  113. begin
  114. FSplit(s,path,name,ext);
  115. SharedLibName:=Path+Name+target_os.SharedLibExt;
  116. StaticLibName:=Path+Name+target_os.StaticLibExt;
  117. end;
  118. var
  119. LastLDBin : string;
  120. Function TLinker.FindLinker:string;
  121. var
  122. ldfound : boolean;
  123. begin
  124. if LastLDBin='' then
  125. begin
  126. LastLDBin:=FindExe(target_link.linkbin,ldfound);
  127. if (not ldfound) and (not externlink) then
  128. begin
  129. Message1(exec_w_linker_not_found,LastLDBin);
  130. externlink:=true;
  131. end;
  132. if ldfound then
  133. Message1(exec_u_using_linker,LastLDBin);
  134. end;
  135. FindLinker:=LastLDBin;
  136. end;
  137. { searches an object file }
  138. function TLinker.FindObjectFile(s:string) : string;
  139. var
  140. found : boolean;
  141. begin
  142. if pos('.',s)=0 then
  143. s:=s+target_info.objext;
  144. s:=FixFileName(s);
  145. if FileExists(s) then
  146. begin
  147. Findobjectfile:=s;
  148. exit;
  149. end;
  150. findobjectfile:=search(s,'.;'+unitsearchpath+';'+exepath,found)+s;
  151. if (not externlink) and (not found) then
  152. Message1(exec_w_objfile_not_found,s);
  153. end;
  154. { searches an library file }
  155. function TLinker.FindLibraryFile(s:string;const ext:string) : string;
  156. var
  157. found : boolean;
  158. begin
  159. if pos('.',s)=0 then
  160. s:=s+ext;
  161. s:=FixFileName(s);
  162. if FileExists(s) then
  163. begin
  164. FindLibraryFile:=s;
  165. exit;
  166. end;
  167. findlibraryfile:=search(s,'.;'+librarysearchpath+';'+exepath,found)+s;
  168. if (not externlink) and (not found) then
  169. Message1(exec_w_libfile_not_found,s);
  170. end;
  171. Procedure TLinker.AddObject(const S : String);
  172. begin
  173. ObjectFiles.Insert(FindObjectFile(s));
  174. end;
  175. Procedure TLinker.AddSharedLibrary(const S:String);
  176. begin
  177. SharedLibFiles.Insert (S);
  178. { SharedLibFiles.Insert(FindLibraryFile(s,target_os.sharedlibext)); }
  179. end;
  180. Procedure TLinker.AddStaticLibrary(const S:String);
  181. begin
  182. StaticLibFiles.Insert(FindLibraryFile(s,target_os.staticlibext));
  183. end;
  184. Function TLinker.DoExec(const command,para:string;info,useshell:boolean):boolean;
  185. begin
  186. DoExec:=true;
  187. if not externlink then
  188. begin
  189. swapvectors;
  190. if useshell then
  191. shell(command+' '+para)
  192. else
  193. exec(command,para);
  194. swapvectors;
  195. if (dosexitcode<>0) then
  196. begin
  197. Message(exec_w_error_while_linking);
  198. DoExec:=false;
  199. exit;
  200. end
  201. else
  202. if (dosError<>0) then
  203. begin
  204. Message(exec_w_cant_call_linker);
  205. ExternLink:=true;
  206. end;
  207. end;
  208. if externlink then
  209. begin
  210. if info then
  211. AsmRes.AddLinkCommand(Command,Para,ExeName)
  212. else
  213. AsmRes.AddLinkCommand(Command,Para,'');
  214. end;
  215. end;
  216. Function TLinker.WriteResponseFile : Boolean;
  217. Var
  218. LinkResponse : Text;
  219. i : longint;
  220. prtobj,s : string;
  221. begin
  222. { Open linkresponse and write header }
  223. assign(linkresponse,inputdir+LinkResName);
  224. rewrite(linkresponse);
  225. { set special options for some targets }
  226. prtobj:='prt0';
  227. case target_info.target of
  228. target_Win32 : prtobj:='';
  229. target_linux : begin
  230. if cs_profile in aktswitches then
  231. begin
  232. prtobj:='gprt0';
  233. AddSharedLibrary('gmon');
  234. AddSharedLibrary('c');
  235. end;
  236. end;
  237. end;
  238. writeln(linkresponse,target_link.inputstart);
  239. { add objectfiles, start with prt0 always }
  240. if prtobj<>'' then
  241. Writeln(linkresponse,FindObjectFile(prtobj));
  242. while not ObjectFiles.Empty do
  243. begin
  244. s:=ObjectFiles.Get;
  245. if s<>'' then
  246. Writeln(linkresponse,s);
  247. end;
  248. { Write sharedlibraries like -l<lib> }
  249. While not SharedLibFiles.Empty do
  250. begin
  251. S:=SharedLibFiles.Get;
  252. i:=Pos(target_os.sharedlibext,S);
  253. if i>0 then
  254. Delete(S,i,255);
  255. writeln(linkresponse,target_link.libprefix+s);
  256. end;
  257. writeln(linkresponse,target_link.inputend);
  258. { Write staticlibraries }
  259. if not StaticLibFiles.Empty then
  260. begin
  261. Writeln(LinkResponse,target_link.GroupStart);
  262. While not StaticLibFiles.Empty do
  263. begin
  264. S:=StaticLibFiles.Get;
  265. writeln(linkresponse,s)
  266. end;
  267. Writeln(LinkResponse,target_link.GroupEnd);
  268. end;
  269. { Close response }
  270. close(linkresponse);
  271. WriteResponseFile:=True;
  272. end;
  273. Function TLinker.MakeExecutable:boolean;
  274. var
  275. bindbin : string[80];
  276. bindfound : boolean;
  277. _stacksize,i,
  278. _heapsize : longint;
  279. s,s2 : string;
  280. dummy : file;
  281. success : boolean;
  282. begin
  283. {$ifdef linux}
  284. if LinkToC then
  285. begin
  286. AddObject('/usr/lib/crt0.o');
  287. AddObject('lprt');
  288. AddStaticLibrary('libc.a');
  289. AddStaticLibrary('libgcc.a');
  290. end;
  291. {$endif Linux}
  292. If not SharedLibFiles.Empty then
  293. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  294. if Strip then
  295. LinkOptions:=LinkOptions+target_link.stripopt;
  296. { Write used files and libraries }
  297. WriteResponseFile;
  298. { Call linker }
  299. if not externlink then
  300. Message1(exec_i_linking,ExeName);
  301. s:=target_link.linkcmd;
  302. Replace(s,'$EXE',exename);
  303. Replace(s,'$OPT',LinkOptions);
  304. Replace(s,'$RES',inputdir+LinkResName);
  305. success:=DoExec(FindLinker,s,true,false);
  306. {Bind}
  307. if target_info.target=target_os2 then
  308. begin
  309. {Calculate the stack and heap size in kilobytes, rounded upwards.}
  310. _stacksize:=(stacksize+1023) shr 10;
  311. {Minimum stacksize for EMX is 32K.}
  312. if _stacksize<32 then
  313. _stacksize:=32;
  314. str(_stacksize,s);
  315. _heapsize:=(heapsize+1023) shr 10;
  316. str(_heapsize,s2);
  317. bindbin:=FindExe('emxbind',bindfound);
  318. if (not bindfound) and (not externlink) then
  319. begin
  320. Message(exec_w_binder_not_found);
  321. externlink:=true;
  322. end;
  323. DoExec(bindbin,'-k'+s+' -o '+exename+'.exe '+exename+' -aim -s'+s2,false,false);
  324. end;
  325. {Remove ReponseFile}
  326. if (success) and (not externlink) then
  327. begin
  328. assign(dummy,LinkResName);
  329. {$I-}
  330. erase(dummy);
  331. {$I+}
  332. i:=ioresult;
  333. end;
  334. MakeExecutable:=success; { otherwise a recursive call to link method }
  335. end;
  336. Procedure TLinker.MakeStaticLibrary(const path:string);
  337. var
  338. arbin : string;
  339. arfound : boolean;
  340. i : word;
  341. f : file;
  342. Dir : searchrec;
  343. begin
  344. arbin:=FindExe('ar',arfound);
  345. if (not arfound) and (not externlink) then
  346. begin
  347. Message(exec_w_ar_not_found);
  348. externlink:=true;
  349. end;
  350. DoExec(arbin,'rs '+staticlibname+' '+FixPath(path)+'*'+target_info.objext,false,true);
  351. { Clean up }
  352. if (not writeasmfile) and (not externlink) then
  353. begin
  354. findfirst(FixPath(path)+'*'+target_info.objext,$20,Dir);
  355. while doserror=0 do
  356. begin
  357. assign(f,FixPath(path)+dir.name);
  358. {$I-}
  359. erase(f);
  360. {$I+}
  361. i:=ioresult;
  362. findnext(dir);
  363. end;
  364. {$I-}
  365. rmdir(path);
  366. {$I+}
  367. i:=ioresult;
  368. end;
  369. end;
  370. Procedure TLinker.MakeSharedLibrary;
  371. begin
  372. DoExec(FindLinker,' -shared -o '+sharedlibname+' link.res',false,false);
  373. end;
  374. end.
  375. {
  376. $Log$
  377. Revision 1.6 1998-05-06 09:26:49 peter
  378. * fixed ld call with shell
  379. Revision 1.4 1998/05/04 17:54:25 peter
  380. + smartlinking works (only case jumptable left todo)
  381. * redesign of systems.pas to support assemblers and linkers
  382. + Unitname is now also in the PPU-file, increased version to 14
  383. Revision 1.3 1998/04/16 10:54:30 daniel
  384. * Fixed linking for OS/2.
  385. }