link.pas 14 KB

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