link.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. Glibc2,
  25. LinkToC, { Should we link to the C libs? }
  26. Strip : Boolean; { Strip symbols ? }
  27. ObjectFiles,
  28. SharedLibFiles,
  29. StaticLibFiles : TStringContainer;
  30. LibrarySearchPath, { Search path for libraries }
  31. LinkOptions : string; { Additional options to the linker }
  32. DynamicLinker : String[80]; { What Dynamic linker ? }
  33. LinkResName : String[32]; { Name of response file }
  34. { Methods }
  35. Constructor Init;
  36. Destructor Done;
  37. procedure AddModuleFiles(hp:pmodule);
  38. function FindObjectFile(s : string) : string;
  39. function FindLibraryFile(s:string;const ext:string) : string;
  40. Procedure AddObject(const S : String);
  41. Procedure AddStaticLibrary(const S : String);
  42. Procedure AddSharedLibrary(S : String);
  43. Function FindLinker:String; { Find linker, sets Name }
  44. Function DoExec(const command,para:string;info,useshell:boolean):boolean;
  45. Function WriteResponseFile:Boolean;
  46. Function MakeExecutable:boolean;
  47. Procedure MakeStaticLibrary(filescnt:longint);
  48. Procedure MakeSharedLibrary;
  49. end;
  50. PLinker=^TLinker;
  51. Var
  52. Linker : TLinker;
  53. Implementation
  54. uses
  55. Script,globals,systems,verbose
  56. {$ifdef i386}
  57. ,win_targ
  58. {$endif}
  59. {$ifdef linux}
  60. ,linux
  61. {$endif}
  62. ,dos
  63. ;
  64. {$ifndef linux}
  65. Procedure Shell(command:string);
  66. { This is already defined in the linux.ppu for linux, need for the *
  67. expansion under linux }
  68. var
  69. comspec : string;
  70. begin
  71. comspec:=getenv('COMSPEC');
  72. Exec(comspec,' /C '+command);
  73. end;
  74. {$endif}
  75. Constructor TLinker.Init;
  76. begin
  77. ObjectFiles.Init_no_double;
  78. SharedLibFiles.Init_no_double;
  79. StaticLibFiles.Init_no_double;
  80. LinkToC:=False;
  81. Glibc2:=false;
  82. Strip:=false;
  83. LinkOptions:='';
  84. {$ifdef linux}
  85. { first try glibc2 }
  86. DynamicLinker:='/lib/ld-linux.so.2';
  87. if FileExists(DynamicLinker) then
  88. Glibc2:=true
  89. else
  90. DynamicLinker:='/lib/ld-linux.so.1';
  91. LibrarySearchPath:='/lib;/usr/lib';
  92. {$else}
  93. DynamicLinker:='';
  94. LibrarySearchPath:='';
  95. {$endif}
  96. LinkResName:='link.res';
  97. end;
  98. Destructor TLinker.Done;
  99. begin
  100. ObjectFiles.Done;
  101. SharedLibFiles.Done;
  102. StaticLibFiles.Done;
  103. end;
  104. procedure TLinker.AddModuleFiles(hp:pmodule);
  105. begin
  106. with hp^ do
  107. begin
  108. while not linkofiles.empty do
  109. AddObject(linkofiles.Get);
  110. while not linksharedlibs.empty do
  111. AddSharedLibrary(linksharedlibs.Get);
  112. while not linkstaticlibs.empty do
  113. AddStaticLibrary(linkstaticlibs.Get);
  114. end;
  115. end;
  116. var
  117. LastLDBin : string;
  118. Function TLinker.FindLinker:string;
  119. var
  120. ldfound : boolean;
  121. begin
  122. if LastLDBin='' then
  123. begin
  124. if utilsdirectory<>'' then
  125. begin
  126. LastLDBin:=Search(target_link.linkbin+source_os.exeext,
  127. utilsdirectory,ldfound)+target_link.linkbin+source_os.exeext;
  128. end
  129. else
  130. LastLDBin:=FindExe(target_link.linkbin,ldfound);
  131. if (not ldfound) and not(cs_link_extern in aktglobalswitches) then
  132. begin
  133. Message1(exec_w_linker_not_found,LastLDBin);
  134. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  135. end;
  136. if ldfound then
  137. Message1(exec_t_using_linker,LastLDBin);
  138. end;
  139. FindLinker:=LastLDBin;
  140. end;
  141. { searches an object file }
  142. function TLinker.FindObjectFile(s:string) : string;
  143. var
  144. found : boolean;
  145. begin
  146. if pos('.',s)=0 then
  147. s:=s+target_info.objext;
  148. s:=FixFileName(s);
  149. if FileExists(s) then
  150. begin
  151. Findobjectfile:=s;
  152. exit;
  153. end;
  154. findobjectfile:=search(s,'.;'+unitsearchpath+';'+exepath,found)+s;
  155. if not(cs_link_extern in aktglobalswitches) and (not found) then
  156. Message1(exec_w_objfile_not_found,s);
  157. end;
  158. { searches an library file }
  159. function TLinker.FindLibraryFile(s:string;const ext:string) : string;
  160. var
  161. found : boolean;
  162. begin
  163. if pos('.',s)=0 then
  164. s:=s+ext;
  165. if FileExists(s) then
  166. begin
  167. FindLibraryFile:=s;
  168. exit;
  169. end;
  170. findlibraryfile:=search(s,'.;'+librarysearchpath+';'+exepath,found)+s;
  171. if not(cs_link_extern in aktglobalswitches) and (not found) then
  172. Message1(exec_w_libfile_not_found,s);
  173. end;
  174. Procedure TLinker.AddObject(const S : String);
  175. begin
  176. ObjectFiles.Insert(FindObjectFile(s));
  177. end;
  178. Procedure TLinker.AddSharedLibrary(S:String);
  179. begin
  180. { remove prefix 'lib' }
  181. if Copy(s,1,length(target_os.libprefix))=target_os.libprefix then
  182. Delete(s,1,length(target_os.libprefix));
  183. { remove extension if any }
  184. if Copy(s,length(s)-length(target_os.sharedlibext)+1,length(target_os.sharedlibext))=target_os.sharedlibext then
  185. Delete(s,length(s)-length(target_os.sharedlibext)+1,length(target_os.sharedlibext)+1);
  186. { ready to be inserted }
  187. SharedLibFiles.Insert (S);
  188. end;
  189. Procedure TLinker.AddStaticLibrary(const S:String);
  190. begin
  191. StaticLibFiles.Insert(FindLibraryFile(s,target_os.staticlibext));
  192. end;
  193. Function TLinker.DoExec(const command,para:string;info,useshell:boolean):boolean;
  194. begin
  195. DoExec:=true;
  196. if not(cs_link_extern in aktglobalswitches) then
  197. begin
  198. swapvectors;
  199. if useshell then
  200. shell(command+' '+para)
  201. else
  202. exec(command,para);
  203. swapvectors;
  204. if (doserror<>0) then
  205. begin
  206. Message(exec_w_cant_call_linker);
  207. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  208. DoExec:=false;
  209. end
  210. else
  211. if (dosexitcode<>0) then
  212. begin
  213. Message(exec_w_error_while_linking);
  214. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  215. DoExec:=false;
  216. end;
  217. end;
  218. { Update asmres when externmode is set }
  219. if cs_link_extern in aktglobalswitches then
  220. begin
  221. if info then
  222. AsmRes.AddLinkCommand(Command,Para,current_module^.exefilename^)
  223. else
  224. AsmRes.AddLinkCommand(Command,Para,'');
  225. end;
  226. end;
  227. Function TLinker.WriteResponseFile : Boolean;
  228. Var
  229. LinkResponse : Text;
  230. i : longint;
  231. prtobj,s,s2 : string;
  232. found,
  233. linklibc : boolean;
  234. procedure WriteRes(const s:string);
  235. begin
  236. if s<>'' then
  237. WriteLn(Linkresponse,s);
  238. end;
  239. begin
  240. WriteResponseFile:=False;
  241. { set special options for some targets }
  242. linklibc:=false;
  243. prtobj:='prt0';
  244. case target_info.target of
  245. target_m68k_Palmos,
  246. target_i386_Win32 :
  247. begin
  248. prtobj:='';
  249. end;
  250. target_m68k_linux,
  251. target_i386_linux :
  252. begin
  253. linklibc:=SharedLibFiles.Find('c');
  254. if cs_profile in aktmoduleswitches then
  255. begin
  256. prtobj:='gprt0';
  257. if not glibc2 then
  258. AddSharedLibrary('gmon');
  259. AddSharedLibrary('c');
  260. linklibc:=true;
  261. end
  262. else
  263. begin
  264. if linklibc then
  265. prtobj:='cprt0';
  266. end;
  267. end;
  268. end;
  269. { Fix command line options }
  270. If not SharedLibFiles.Empty then
  271. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  272. if Strip and not(cs_debuginfo in aktmoduleswitches) then
  273. LinkOptions:=LinkOptions+target_link.stripopt;
  274. { Open linkresponse and write header }
  275. assign(linkresponse,inputdir+LinkResName);
  276. {$I-}
  277. rewrite(linkresponse);
  278. {$I+}
  279. if ioresult<>0 then
  280. exit;
  281. { Write library searchpath }
  282. S2:=LibrarySearchPath;
  283. Repeat
  284. i:=Pos(';',S2);
  285. If i=0 then
  286. i:=255;
  287. S:=Copy(S2,1,i-1);
  288. If S<>'' then
  289. WriteRes(target_link.libpathprefix+s+target_link.libpathsuffix);
  290. Delete (S2,1,i);
  291. until S2='';
  292. WriteRes(target_link.inputstart);
  293. { add objectfiles, start with prt0 always }
  294. if prtobj<>'' then
  295. WriteRes(FindObjectFile(prtobj));
  296. if linklibc then
  297. begin
  298. WriteRes(search('crti.o',librarysearchpath,found)+'crti.o');
  299. WriteRes(search('crtbegin.o',librarysearchpath,found)+'crtbegin.o');
  300. end;
  301. while not ObjectFiles.Empty do
  302. begin
  303. s:=ObjectFiles.Get;
  304. if s<>'' then
  305. WriteRes(s);
  306. end;
  307. if linklibc then
  308. begin
  309. WriteRes(search('crtend.o',librarysearchpath,found)+'crtend.o');
  310. WriteRes(search('crtn.o',librarysearchpath,found)+'crtn.o');
  311. end;
  312. { Write sharedlibraries like -l<lib> }
  313. While not SharedLibFiles.Empty do
  314. begin
  315. S:=SharedLibFiles.Get;
  316. i:=Pos(target_os.sharedlibext,S);
  317. if i>0 then
  318. Delete(S,i,255);
  319. WriteRes(target_link.libprefix+s);
  320. end;
  321. WriteRes(target_link.inputend);
  322. { Write staticlibraries }
  323. if not StaticLibFiles.Empty then
  324. begin
  325. WriteRes(target_link.GroupStart);
  326. While not StaticLibFiles.Empty do
  327. begin
  328. S:=StaticLibFiles.Get;
  329. WriteRes(s)
  330. end;
  331. WriteRes(target_link.GroupEnd);
  332. end;
  333. { Close response }
  334. close(linkresponse);
  335. WriteResponseFile:=True;
  336. end;
  337. function TLinker.MakeExecutable:boolean;
  338. var
  339. bindbin : string[80];
  340. bindfound : boolean;
  341. s : string;
  342. success : boolean;
  343. begin
  344. {$ifdef linux}
  345. if LinkToC then
  346. begin
  347. AddObject('/usr/lib/crt0.o');
  348. AddObject('lprt');
  349. AddStaticLibrary('libc.a');
  350. AddStaticLibrary('libgcc.a');
  351. end;
  352. {$endif Linux}
  353. { Write used files and libraries }
  354. WriteResponseFile;
  355. { Call linker }
  356. if not(cs_link_extern in aktglobalswitches) then
  357. Message1(exec_i_linking,current_module^.exefilename^);
  358. s:=target_link.linkcmd;
  359. Replace(s,'$EXE',current_module^.exefilename^);
  360. Replace(s,'$OPT',LinkOptions);
  361. Replace(s,'$RES',inputdir+LinkResName);
  362. success:=DoExec(FindLinker,s,true,false);
  363. {Bind}
  364. if target_link.bindbin<>'' then
  365. begin
  366. s:=target_link.bindcmd;
  367. Replace(s,'$EXE',current_module^.exefilename^);
  368. {Size of the heap when an EMX program runs in OS/2.}
  369. Replace(s,'$HEAPMB',tostr((maxheapsize+1048575) shr 20));
  370. {Size of the stack when an EMX program runs in OS/2.}
  371. Replace(s,'$STACKKB',tostr((stacksize+1023) shr 10));
  372. {When an EMX program runs in DOS, the heap and stack share the
  373. same memory pool. The heap grows upwards, the stack grows downwards.}
  374. Replace(s,'$DOSHEAPKB',tostr((stacksize+maxheapsize+1023) shr 10));
  375. if utilsdirectory<>'' then
  376. begin
  377. bindbin:=Search(target_link.bindbin+source_os.exeext,
  378. utilsdirectory,bindfound)+target_link.bindbin+source_os.exeext;
  379. end
  380. else
  381. bindbin:=FindExe(target_link.bindbin,bindfound);
  382. if (not bindfound) and not (cs_link_extern in aktglobalswitches) then
  383. begin
  384. Message1(exec_w_binder_not_found,bindbin);
  385. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  386. end;
  387. DoExec(bindbin,s,false,false);
  388. end;
  389. { Post processor executable }
  390. {$ifdef i386}
  391. if target_info.target=target_i386_Win32 then
  392. win_targ.postprocessexecutable(current_module^.exefilename^);
  393. {$endif}
  394. {Remove ReponseFile}
  395. if (success) and not(cs_link_extern in aktglobalswitches) then
  396. RemoveFile(LinkResName);
  397. MakeExecutable:=success; { otherwise a recursive call to link method }
  398. end;
  399. Procedure TLinker.MakeStaticLibrary(filescnt:longint);
  400. {
  401. FilesCnt holds the amount of .o files created, if filescnt=0 then
  402. no smartlinking is used
  403. }
  404. var
  405. smartpath,
  406. s,
  407. arbin : string;
  408. arfound : boolean;
  409. cnt : longint;
  410. begin
  411. smartpath:=current_module^.path^+FixPath(FixFileName(current_module^.modulename^)+target_info.smartext);
  412. { find ar binary }
  413. if utilsdirectory<>'' then
  414. begin
  415. arbin:=Search(target_ar.arbin+source_os.exeext,
  416. utilsdirectory,arfound)+target_ar.arbin+source_os.exeext;
  417. end
  418. else
  419. arbin:=FindExe(target_ar.arbin,arfound);
  420. if (not arfound) and not(cs_link_extern in aktglobalswitches) then
  421. begin
  422. Message(exec_w_ar_not_found);
  423. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  424. end;
  425. s:=target_ar.arcmd;
  426. Replace(s,'$LIB',current_module^.staticlibfilename^);
  427. if filescnt=0 then
  428. Replace(s,'$FILES',current_module^.objfilename^)
  429. else
  430. Replace(s,'$FILES',FixFileName(smartpath+current_module^.asmprefix^+'*'+target_info.objext));
  431. DoExec(arbin,s,false,true);
  432. { Clean up }
  433. if not(cs_asm_leave in aktglobalswitches) and not(cs_link_extern in aktglobalswitches) then
  434. begin
  435. if filescnt=0 then
  436. RemoveFile(current_module^.objfilename^)
  437. else
  438. begin
  439. for cnt:=1 to filescnt do
  440. if not RemoveFile(FixFileName(smartpath+current_module^.asmprefix^+tostr(cnt)+target_info.objext)) then
  441. RemoveFile(FixFileName(smartpath+current_module^.asmprefix^+'e'+tostr(cnt)+target_info.objext));
  442. RemoveDir(smartpath);
  443. end;
  444. end;
  445. end;
  446. Procedure TLinker.MakeSharedLibrary;
  447. var
  448. s : string;
  449. begin
  450. s:=' -shared -o $LIB $FILES';
  451. Replace(s,'$LIB',current_module^.sharedlibfilename^);
  452. Replace(s,'$FILES',current_module^.objfilename^);
  453. if DoExec(FindLinker,s,false,false) then
  454. RemoveFile(current_module^.objfilename^);
  455. end;
  456. end.
  457. {
  458. $Log$
  459. Revision 1.36 1998-10-22 15:18:44 florian
  460. + switch -vx for win32 added
  461. Revision 1.35 1998/10/19 18:06:23 peter
  462. * use no_double
  463. Revision 1.34 1998/10/16 13:37:18 florian
  464. + switch -FD added to specify the path for utilities
  465. Revision 1.33 1998/10/14 13:38:22 peter
  466. * fixed path with staticlib/objects in ppufiles
  467. Revision 1.32 1998/10/14 11:03:55 daniel
  468. * Forgot to dereference a pointer.
  469. Revision 1.31 1998/10/14 11:01:21 daniel
  470. * Staticlibfilename no longer not include a path. Correction when calling
  471. ar.
  472. Revision 1.30 1998/10/13 13:10:18 peter
  473. * new style for m68k/i386 infos and enums
  474. Revision 1.29 1998/10/13 08:19:34 pierre
  475. + source_os is now set correctly for cross-processor compilers
  476. (tos contains all target_infos and
  477. we use CPU86 and CPU68 conditionnals to
  478. get the source operating system
  479. this only works if you do not undefine
  480. the source target !!)
  481. * several cg68k memory leaks fixed
  482. + started to change the code so that it should be possible to have
  483. a complete compiler (both for m68k and i386 !!)
  484. Revision 1.28 1998/10/08 23:28:56 peter
  485. * -vu shows unit info, -vt shows tried/used files
  486. Revision 1.27 1998/10/06 17:16:52 pierre
  487. * some memory leaks fixed (thanks to Peter for heaptrc !)
  488. Revision 1.26 1998/09/29 15:23:05 peter
  489. * remove also the end files for smartlinking
  490. Revision 1.25 1998/09/10 15:25:31 daniel
  491. + Added maxheapsize.
  492. * Corrected semi-bug in calling the assembler and the linker
  493. Revision 1.24 1998/09/07 18:32:45 peter
  494. * fixed for m68k
  495. Revision 1.23 1998/09/03 17:39:04 florian
  496. + better code for type conversation longint/dword to real type
  497. Revision 1.22 1998/09/01 09:01:00 peter
  498. + glibc2 support
  499. Revision 1.21 1998/08/31 12:26:26 peter
  500. * m68k and palmos updates from surebugfixes
  501. Revision 1.20 1998/08/19 10:06:14 peter
  502. * fixed filenames and removedir which supports slash at the end
  503. Revision 1.19 1998/08/17 09:17:47 peter
  504. * static/shared linking updates
  505. Revision 1.18 1998/08/14 21:56:34 peter
  506. * setting the outputfile using -o works now to create static libs
  507. Revision 1.17 1998/08/14 18:16:08 peter
  508. * return after a failed call will now add it to ppas
  509. Revision 1.16 1998/08/12 19:28:15 peter
  510. * better libc support
  511. Revision 1.15 1998/08/10 14:50:02 peter
  512. + localswitches, moduleswitches, globalswitches splitting
  513. Revision 1.14 1998/06/17 14:10:13 peter
  514. * small os2 fixes
  515. * fixed interdependent units with newppu (remake3 under linux works now)
  516. Revision 1.13 1998/06/08 22:59:46 peter
  517. * smartlinking works for win32
  518. * some defines to exclude some compiler parts
  519. Revision 1.12 1998/06/04 23:51:44 peter
  520. * m68k compiles
  521. + .def file creation moved to gendef.pas so it could also be used
  522. for win32
  523. Revision 1.11 1998/05/27 00:20:31 peter
  524. * some scanner optimizes
  525. * automaticly aout2exe for go32v1
  526. * fixed dynamiclinker option which was added at the wrong place
  527. Revision 1.10 1998/05/22 12:32:47 peter
  528. * fixed -L on the commandline, Dos commandline is only 128 bytes
  529. Revision 1.9 1998/05/12 10:46:59 peter
  530. * moved printstatus to verb_def
  531. + V_Normal which is between V_Error and V_Warning and doesn't have a
  532. prefix like error: warning: and is included in V_Default
  533. * fixed some messages
  534. * first time parameter scan is only for -v and -T
  535. - removed old style messages
  536. Revision 1.8 1998/05/11 13:07:54 peter
  537. + $ifdef NEWPPU for the new ppuformat
  538. + $define GDB not longer required
  539. * removed all warnings and stripped some log comments
  540. * no findfirst/findnext anymore to remove smartlink *.o files
  541. Revision 1.7 1998/05/08 09:21:20 michael
  542. * Added missing -Fl message to messages file.
  543. * Corrected mangling of file names when doing Linklib
  544. * -Fl now actually WORKS.
  545. * Librarysearchpath is now a field in linker object.
  546. Revision 1.6 1998/05/06 09:26:49 peter
  547. * fixed ld call with shell
  548. Revision 1.4 1998/05/04 17:54:25 peter
  549. + smartlinking works (only case jumptable left todo)
  550. * redesign of systems.pas to support assemblers and linkers
  551. + Unitname is now also in the PPU-file, increased version to 14
  552. Revision 1.3 1998/04/16 10:54:30 daniel
  553. * Fixed linking for OS/2.
  554. }