link.pas 14 KB

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