link.pas 14 KB

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