link.pas 15 KB

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