link.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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(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(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(S:String);
  165. begin
  166. { remove prefix 'lib' }
  167. if Copy(s,1,length(target_os.libprefix))=target_os.libprefix then
  168. Delete(s,1,length(target_os.libprefix));
  169. { remove extension if any }
  170. if Copy(s,length(s)-length(target_os.sharedlibext)+1,length(target_os.sharedlibext))=target_os.sharedlibext then
  171. Delete(s,length(s)-length(target_os.sharedlibext)+1,length(target_os.sharedlibext)+1);
  172. { ready to be inserted }
  173. SharedLibFiles.Insert (S);
  174. end;
  175. Procedure TLinker.AddStaticLibrary(const S:String);
  176. begin
  177. StaticLibFiles.Insert(FindLibraryFile(s,target_os.staticlibext));
  178. end;
  179. Function TLinker.DoExec(const command,para:string;info,useshell:boolean):boolean;
  180. begin
  181. DoExec:=true;
  182. if not(cs_link_extern in aktglobalswitches) then
  183. begin
  184. swapvectors;
  185. if useshell then
  186. shell(command+' '+para)
  187. else
  188. exec(command,para);
  189. swapvectors;
  190. if (dosexitcode<>0) then
  191. begin
  192. Message(exec_w_error_while_linking);
  193. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  194. DoExec:=false;
  195. end
  196. else
  197. if (dosError<>0) then
  198. begin
  199. Message(exec_w_cant_call_linker);
  200. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  201. DoExec:=false;
  202. end;
  203. end;
  204. { Update asmres when externmode is set }
  205. if cs_link_extern in aktglobalswitches then
  206. begin
  207. if info then
  208. AsmRes.AddLinkCommand(Command,Para,current_module^.exefilename^)
  209. else
  210. AsmRes.AddLinkCommand(Command,Para,'');
  211. end;
  212. end;
  213. Function TLinker.WriteResponseFile : Boolean;
  214. Var
  215. LinkResponse : Text;
  216. i : longint;
  217. prtobj,s,s2 : string;
  218. found,
  219. linklibc : boolean;
  220. procedure WriteRes(const s:string);
  221. begin
  222. if s<>'' then
  223. WriteLn(Linkresponse,s);
  224. end;
  225. begin
  226. WriteResponseFile:=False;
  227. { set special options for some targets }
  228. linklibc:=SharedLibFiles.Find('c');
  229. prtobj:='prt0';
  230. case target_info.target of
  231. {$ifdef i386}
  232. target_Win32 : prtobj:='';
  233. target_linux : begin
  234. if cs_profile in aktmoduleswitches then
  235. begin
  236. prtobj:='gprt0';
  237. AddSharedLibrary('gmon');
  238. AddSharedLibrary('c');
  239. linklibc:=true;
  240. end
  241. else
  242. begin
  243. if linklibc then
  244. prtobj:='cprt0';
  245. end;
  246. end;
  247. {$endif i386}
  248. {$ifdef m68k}
  249. target_linux : begin
  250. if cs_profile in aktmoduleswitches then
  251. begin
  252. prtobj:='gprt0';
  253. AddSharedLibrary('gmon');
  254. AddSharedLibrary('c');
  255. end;
  256. end;
  257. {$endif}
  258. end;
  259. { Fix command line options }
  260. If not SharedLibFiles.Empty then
  261. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  262. if Strip and not(cs_debuginfo in aktmoduleswitches) then
  263. LinkOptions:=LinkOptions+target_link.stripopt;
  264. { Open linkresponse and write header }
  265. assign(linkresponse,inputdir+LinkResName);
  266. {$I-}
  267. rewrite(linkresponse);
  268. {$I+}
  269. if ioresult<>0 then
  270. exit;
  271. { Write library searchpath }
  272. S2:=LibrarySearchPath;
  273. Repeat
  274. i:=Pos(';',S2);
  275. If i=0 then
  276. i:=255;
  277. S:=Copy(S2,1,i-1);
  278. If S<>'' then
  279. WriteRes(target_link.libpathprefix+s+target_link.libpathsuffix);
  280. Delete (S2,1,i);
  281. until S2='';
  282. WriteRes(target_link.inputstart);
  283. { add objectfiles, start with prt0 always }
  284. if prtobj<>'' then
  285. WriteRes(FindObjectFile(prtobj));
  286. if linklibc then
  287. begin
  288. s2:=search('crti.o',librarysearchpath,found);
  289. if s2<>'' then
  290. begin
  291. WriteRes(s2+'crti.o');
  292. WriteRes(s2+'crtbegin.o');
  293. end;
  294. end;
  295. while not ObjectFiles.Empty do
  296. begin
  297. s:=ObjectFiles.Get;
  298. if s<>'' then
  299. WriteRes(s);
  300. end;
  301. if linklibc then
  302. begin
  303. if s2<>'' then
  304. begin
  305. WriteRes(s2+'crtend.o');
  306. WriteRes(s2+'crtn.o');
  307. end;
  308. end;
  309. { Write sharedlibraries like -l<lib> }
  310. While not SharedLibFiles.Empty do
  311. begin
  312. S:=SharedLibFiles.Get;
  313. i:=Pos(target_os.sharedlibext,S);
  314. if i>0 then
  315. Delete(S,i,255);
  316. WriteRes(target_link.libprefix+s);
  317. end;
  318. WriteRes(target_link.inputend);
  319. { Write staticlibraries }
  320. if not StaticLibFiles.Empty then
  321. begin
  322. WriteRes(target_link.GroupStart);
  323. While not StaticLibFiles.Empty do
  324. begin
  325. S:=StaticLibFiles.Get;
  326. WriteRes(s)
  327. end;
  328. WriteRes(target_link.GroupEnd);
  329. end;
  330. { Close response }
  331. close(linkresponse);
  332. WriteResponseFile:=True;
  333. end;
  334. function TLinker.MakeExecutable:boolean;
  335. var
  336. bindbin : string[80];
  337. bindfound : boolean;
  338. s : string;
  339. success : boolean;
  340. begin
  341. {$ifdef linux}
  342. if LinkToC then
  343. begin
  344. AddObject('/usr/lib/crt0.o');
  345. AddObject('lprt');
  346. AddStaticLibrary('libc.a');
  347. AddStaticLibrary('libgcc.a');
  348. end;
  349. {$endif Linux}
  350. { Write used files and libraries }
  351. WriteResponseFile;
  352. { Call linker }
  353. if not(cs_link_extern in aktglobalswitches) then
  354. Message1(exec_i_linking,current_module^.exefilename^);
  355. s:=target_link.linkcmd;
  356. Replace(s,'$EXE',current_module^.exefilename^);
  357. Replace(s,'$OPT',LinkOptions);
  358. Replace(s,'$RES',inputdir+LinkResName);
  359. success:=DoExec(FindLinker,s,true,false);
  360. {Bind}
  361. if target_link.bindbin<>'' then
  362. begin
  363. s:=target_link.bindcmd;
  364. Replace(s,'$EXE',current_module^.exefilename^);
  365. Replace(s,'$HEAPKB',tostr((heapsize+1023) shr 10));
  366. Replace(s,'$STACKKB',tostr((stacksize+1023) shr 10));
  367. bindbin:=FindExe(target_link.bindbin,bindfound);
  368. if (not bindfound) and not(cs_link_extern in aktglobalswitches) then
  369. begin
  370. Message1(exec_w_binder_not_found,bindbin);
  371. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  372. end;
  373. DoExec(bindbin,s,false,false);
  374. end;
  375. {Remove ReponseFile}
  376. if (success) and not(cs_link_extern in aktglobalswitches) then
  377. RemoveFile(LinkResName);
  378. MakeExecutable:=success; { otherwise a recursive call to link method }
  379. end;
  380. Procedure TLinker.MakeStaticLibrary(filescnt:longint);
  381. {
  382. FilesCnt holds the amount of .o files created, if filescnt=0 then
  383. no smartlinking is used
  384. }
  385. var
  386. smartpath,
  387. s,
  388. arbin : string;
  389. arfound : boolean;
  390. cnt : longint;
  391. begin
  392. smartpath:=current_module^.path^+FixPath(FixFileName(current_module^.modulename^)+target_info.smartext);
  393. { find ar binary }
  394. arbin:=FindExe(target_ar.arbin,arfound);
  395. if (not arfound) and not(cs_link_extern in aktglobalswitches) then
  396. begin
  397. Message(exec_w_ar_not_found);
  398. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  399. end;
  400. s:=target_ar.arcmd;
  401. Replace(s,'$LIB',current_module^.staticlibfilename^);
  402. if filescnt=0 then
  403. Replace(s,'$FILES',current_module^.objfilename^)
  404. else
  405. Replace(s,'$FILES',FixFileName(smartpath+current_module^.asmprefix^+'*'+target_info.objext));
  406. DoExec(arbin,s,false,true);
  407. { Clean up }
  408. if not(cs_asm_leave in aktglobalswitches) and not(cs_link_extern in aktglobalswitches) then
  409. begin
  410. if filescnt=0 then
  411. RemoveFile(current_module^.objfilename^)
  412. else
  413. begin
  414. for cnt:=1 to filescnt do
  415. RemoveFile(FixFileName(smartpath+current_module^.asmprefix^+tostr(cnt)+target_info.objext));
  416. RemoveDir(smartpath);
  417. end;
  418. end;
  419. end;
  420. Procedure TLinker.MakeSharedLibrary;
  421. var
  422. s : string;
  423. begin
  424. s:=' -shared -o $LIB $FILES';
  425. Replace(s,'$LIB',current_module^.sharedlibfilename^);
  426. Replace(s,'$FILES',current_module^.objfilename^);
  427. if DoExec(FindLinker,s,false,false) then
  428. RemoveFile(current_module^.objfilename^);
  429. end;
  430. end.
  431. {
  432. $Log$
  433. Revision 1.20 1998-08-19 10:06:14 peter
  434. * fixed filenames and removedir which supports slash at the end
  435. Revision 1.19 1998/08/17 09:17:47 peter
  436. * static/shared linking updates
  437. Revision 1.18 1998/08/14 21:56:34 peter
  438. * setting the outputfile using -o works now to create static libs
  439. Revision 1.17 1998/08/14 18:16:08 peter
  440. * return after a failed call will now add it to ppas
  441. Revision 1.16 1998/08/12 19:28:15 peter
  442. * better libc support
  443. Revision 1.15 1998/08/10 14:50:02 peter
  444. + localswitches, moduleswitches, globalswitches splitting
  445. Revision 1.14 1998/06/17 14:10:13 peter
  446. * small os2 fixes
  447. * fixed interdependent units with newppu (remake3 under linux works now)
  448. Revision 1.13 1998/06/08 22:59:46 peter
  449. * smartlinking works for win32
  450. * some defines to exclude some compiler parts
  451. Revision 1.12 1998/06/04 23:51:44 peter
  452. * m68k compiles
  453. + .def file creation moved to gendef.pas so it could also be used
  454. for win32
  455. Revision 1.11 1998/05/27 00:20:31 peter
  456. * some scanner optimizes
  457. * automaticly aout2exe for go32v1
  458. * fixed dynamiclinker option which was added at the wrong place
  459. Revision 1.10 1998/05/22 12:32:47 peter
  460. * fixed -L on the commandline, Dos commandline is only 128 bytes
  461. Revision 1.9 1998/05/12 10:46:59 peter
  462. * moved printstatus to verb_def
  463. + V_Normal which is between V_Error and V_Warning and doesn't have a
  464. prefix like error: warning: and is included in V_Default
  465. * fixed some messages
  466. * first time parameter scan is only for -v and -T
  467. - removed old style messages
  468. Revision 1.8 1998/05/11 13:07:54 peter
  469. + $ifdef NEWPPU for the new ppuformat
  470. + $define GDB not longer required
  471. * removed all warnings and stripped some log comments
  472. * no findfirst/findnext anymore to remove smartlink *.o files
  473. Revision 1.7 1998/05/08 09:21:20 michael
  474. * Added missing -Fl message to messages file.
  475. * Corrected mangling of file names when doing Linklib
  476. * -Fl now actually WORKS.
  477. * Librarysearchpath is now a field in linker object.
  478. Revision 1.6 1998/05/06 09:26:49 peter
  479. * fixed ld call with shell
  480. Revision 1.4 1998/05/04 17:54:25 peter
  481. + smartlinking works (only case jumptable left todo)
  482. * redesign of systems.pas to support assemblers and linkers
  483. + Unitname is now also in the PPU-file, increased version to 14
  484. Revision 1.3 1998/04/16 10:54:30 daniel
  485. * Fixed linking for OS/2.
  486. }