link.pas 16 KB

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