link.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. OutputName,
  30. LibrarySearchPath, { Search path for libraries }
  31. ExeName, { FileName of the exe to be created }
  32. SharedLibName,
  33. StaticLibName, { FileName of the lib to be created }
  34. LinkOptions : string; { Additional options to the linker }
  35. DynamicLinker : String[80]; { What Dynamic linker ? }
  36. LinkResName : String[32]; { Name of response file }
  37. { Methods }
  38. Constructor Init;
  39. Destructor Done;
  40. Procedure SetOutputName(const s:string);
  41. Procedure SetExeName(const s:string);
  42. Procedure SetLibName(const s:string);
  43. function FindObjectFile(s : string) : string;
  44. function FindLibraryFile(s:string;const ext:string) : string;
  45. Procedure AddObject(const S : String);
  46. Procedure AddStaticLibrary(const S : String);
  47. Procedure AddSharedLibrary(const S : String);
  48. Function FindLinker:String; { Find linker, sets Name }
  49. Function DoExec(const command,para:string;info,useshell:boolean):boolean;
  50. Function WriteResponseFile:Boolean;
  51. Function MakeExecutable:boolean;
  52. Procedure MakeStaticLibrary(const path:string;filescnt:longint);
  53. Procedure MakeSharedLibrary;
  54. end;
  55. PLinker=^TLinker;
  56. Var
  57. Linker : TLinker;
  58. Implementation
  59. uses
  60. Script,globals,systems,verbose
  61. {$ifdef linux}
  62. ,linux
  63. {$endif}
  64. ,dos;
  65. {$ifndef linux}
  66. Procedure Shell(command:string);
  67. { This is already defined in the linux.ppu for linux, need for the *
  68. expansion under linux }
  69. var
  70. comspec : string;
  71. begin
  72. comspec:=getenv('COMSPEC');
  73. Exec(comspec,' /C '+command);
  74. end;
  75. {$endif}
  76. Constructor TLinker.Init;
  77. begin
  78. ObjectFiles.Init;
  79. SharedLibFiles.Init;
  80. StaticLibFiles.Init;
  81. ObjectFiles.Doubles:=False;
  82. SharedLibFiles.Doubles:=False;
  83. StaticLibFiles.Doubles:=False;
  84. LinkToC:=False;
  85. Strip:=false;
  86. LinkOptions:='';
  87. ExeName:='';
  88. OutputName:='';
  89. SharedLibName:='';
  90. StaticLibName:='';
  91. ObjectSearchPath:='';
  92. LibrarySearchPath:='';
  93. {$ifdef linux}
  94. DynamicLinker:='/lib/ld-linux.so.1';
  95. {$else}
  96. DynamicLinker:='';
  97. {$endif}
  98. LinkResName:='link.res';
  99. end;
  100. Destructor TLinker.Done;
  101. begin
  102. end;
  103. Procedure TLinker.SetOutputName(const s:string);
  104. begin
  105. OutputName:=s;
  106. end;
  107. Procedure TLinker.SetExeName(const s:string);
  108. var
  109. path : dirstr;
  110. name : namestr;
  111. ext : extstr;
  112. begin
  113. if OutputName='' then
  114. begin
  115. FSplit(s,path,name,ext);
  116. ExeName:=Path+Name+target_info.ExeExt;
  117. end
  118. else
  119. ExeName:=OutputName;
  120. end;
  121. Procedure TLinker.SetLibName(const s:string);
  122. var
  123. path : dirstr;
  124. name : namestr;
  125. ext : extstr;
  126. begin
  127. if OutputName='' then
  128. begin
  129. FSplit(s,path,name,ext);
  130. SharedLibName:=Path+Name+target_os.SharedLibExt;
  131. StaticLibName:=Path+Name+target_os.StaticLibExt;
  132. end
  133. else
  134. begin
  135. SharedLibName:=OutputName;
  136. StaticLibName:=OutputName;
  137. end;
  138. end;
  139. var
  140. LastLDBin : string;
  141. Function TLinker.FindLinker:string;
  142. var
  143. ldfound : boolean;
  144. begin
  145. if LastLDBin='' then
  146. begin
  147. LastLDBin:=FindExe(target_link.linkbin,ldfound);
  148. if (not ldfound) and (not externlink) then
  149. begin
  150. Message1(exec_w_linker_not_found,LastLDBin);
  151. externlink:=true;
  152. end;
  153. if ldfound then
  154. Message1(exec_u_using_linker,LastLDBin);
  155. end;
  156. FindLinker:=LastLDBin;
  157. end;
  158. { searches an object file }
  159. function TLinker.FindObjectFile(s:string) : string;
  160. var
  161. found : boolean;
  162. begin
  163. if pos('.',s)=0 then
  164. s:=s+target_info.objext;
  165. s:=FixFileName(s);
  166. if FileExists(s) then
  167. begin
  168. Findobjectfile:=s;
  169. exit;
  170. end;
  171. findobjectfile:=search(s,'.;'+unitsearchpath+';'+exepath,found)+s;
  172. if (not externlink) and (not found) then
  173. Message1(exec_w_objfile_not_found,s);
  174. end;
  175. { searches an library file }
  176. function TLinker.FindLibraryFile(s:string;const ext:string) : string;
  177. var
  178. found : boolean;
  179. begin
  180. if pos('.',s)=0 then
  181. s:=s+ext;
  182. if FileExists(s) then
  183. begin
  184. FindLibraryFile:=s;
  185. exit;
  186. end;
  187. findlibraryfile:=search(s,'.;'+librarysearchpath+';'+exepath,found)+s;
  188. if (not externlink) and (not found) then
  189. Message1(exec_w_libfile_not_found,s);
  190. end;
  191. Procedure TLinker.AddObject(const S : String);
  192. begin
  193. ObjectFiles.Insert(FindObjectFile(s));
  194. end;
  195. Procedure TLinker.AddSharedLibrary(const S:String);
  196. begin
  197. SharedLibFiles.Insert (S);
  198. { SharedLibFiles.Insert(FindLibraryFile(s,target_os.sharedlibext)); }
  199. end;
  200. Procedure TLinker.AddStaticLibrary(const S:String);
  201. begin
  202. StaticLibFiles.Insert(FindLibraryFile(s,target_os.staticlibext));
  203. end;
  204. Function TLinker.DoExec(const command,para:string;info,useshell:boolean):boolean;
  205. begin
  206. DoExec:=true;
  207. if not externlink then
  208. begin
  209. swapvectors;
  210. if useshell then
  211. shell(command+' '+para)
  212. else
  213. exec(command,para);
  214. swapvectors;
  215. if (dosexitcode<>0) then
  216. begin
  217. Message(exec_w_error_while_linking);
  218. DoExec:=false;
  219. exit;
  220. end
  221. else
  222. if (dosError<>0) then
  223. begin
  224. Message(exec_w_cant_call_linker);
  225. ExternLink:=true;
  226. end;
  227. end;
  228. if externlink then
  229. begin
  230. if info then
  231. AsmRes.AddLinkCommand(Command,Para,ExeName)
  232. else
  233. AsmRes.AddLinkCommand(Command,Para,'');
  234. end;
  235. end;
  236. Function TLinker.WriteResponseFile : Boolean;
  237. Var
  238. LinkResponse : Text;
  239. i : longint;
  240. prtobj,s,s2 : string;
  241. procedure WriteRes(const s:string);
  242. begin
  243. if s<>'' then
  244. WriteLn(Linkresponse,s);
  245. end;
  246. begin
  247. WriteResponseFile:=False;
  248. { set special options for some targets }
  249. prtobj:='prt0';
  250. case target_info.target of
  251. {$ifdef i386}
  252. target_Win32 : prtobj:='';
  253. target_linux : begin
  254. if cs_profile in aktswitches then
  255. begin
  256. prtobj:='gprt0';
  257. AddSharedLibrary('gmon');
  258. AddSharedLibrary('c');
  259. end;
  260. end;
  261. {$endif i386}
  262. {$ifdef m68k}
  263. target_linux : begin
  264. if cs_profile in aktswitches then
  265. begin
  266. prtobj:='gprt0';
  267. AddSharedLibrary('gmon');
  268. AddSharedLibrary('c');
  269. end;
  270. end;
  271. {$endif}
  272. end;
  273. { Fix command line options }
  274. If not SharedLibFiles.Empty then
  275. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  276. if Strip then
  277. LinkOptions:=LinkOptions+target_link.stripopt;
  278. { Open linkresponse and write header }
  279. assign(linkresponse,inputdir+LinkResName);
  280. {$I-}
  281. rewrite(linkresponse);
  282. {$I+}
  283. if ioresult<>0 then
  284. exit;
  285. { Write library searchpath }
  286. S2:=LibrarySearchPath;
  287. Repeat
  288. i:=Pos(';',S2);
  289. If i=0 then
  290. i:=255;
  291. S:=Copy(S2,1,i-1);
  292. If S<>'' then
  293. WriteRes(target_link.libpathprefix+s+target_link.libpathsuffix);
  294. Delete (S2,1,i);
  295. until S2='';
  296. WriteRes(target_link.inputstart);
  297. { add objectfiles, start with prt0 always }
  298. if prtobj<>'' then
  299. WriteRes(FindObjectFile(prtobj));
  300. while not ObjectFiles.Empty do
  301. begin
  302. s:=ObjectFiles.Get;
  303. if s<>'' then
  304. WriteRes(s);
  305. end;
  306. { Write sharedlibraries like -l<lib> }
  307. While not SharedLibFiles.Empty do
  308. begin
  309. S:=SharedLibFiles.Get;
  310. i:=Pos(target_os.sharedlibext,S);
  311. if i>0 then
  312. Delete(S,i,255);
  313. WriteRes(target_link.libprefix+s);
  314. end;
  315. WriteRes(target_link.inputend);
  316. { Write staticlibraries }
  317. if not StaticLibFiles.Empty then
  318. begin
  319. WriteRes(target_link.GroupStart);
  320. While not StaticLibFiles.Empty do
  321. begin
  322. S:=StaticLibFiles.Get;
  323. WriteRes(s)
  324. end;
  325. WriteRes(target_link.GroupEnd);
  326. end;
  327. { Close response }
  328. close(linkresponse);
  329. WriteResponseFile:=True;
  330. end;
  331. function TLinker.MakeExecutable:boolean;
  332. var
  333. bindbin : string[80];
  334. bindfound : boolean;
  335. i : longint;
  336. s : string;
  337. dummy : file;
  338. success : boolean;
  339. begin
  340. {$ifdef linux}
  341. if LinkToC then
  342. begin
  343. AddObject('/usr/lib/crt0.o');
  344. AddObject('lprt');
  345. AddStaticLibrary('libc.a');
  346. AddStaticLibrary('libgcc.a');
  347. end;
  348. {$endif Linux}
  349. { Write used files and libraries }
  350. WriteResponseFile;
  351. { Call linker }
  352. if not externlink then
  353. Message1(exec_i_linking,ExeName);
  354. s:=target_link.linkcmd;
  355. Replace(s,'$EXE',exename);
  356. Replace(s,'$OPT',LinkOptions);
  357. Replace(s,'$RES',inputdir+LinkResName);
  358. success:=DoExec(FindLinker,s,true,false);
  359. {Bind}
  360. if target_link.bindbin<>'' then
  361. begin
  362. s:=target_link.bindcmd;
  363. Replace(s,'$EXE',exename);
  364. Replace(s,'$HEAPKB',tostr((heapsize+1023) shr 10));
  365. Replace(s,'$STACKKB',tostr((stacksize+1023) shr 10));
  366. bindbin:=FindExe(target_link.bindbin,bindfound);
  367. if (not bindfound) and (not externlink) then
  368. begin
  369. Message1(exec_w_binder_not_found,bindbin);
  370. externlink:=true;
  371. end;
  372. DoExec(bindbin,s,false,false);
  373. end;
  374. {Remove ReponseFile}
  375. if (success) and (not externlink) then
  376. begin
  377. assign(dummy,LinkResName);
  378. {$I-}
  379. erase(dummy);
  380. {$I+}
  381. i:=ioresult;
  382. end;
  383. MakeExecutable:=success; { otherwise a recursive call to link method }
  384. end;
  385. Procedure TLinker.MakeStaticLibrary(const path:string;filescnt:longint);
  386. var
  387. s,
  388. arbin : string;
  389. arfound : boolean;
  390. cnt : longint;
  391. i : word;
  392. f : file;
  393. begin
  394. arbin:=FindExe(target_ar.arbin,arfound);
  395. if (not arfound) and (not externlink) then
  396. begin
  397. Message(exec_w_ar_not_found);
  398. externlink:=true;
  399. end;
  400. s:=target_ar.arcmd;
  401. Replace(s,'$LIB',staticlibname);
  402. Replace(s,'$FILES',FixPath(path)+'*'+target_info.objext);
  403. DoExec(arbin,s,false,true);
  404. { Clean up }
  405. if (not writeasmfile) and (not externlink) then
  406. begin
  407. for cnt:=1to filescnt do
  408. begin
  409. assign(f,FixPath(path)+'as'+tostr(cnt)+target_info.objext);
  410. {$I-}
  411. erase(f);
  412. {$I+}
  413. i:=ioresult;
  414. end;
  415. {$I-}
  416. rmdir(path);
  417. {$I+}
  418. i:=ioresult;
  419. end;
  420. end;
  421. Procedure TLinker.MakeSharedLibrary;
  422. begin
  423. DoExec(FindLinker,' -shared -o '+sharedlibname+' link.res',false,false);
  424. end;
  425. end.
  426. {
  427. $Log$
  428. Revision 1.14 1998-06-17 14:10:13 peter
  429. * small os2 fixes
  430. * fixed interdependent units with newppu (remake3 under linux works now)
  431. Revision 1.13 1998/06/08 22:59:46 peter
  432. * smartlinking works for win32
  433. * some defines to exclude some compiler parts
  434. Revision 1.12 1998/06/04 23:51:44 peter
  435. * m68k compiles
  436. + .def file creation moved to gendef.pas so it could also be used
  437. for win32
  438. Revision 1.11 1998/05/27 00:20:31 peter
  439. * some scanner optimizes
  440. * automaticly aout2exe for go32v1
  441. * fixed dynamiclinker option which was added at the wrong place
  442. Revision 1.10 1998/05/22 12:32:47 peter
  443. * fixed -L on the commandline, Dos commandline is only 128 bytes
  444. Revision 1.9 1998/05/12 10:46:59 peter
  445. * moved printstatus to verb_def
  446. + V_Normal which is between V_Error and V_Warning and doesn't have a
  447. prefix like error: warning: and is included in V_Default
  448. * fixed some messages
  449. * first time parameter scan is only for -v and -T
  450. - removed old style messages
  451. Revision 1.8 1998/05/11 13:07:54 peter
  452. + $ifdef NEWPPU for the new ppuformat
  453. + $define GDB not longer required
  454. * removed all warnings and stripped some log comments
  455. * no findfirst/findnext anymore to remove smartlink *.o files
  456. Revision 1.7 1998/05/08 09:21:20 michael
  457. * Added missing -Fl message to messages file.
  458. * Corrected mangling of file names when doing Linklib
  459. * -Fl now actually WORKS.
  460. * Librarysearchpath is now a field in linker object.
  461. Revision 1.6 1998/05/06 09:26:49 peter
  462. * fixed ld call with shell
  463. Revision 1.4 1998/05/04 17:54:25 peter
  464. + smartlinking works (only case jumptable left todo)
  465. * redesign of systems.pas to support assemblers and linkers
  466. + Unitname is now also in the PPU-file, increased version to 14
  467. Revision 1.3 1998/04/16 10:54:30 daniel
  468. * Fixed linking for OS/2.
  469. }