link.pas 11 KB

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