link.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. LibrarySearchPath:='';
  93. {$ifdef linux}
  94. DynamicLinker:='/lib/ld-linux.so.1';
  95. {$else}
  96. DynamicLinker:='';
  97. {$endif}
  98. LinkResName:='link.res';
  99. end;
  100. Destructor TLinker.Done;
  101. begin
  102. end;
  103. Procedure TLinker.SetOutputName(const s:string);
  104. begin
  105. OutputName:=s;
  106. end;
  107. Procedure TLinker.SetExeName(const s:string);
  108. var
  109. path : dirstr;
  110. name : namestr;
  111. ext : extstr;
  112. begin
  113. if OutputName='' then
  114. begin
  115. FSplit(s,path,name,ext);
  116. ExeName:=Path+Name+target_info.ExeExt;
  117. end
  118. else
  119. ExeName:=OutputName;
  120. end;
  121. Procedure TLinker.SetLibName(const s:string);
  122. var
  123. path : dirstr;
  124. name : namestr;
  125. ext : extstr;
  126. begin
  127. if OutputName='' then
  128. begin
  129. FSplit(s,path,name,ext);
  130. SharedLibName:=Path+Name+target_os.SharedLibExt;
  131. StaticLibName:=Path+Name+target_os.StaticLibExt;
  132. end
  133. else
  134. begin
  135. SharedLibName:=OutputName;
  136. StaticLibName:=OutputName;
  137. end;
  138. end;
  139. var
  140. LastLDBin : string;
  141. Function TLinker.FindLinker:string;
  142. var
  143. ldfound : boolean;
  144. begin
  145. if LastLDBin='' then
  146. begin
  147. LastLDBin:=FindExe(target_link.linkbin,ldfound);
  148. if (not ldfound) and (not externlink) then
  149. begin
  150. Message1(exec_w_linker_not_found,LastLDBin);
  151. externlink:=true;
  152. end;
  153. if ldfound then
  154. Message1(exec_u_using_linker,LastLDBin);
  155. end;
  156. FindLinker:=LastLDBin;
  157. end;
  158. { searches an object file }
  159. function TLinker.FindObjectFile(s:string) : string;
  160. var
  161. found : boolean;
  162. begin
  163. if pos('.',s)=0 then
  164. s:=s+target_info.objext;
  165. s:=FixFileName(s);
  166. if FileExists(s) then
  167. begin
  168. Findobjectfile:=s;
  169. exit;
  170. end;
  171. findobjectfile:=search(s,'.;'+unitsearchpath+';'+exepath,found)+s;
  172. if (not externlink) and (not found) then
  173. Message1(exec_w_objfile_not_found,s);
  174. end;
  175. { searches an library file }
  176. function TLinker.FindLibraryFile(s:string;const ext:string) : string;
  177. var
  178. found : boolean;
  179. begin
  180. if pos('.',s)=0 then
  181. s:=s+ext;
  182. if FileExists(s) then
  183. begin
  184. FindLibraryFile:=s;
  185. exit;
  186. end;
  187. findlibraryfile:=search(s,'.;'+librarysearchpath+';'+exepath,found)+s;
  188. if (not externlink) and (not found) then
  189. Message1(exec_w_libfile_not_found,s);
  190. end;
  191. Procedure TLinker.AddObject(const S : String);
  192. begin
  193. ObjectFiles.Insert(FindObjectFile(s));
  194. end;
  195. Procedure TLinker.AddSharedLibrary(const S:String);
  196. begin
  197. SharedLibFiles.Insert (S);
  198. { SharedLibFiles.Insert(FindLibraryFile(s,target_os.sharedlibext)); }
  199. end;
  200. Procedure TLinker.AddStaticLibrary(const S:String);
  201. begin
  202. StaticLibFiles.Insert(FindLibraryFile(s,target_os.staticlibext));
  203. end;
  204. Function TLinker.DoExec(const command,para:string;info,useshell:boolean):boolean;
  205. begin
  206. DoExec:=true;
  207. if not externlink then
  208. begin
  209. swapvectors;
  210. if useshell then
  211. shell(command+' '+para)
  212. else
  213. exec(command,para);
  214. swapvectors;
  215. if (dosexitcode<>0) then
  216. begin
  217. Message(exec_w_error_while_linking);
  218. DoExec:=false;
  219. exit;
  220. end
  221. else
  222. if (dosError<>0) then
  223. begin
  224. Message(exec_w_cant_call_linker);
  225. ExternLink:=true;
  226. end;
  227. end;
  228. if externlink then
  229. begin
  230. if info then
  231. AsmRes.AddLinkCommand(Command,Para,ExeName)
  232. else
  233. AsmRes.AddLinkCommand(Command,Para,'');
  234. end;
  235. end;
  236. Function TLinker.WriteResponseFile : Boolean;
  237. Var
  238. LinkResponse : Text;
  239. i : longint;
  240. prtobj,s,s2 : string;
  241. begin
  242. WriteResponseFile:=False;
  243. { set special options for some targets }
  244. prtobj:='prt0';
  245. case target_info.target of
  246. {$ifdef i386}
  247. target_Win32 : prtobj:='';
  248. target_linux : begin
  249. if cs_profile in aktswitches then
  250. begin
  251. prtobj:='gprt0';
  252. AddSharedLibrary('gmon');
  253. AddSharedLibrary('c');
  254. end;
  255. end;
  256. {$endif i386}
  257. {$ifdef m68k}
  258. target_linux : begin
  259. if cs_profile in aktswitches then
  260. begin
  261. prtobj:='gprt0';
  262. AddSharedLibrary('gmon');
  263. AddSharedLibrary('c');
  264. end;
  265. end;
  266. {$endif}
  267. end;
  268. { Fix command line options }
  269. If not SharedLibFiles.Empty then
  270. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  271. if Strip then
  272. LinkOptions:=LinkOptions+target_link.stripopt;
  273. { Open linkresponse and write header }
  274. assign(linkresponse,inputdir+LinkResName);
  275. {$I-}
  276. rewrite(linkresponse);
  277. {$I+}
  278. if ioresult<>0 then
  279. exit;
  280. { Write library searchpath }
  281. S2:=LibrarySearchPath;
  282. Repeat
  283. i:=Pos(';',S2);
  284. If i=0 then
  285. i:=255;
  286. S:=Copy(S2,1,i-1);
  287. If S<>'' then
  288. WriteLn(linkresponse,target_link.libpathprefix+s+target_link.libpathsuffix);
  289. Delete (S2,1,i);
  290. until S2='';
  291. writeln(linkresponse,target_link.inputstart);
  292. { add objectfiles, start with prt0 always }
  293. if prtobj<>'' then
  294. Writeln(linkresponse,FindObjectFile(prtobj));
  295. while not ObjectFiles.Empty do
  296. begin
  297. s:=ObjectFiles.Get;
  298. if s<>'' then
  299. Writeln(linkresponse,s);
  300. end;
  301. { Write sharedlibraries like -l<lib> }
  302. While not SharedLibFiles.Empty do
  303. begin
  304. S:=SharedLibFiles.Get;
  305. i:=Pos(target_os.sharedlibext,S);
  306. if i>0 then
  307. Delete(S,i,255);
  308. writeln(linkresponse,target_link.libprefix+s);
  309. end;
  310. writeln(linkresponse,target_link.inputend);
  311. { Write staticlibraries }
  312. if not StaticLibFiles.Empty then
  313. begin
  314. Writeln(LinkResponse,target_link.GroupStart);
  315. While not StaticLibFiles.Empty do
  316. begin
  317. S:=StaticLibFiles.Get;
  318. writeln(linkresponse,s)
  319. end;
  320. Writeln(LinkResponse,target_link.GroupEnd);
  321. end;
  322. { Close response }
  323. close(linkresponse);
  324. WriteResponseFile:=True;
  325. end;
  326. function TLinker.MakeExecutable:boolean;
  327. var
  328. bindbin : string[80];
  329. bindfound : boolean;
  330. i : longint;
  331. s : string;
  332. dummy : file;
  333. success : boolean;
  334. begin
  335. {$ifdef linux}
  336. if LinkToC then
  337. begin
  338. AddObject('/usr/lib/crt0.o');
  339. AddObject('lprt');
  340. AddStaticLibrary('libc.a');
  341. AddStaticLibrary('libgcc.a');
  342. end;
  343. {$endif Linux}
  344. { Write used files and libraries }
  345. WriteResponseFile;
  346. { Call linker }
  347. if not externlink then
  348. Message1(exec_i_linking,ExeName);
  349. s:=target_link.linkcmd;
  350. Replace(s,'$EXE',exename);
  351. Replace(s,'$OPT',LinkOptions);
  352. Replace(s,'$RES',inputdir+LinkResName);
  353. success:=DoExec(FindLinker,s,true,false);
  354. {Bind}
  355. if target_link.bindbin<>'' then
  356. begin
  357. s:=target_link.bindcmd;
  358. Replace(s,'$EXE',exename);
  359. Replace(s,'$HEAPKB',tostr((heapsize+1023) shr 10));
  360. Replace(s,'$STACKKB',tostr((stacksize+1023) shr 10));
  361. bindbin:=FindExe(target_link.bindbin,bindfound);
  362. if (not bindfound) and (not externlink) then
  363. begin
  364. Message1(exec_w_binder_not_found,bindbin);
  365. externlink:=true;
  366. end;
  367. DoExec(bindbin,s,false,false);
  368. end;
  369. {Remove ReponseFile}
  370. if (success) and (not externlink) then
  371. begin
  372. assign(dummy,LinkResName);
  373. {$I-}
  374. erase(dummy);
  375. {$I+}
  376. i:=ioresult;
  377. end;
  378. MakeExecutable:=success; { otherwise a recursive call to link method }
  379. end;
  380. Procedure TLinker.MakeStaticLibrary(const path:string;filescnt:longint);
  381. var
  382. arbin : string;
  383. arfound : boolean;
  384. cnt : longint;
  385. i : word;
  386. f : file;
  387. begin
  388. arbin:=FindExe('ar',arfound);
  389. if (not arfound) and (not externlink) then
  390. begin
  391. Message(exec_w_ar_not_found);
  392. externlink:=true;
  393. end;
  394. DoExec(arbin,'rs '+staticlibname+' '+FixPath(path)+'*'+target_info.objext,false,true);
  395. { Clean up }
  396. if (not writeasmfile) and (not externlink) then
  397. begin
  398. for cnt:=1to filescnt do
  399. begin
  400. assign(f,FixPath(path)+'as'+tostr(cnt)+target_info.objext);
  401. {$I-}
  402. erase(f);
  403. {$I+}
  404. i:=ioresult;
  405. end;
  406. {$I-}
  407. rmdir(path);
  408. {$I+}
  409. i:=ioresult;
  410. end;
  411. end;
  412. Procedure TLinker.MakeSharedLibrary;
  413. begin
  414. DoExec(FindLinker,' -shared -o '+sharedlibname+' link.res',false,false);
  415. end;
  416. end.
  417. {
  418. $Log$
  419. Revision 1.12 1998-06-04 23:51:44 peter
  420. * m68k compiles
  421. + .def file creation moved to gendef.pas so it could also be used
  422. for win32
  423. Revision 1.11 1998/05/27 00:20:31 peter
  424. * some scanner optimizes
  425. * automaticly aout2exe for go32v1
  426. * fixed dynamiclinker option which was added at the wrong place
  427. Revision 1.10 1998/05/22 12:32:47 peter
  428. * fixed -L on the commandline, Dos commandline is only 128 bytes
  429. Revision 1.9 1998/05/12 10:46:59 peter
  430. * moved printstatus to verb_def
  431. + V_Normal which is between V_Error and V_Warning and doesn't have a
  432. prefix like error: warning: and is included in V_Default
  433. * fixed some messages
  434. * first time parameter scan is only for -v and -T
  435. - removed old style messages
  436. Revision 1.8 1998/05/11 13:07:54 peter
  437. + $ifdef NEWPPU for the new ppuformat
  438. + $define GDB not longer required
  439. * removed all warnings and stripped some log comments
  440. * no findfirst/findnext anymore to remove smartlink *.o files
  441. Revision 1.7 1998/05/08 09:21:20 michael
  442. * Added missing -Fl message to messages file.
  443. * Corrected mangling of file names when doing Linklib
  444. * -Fl now actually WORKS.
  445. * Librarysearchpath is now a field in linker object.
  446. Revision 1.6 1998/05/06 09:26:49 peter
  447. * fixed ld call with shell
  448. Revision 1.4 1998/05/04 17:54:25 peter
  449. + smartlinking works (only case jumptable left todo)
  450. * redesign of systems.pas to support assemblers and linkers
  451. + Unitname is now also in the PPU-file, increased version to 14
  452. Revision 1.3 1998/04/16 10:54:30 daniel
  453. * Fixed linking for OS/2.
  454. }