link.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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;filescnt:longint);
  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. 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. { Add library searchpath to the commandline }
  297. S2:=LibrarySearchPath;
  298. Repeat
  299. i:=Pos(';',S2);
  300. If i=0 then
  301. i:=255;
  302. S:=Copy(S2,1,i-1);
  303. If S<>'' then
  304. LinkOptions:=LinkOptions+' -L'+s;
  305. Delete (S2,1,i);
  306. until S2='';
  307. { Write used files and libraries }
  308. WriteResponseFile;
  309. { Call linker }
  310. if not externlink then
  311. Message1(exec_i_linking,ExeName);
  312. s:=target_link.linkcmd;
  313. Replace(s,'$EXE',exename);
  314. Replace(s,'$OPT',LinkOptions);
  315. Replace(s,'$RES',inputdir+LinkResName);
  316. success:=DoExec(FindLinker,s,true,false);
  317. {Bind}
  318. if target_info.target=target_os2 then
  319. begin
  320. {Calculate the stack and heap size in kilobytes, rounded upwards.}
  321. _stacksize:=(stacksize+1023) shr 10;
  322. {Minimum stacksize for EMX is 32K.}
  323. if _stacksize<32 then
  324. _stacksize:=32;
  325. str(_stacksize,s);
  326. _heapsize:=(heapsize+1023) shr 10;
  327. str(_heapsize,s2);
  328. bindbin:=FindExe('emxbind',bindfound);
  329. if (not bindfound) and (not externlink) then
  330. begin
  331. Message(exec_w_binder_not_found);
  332. externlink:=true;
  333. end;
  334. DoExec(bindbin,'-k'+s+' -o '+exename+'.exe '+exename+' -aim -s'+s2,false,false);
  335. end;
  336. {Remove ReponseFile}
  337. if (success) and (not externlink) then
  338. begin
  339. assign(dummy,LinkResName);
  340. {$I-}
  341. erase(dummy);
  342. {$I+}
  343. i:=ioresult;
  344. end;
  345. MakeExecutable:=success; { otherwise a recursive call to link method }
  346. end;
  347. Procedure TLinker.MakeStaticLibrary(const path:string;filescnt:longint);
  348. var
  349. arbin : string;
  350. arfound : boolean;
  351. cnt : longint;
  352. i : word;
  353. f : file;
  354. begin
  355. arbin:=FindExe('ar',arfound);
  356. if (not arfound) and (not externlink) then
  357. begin
  358. Message(exec_w_ar_not_found);
  359. externlink:=true;
  360. end;
  361. DoExec(arbin,'rs '+staticlibname+' '+FixPath(path)+'*'+target_info.objext,false,true);
  362. { Clean up }
  363. if (not writeasmfile) and (not externlink) then
  364. begin
  365. for cnt:=1to filescnt do
  366. begin
  367. assign(f,FixPath(path)+'as'+tostr(cnt)+target_info.objext);
  368. {$I-}
  369. erase(f);
  370. {$I+}
  371. i:=ioresult;
  372. end;
  373. {$I-}
  374. rmdir(path);
  375. {$I+}
  376. i:=ioresult;
  377. end;
  378. end;
  379. Procedure TLinker.MakeSharedLibrary;
  380. begin
  381. DoExec(FindLinker,' -shared -o '+sharedlibname+' link.res',false,false);
  382. end;
  383. end.
  384. {
  385. $Log$
  386. Revision 1.9 1998-05-12 10:46:59 peter
  387. * moved printstatus to verb_def
  388. + V_Normal which is between V_Error and V_Warning and doesn't have a
  389. prefix like error: warning: and is included in V_Default
  390. * fixed some messages
  391. * first time parameter scan is only for -v and -T
  392. - removed old style messages
  393. Revision 1.8 1998/05/11 13:07:54 peter
  394. + $ifdef NEWPPU for the new ppuformat
  395. + $define GDB not longer required
  396. * removed all warnings and stripped some log comments
  397. * no findfirst/findnext anymore to remove smartlink *.o files
  398. Revision 1.7 1998/05/08 09:21:20 michael
  399. * Added missing -Fl message to messages file.
  400. * Corrected mangling of file names when doing Linklib
  401. * -Fl now actually WORKS.
  402. * Librarysearchpath is now a field in linker object.
  403. Revision 1.6 1998/05/06 09:26:49 peter
  404. * fixed ld call with shell
  405. Revision 1.4 1998/05/04 17:54:25 peter
  406. + smartlinking works (only case jumptable left todo)
  407. * redesign of systems.pas to support assemblers and linkers
  408. + Unitname is now also in the PPU-file, increased version to 14
  409. Revision 1.3 1998/04/16 10:54:30 daniel
  410. * Fixed linking for OS/2.
  411. }