link.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 linux}
  57. ,linux
  58. {$endif}
  59. ,dos
  60. ,win_targ;
  61. {$ifndef linux}
  62. Procedure Shell(command:string);
  63. { This is already defined in the linux.ppu for linux, need for the *
  64. expansion under linux }
  65. var
  66. comspec : string;
  67. begin
  68. comspec:=getenv('COMSPEC');
  69. Exec(comspec,' /C '+command);
  70. end;
  71. {$endif}
  72. Constructor TLinker.Init;
  73. begin
  74. ObjectFiles.Init;
  75. SharedLibFiles.Init;
  76. StaticLibFiles.Init;
  77. ObjectFiles.Doubles:=False;
  78. SharedLibFiles.Doubles:=False;
  79. StaticLibFiles.Doubles:=False;
  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. end;
  101. procedure TLinker.AddModuleFiles(hp:pmodule);
  102. begin
  103. with hp^ do
  104. begin
  105. while not linkofiles.empty do
  106. AddObject(linkofiles.Get);
  107. while not linksharedlibs.empty do
  108. AddSharedLibrary(linksharedlibs.Get);
  109. while not linkstaticlibs.empty do
  110. AddStaticLibrary(linkstaticlibs.Get);
  111. end;
  112. end;
  113. var
  114. LastLDBin : string;
  115. Function TLinker.FindLinker:string;
  116. var
  117. ldfound : boolean;
  118. begin
  119. if LastLDBin='' then
  120. begin
  121. LastLDBin:=FindExe(target_link.linkbin,ldfound);
  122. if (not ldfound) and not(cs_link_extern in aktglobalswitches) then
  123. begin
  124. Message1(exec_w_linker_not_found,LastLDBin);
  125. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  126. end;
  127. if ldfound then
  128. Message1(exec_u_using_linker,LastLDBin);
  129. end;
  130. FindLinker:=LastLDBin;
  131. end;
  132. { searches an object file }
  133. function TLinker.FindObjectFile(s:string) : string;
  134. var
  135. found : boolean;
  136. begin
  137. if pos('.',s)=0 then
  138. s:=s+target_info.objext;
  139. s:=FixFileName(s);
  140. if FileExists(s) then
  141. begin
  142. Findobjectfile:=s;
  143. exit;
  144. end;
  145. findobjectfile:=search(s,'.;'+unitsearchpath+';'+exepath,found)+s;
  146. if not(cs_link_extern in aktglobalswitches) and (not found) then
  147. Message1(exec_w_objfile_not_found,s);
  148. end;
  149. { searches an library file }
  150. function TLinker.FindLibraryFile(s:string;const ext:string) : string;
  151. var
  152. found : boolean;
  153. begin
  154. if pos('.',s)=0 then
  155. s:=s+ext;
  156. if FileExists(s) then
  157. begin
  158. FindLibraryFile:=s;
  159. exit;
  160. end;
  161. findlibraryfile:=search(s,'.;'+librarysearchpath+';'+exepath,found)+s;
  162. if not(cs_link_extern in aktglobalswitches) and (not found) then
  163. Message1(exec_w_libfile_not_found,s);
  164. end;
  165. Procedure TLinker.AddObject(const S : String);
  166. begin
  167. ObjectFiles.Insert(FindObjectFile(s));
  168. end;
  169. Procedure TLinker.AddSharedLibrary(S:String);
  170. begin
  171. { remove prefix 'lib' }
  172. if Copy(s,1,length(target_os.libprefix))=target_os.libprefix then
  173. Delete(s,1,length(target_os.libprefix));
  174. { remove extension if any }
  175. if Copy(s,length(s)-length(target_os.sharedlibext)+1,length(target_os.sharedlibext))=target_os.sharedlibext then
  176. Delete(s,length(s)-length(target_os.sharedlibext)+1,length(target_os.sharedlibext)+1);
  177. { ready to be inserted }
  178. SharedLibFiles.Insert (S);
  179. end;
  180. Procedure TLinker.AddStaticLibrary(const S:String);
  181. begin
  182. StaticLibFiles.Insert(FindLibraryFile(s,target_os.staticlibext));
  183. end;
  184. Function TLinker.DoExec(const command,para:string;info,useshell:boolean):boolean;
  185. begin
  186. DoExec:=true;
  187. if not(cs_link_extern in aktglobalswitches) then
  188. begin
  189. swapvectors;
  190. if useshell then
  191. shell(command+' '+para)
  192. else
  193. exec(command,para);
  194. swapvectors;
  195. if (dosexitcode<>0) then
  196. begin
  197. Message(exec_w_error_while_linking);
  198. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  199. DoExec:=false;
  200. end
  201. else
  202. if (dosError<>0) then
  203. begin
  204. Message(exec_w_cant_call_linker);
  205. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  206. DoExec:=false;
  207. end;
  208. end;
  209. { Update asmres when externmode is set }
  210. if cs_link_extern in aktglobalswitches then
  211. begin
  212. if info then
  213. AsmRes.AddLinkCommand(Command,Para,current_module^.exefilename^)
  214. else
  215. AsmRes.AddLinkCommand(Command,Para,'');
  216. end;
  217. end;
  218. Function TLinker.WriteResponseFile : Boolean;
  219. Var
  220. LinkResponse : Text;
  221. i : longint;
  222. prtobj,s,s2 : string;
  223. found,
  224. linklibc : boolean;
  225. procedure WriteRes(const s:string);
  226. begin
  227. if s<>'' then
  228. WriteLn(Linkresponse,s);
  229. end;
  230. begin
  231. WriteResponseFile:=False;
  232. { set special options for some targets }
  233. linklibc:=SharedLibFiles.Find('c');
  234. prtobj:='prt0';
  235. case target_info.target of
  236. {$ifdef i386}
  237. target_Win32 : prtobj:='';
  238. target_linux : begin
  239. if cs_profile in aktmoduleswitches then
  240. begin
  241. prtobj:='gprt0';
  242. if not glibc2 then
  243. AddSharedLibrary('gmon');
  244. AddSharedLibrary('c');
  245. linklibc:=true;
  246. end
  247. else
  248. begin
  249. if linklibc then
  250. prtobj:='cprt0';
  251. end;
  252. end;
  253. {$endif i386}
  254. {$ifdef m68k}
  255. target_Palmos : prtobj:='';
  256. target_linux : begin
  257. if cs_profile in aktmoduleswitches then
  258. begin
  259. prtobj:='gprt0';
  260. AddSharedLibrary('gmon');
  261. AddSharedLibrary('c');
  262. end;
  263. end;
  264. {$endif}
  265. end;
  266. { Fix command line options }
  267. If not SharedLibFiles.Empty then
  268. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  269. if Strip and not(cs_debuginfo in aktmoduleswitches) then
  270. LinkOptions:=LinkOptions+target_link.stripopt;
  271. { Open linkresponse and write header }
  272. assign(linkresponse,inputdir+LinkResName);
  273. {$I-}
  274. rewrite(linkresponse);
  275. {$I+}
  276. if ioresult<>0 then
  277. exit;
  278. { Write library searchpath }
  279. S2:=LibrarySearchPath;
  280. Repeat
  281. i:=Pos(';',S2);
  282. If i=0 then
  283. i:=255;
  284. S:=Copy(S2,1,i-1);
  285. If S<>'' then
  286. WriteRes(target_link.libpathprefix+s+target_link.libpathsuffix);
  287. Delete (S2,1,i);
  288. until S2='';
  289. WriteRes(target_link.inputstart);
  290. { add objectfiles, start with prt0 always }
  291. if prtobj<>'' then
  292. WriteRes(FindObjectFile(prtobj));
  293. if linklibc then
  294. begin
  295. WriteRes(search('crti.o',librarysearchpath,found)+'crti.o');
  296. WriteRes(search('crtbegin.o',librarysearchpath,found)+'crtbegin.o');
  297. end;
  298. while not ObjectFiles.Empty do
  299. begin
  300. s:=ObjectFiles.Get;
  301. if s<>'' then
  302. WriteRes(s);
  303. end;
  304. if linklibc then
  305. begin
  306. WriteRes(search('crtend.o',librarysearchpath,found)+'crtend.o');
  307. WriteRes(search('crtn.o',librarysearchpath,found)+'crtn.o');
  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. if target_info.target=target_Win32 then
  376. win_targ.postprocessexecutable;
  377. {Remove ReponseFile}
  378. if (success) and not(cs_link_extern in aktglobalswitches) then
  379. RemoveFile(LinkResName);
  380. MakeExecutable:=success; { otherwise a recursive call to link method }
  381. end;
  382. Procedure TLinker.MakeStaticLibrary(filescnt:longint);
  383. {
  384. FilesCnt holds the amount of .o files created, if filescnt=0 then
  385. no smartlinking is used
  386. }
  387. var
  388. smartpath,
  389. s,
  390. arbin : string;
  391. arfound : boolean;
  392. cnt : longint;
  393. begin
  394. smartpath:=current_module^.path^+FixPath(FixFileName(current_module^.modulename^)+target_info.smartext);
  395. { find ar binary }
  396. arbin:=FindExe(target_ar.arbin,arfound);
  397. if (not arfound) and not(cs_link_extern in aktglobalswitches) then
  398. begin
  399. Message(exec_w_ar_not_found);
  400. aktglobalswitches:=aktglobalswitches+[cs_link_extern];
  401. end;
  402. s:=target_ar.arcmd;
  403. Replace(s,'$LIB',current_module^.staticlibfilename^);
  404. if filescnt=0 then
  405. Replace(s,'$FILES',current_module^.objfilename^)
  406. else
  407. Replace(s,'$FILES',FixFileName(smartpath+current_module^.asmprefix^+'*'+target_info.objext));
  408. DoExec(arbin,s,false,true);
  409. { Clean up }
  410. if not(cs_asm_leave in aktglobalswitches) and not(cs_link_extern in aktglobalswitches) then
  411. begin
  412. if filescnt=0 then
  413. RemoveFile(current_module^.objfilename^)
  414. else
  415. begin
  416. for cnt:=1 to filescnt do
  417. RemoveFile(FixFileName(smartpath+current_module^.asmprefix^+tostr(cnt)+target_info.objext));
  418. RemoveDir(smartpath);
  419. end;
  420. end;
  421. end;
  422. Procedure TLinker.MakeSharedLibrary;
  423. var
  424. s : string;
  425. begin
  426. s:=' -shared -o $LIB $FILES';
  427. Replace(s,'$LIB',current_module^.sharedlibfilename^);
  428. Replace(s,'$FILES',current_module^.objfilename^);
  429. if DoExec(FindLinker,s,false,false) then
  430. RemoveFile(current_module^.objfilename^);
  431. end;
  432. end.
  433. {
  434. $Log$
  435. Revision 1.23 1998-09-03 17:39:04 florian
  436. + better code for type conversation longint/dword to real type
  437. Revision 1.22 1998/09/01 09:01:00 peter
  438. + glibc2 support
  439. Revision 1.21 1998/08/31 12:26:26 peter
  440. * m68k and palmos updates from surebugfixes
  441. Revision 1.20 1998/08/19 10:06:14 peter
  442. * fixed filenames and removedir which supports slash at the end
  443. Revision 1.19 1998/08/17 09:17:47 peter
  444. * static/shared linking updates
  445. Revision 1.18 1998/08/14 21:56:34 peter
  446. * setting the outputfile using -o works now to create static libs
  447. Revision 1.17 1998/08/14 18:16:08 peter
  448. * return after a failed call will now add it to ppas
  449. Revision 1.16 1998/08/12 19:28:15 peter
  450. * better libc support
  451. Revision 1.15 1998/08/10 14:50:02 peter
  452. + localswitches, moduleswitches, globalswitches splitting
  453. Revision 1.14 1998/06/17 14:10:13 peter
  454. * small os2 fixes
  455. * fixed interdependent units with newppu (remake3 under linux works now)
  456. Revision 1.13 1998/06/08 22:59:46 peter
  457. * smartlinking works for win32
  458. * some defines to exclude some compiler parts
  459. Revision 1.12 1998/06/04 23:51:44 peter
  460. * m68k compiles
  461. + .def file creation moved to gendef.pas so it could also be used
  462. for win32
  463. Revision 1.11 1998/05/27 00:20:31 peter
  464. * some scanner optimizes
  465. * automaticly aout2exe for go32v1
  466. * fixed dynamiclinker option which was added at the wrong place
  467. Revision 1.10 1998/05/22 12:32:47 peter
  468. * fixed -L on the commandline, Dos commandline is only 128 bytes
  469. Revision 1.9 1998/05/12 10:46:59 peter
  470. * moved printstatus to verb_def
  471. + V_Normal which is between V_Error and V_Warning and doesn't have a
  472. prefix like error: warning: and is included in V_Default
  473. * fixed some messages
  474. * first time parameter scan is only for -v and -T
  475. - removed old style messages
  476. Revision 1.8 1998/05/11 13:07:54 peter
  477. + $ifdef NEWPPU for the new ppuformat
  478. + $define GDB not longer required
  479. * removed all warnings and stripped some log comments
  480. * no findfirst/findnext anymore to remove smartlink *.o files
  481. Revision 1.7 1998/05/08 09:21:20 michael
  482. * Added missing -Fl message to messages file.
  483. * Corrected mangling of file names when doing Linklib
  484. * -Fl now actually WORKS.
  485. * Librarysearchpath is now a field in linker object.
  486. Revision 1.6 1998/05/06 09:26:49 peter
  487. * fixed ld call with shell
  488. Revision 1.4 1998/05/04 17:54:25 peter
  489. + smartlinking works (only case jumptable left todo)
  490. * redesign of systems.pas to support assemblers and linkers
  491. + Unitname is now also in the PPU-file, increased version to 14
  492. Revision 1.3 1998/04/16 10:54:30 daniel
  493. * Fixed linking for OS/2.
  494. }