link.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. ExeName, { FileName of the exe to be created }
  30. SharedLibName,
  31. StaticLibName, { FileName of the lib to be created }
  32. LinkOptions : string; { Additional options to the linker }
  33. DynamicLinker : String[80]; { What Dynamic linker ? }
  34. LinkResName : String[32]; { Name of response file }
  35. { Methods }
  36. Constructor Init;
  37. Destructor Done;
  38. Procedure SetExeName(const s:string);
  39. Procedure SetLibName(const s:string);
  40. function FindObjectFile(s : string) : string;
  41. function FindLibraryFile(s:string;const ext:string) : string;
  42. Procedure AddObject(const S : String);
  43. Procedure AddStaticLibrary(const S : String);
  44. Procedure AddSharedLibrary(const S : String);
  45. Function FindLinker:String; { Find linker, sets Name }
  46. Function DoExec(const command,para:string;info:boolean):boolean;
  47. Function WriteResponseFile:Boolean;
  48. Function MakeExecutable:boolean;
  49. Procedure MakeStaticLibrary(const path:string);
  50. Procedure MakeSharedLibrary;
  51. end;
  52. PLinker=^TLinker;
  53. Var
  54. Linker : TLinker;
  55. Implementation
  56. uses
  57. Script,globals,systems,verbose
  58. {$ifdef linux}
  59. ,linux
  60. {$endif}
  61. ,dos;
  62. {$ifndef linux}
  63. Procedure Shell(command:string);
  64. { This is already defined in the linux.ppu for linux, need for the *
  65. expansion under linux }
  66. var
  67. comspec : string;
  68. begin
  69. comspec:=getenv('COMSPEC');
  70. Exec(comspec,' /C '+command);
  71. end;
  72. {$endif}
  73. Constructor TLinker.Init;
  74. begin
  75. ObjectFiles.Init;
  76. SharedLibFiles.Init;
  77. StaticLibFiles.Init;
  78. ObjectFiles.Doubles:=False;
  79. SharedLibFiles.Doubles:=False;
  80. StaticLibFiles.Doubles:=False;
  81. LinkToC:=False;
  82. Strip:=false;
  83. LinkOptions:='';
  84. ExeName:='';
  85. SharedLibName:='';
  86. StaticLibName:='';
  87. LibrarySearchPath:='';
  88. ObjectSearchPath:='';
  89. {$ifdef linux}
  90. DynamicLinker:='/lib/ld-linux.so.1';
  91. {$else}
  92. DynamicLinker:='';
  93. {$endif}
  94. LinkResName:='link.res';
  95. end;
  96. Destructor TLinker.Done;
  97. begin
  98. end;
  99. Procedure TLinker.SetExeName(const s:string);
  100. var
  101. path : dirstr;
  102. name : namestr;
  103. ext : extstr;
  104. begin
  105. FSplit(s,path,name,ext);
  106. ExeName:=Path+Name+target_os.ExeExt;
  107. end;
  108. Procedure TLinker.SetLibName(const s:string);
  109. var
  110. path : dirstr;
  111. name : namestr;
  112. ext : extstr;
  113. begin
  114. FSplit(s,path,name,ext);
  115. SharedLibName:=Path+Name+target_os.SharedLibExt;
  116. StaticLibName:=Path+Name+target_os.StaticLibExt;
  117. end;
  118. var
  119. LastLDBin : string;
  120. Function TLinker.FindLinker:string;
  121. var
  122. ldfound : boolean;
  123. begin
  124. if LastLDBin='' then
  125. begin
  126. LastLDBin:=FindExe(target_link.linkbin,ldfound);
  127. if (not ldfound) and (not externlink) then
  128. begin
  129. Message1(exec_w_linker_not_found,LastLDBin);
  130. externlink:=true;
  131. end;
  132. if ldfound then
  133. Message1(exec_u_using_linker,LastLDBin);
  134. end;
  135. FindLinker:=LastLDBin;
  136. end;
  137. { searches an object file }
  138. function TLinker.FindObjectFile(s:string) : string;
  139. var
  140. found : boolean;
  141. begin
  142. if pos('.',s)=0 then
  143. s:=s+target_info.objext;
  144. s:=FixFileName(s);
  145. if FileExists(s) then
  146. begin
  147. Findobjectfile:=s;
  148. exit;
  149. end;
  150. findobjectfile:=search(s,'.;'+unitsearchpath+';'+exepath,found)+s;
  151. if (not externlink) and (not found) then
  152. Message1(exec_w_objfile_not_found,s);
  153. end;
  154. { searches an library file }
  155. function TLinker.FindLibraryFile(s:string;const ext:string) : string;
  156. var
  157. found : boolean;
  158. begin
  159. if pos('.',s)=0 then
  160. s:=s+ext;
  161. s:=FixFileName(s);
  162. if FileExists(s) then
  163. begin
  164. FindLibraryFile:=s;
  165. exit;
  166. end;
  167. findlibraryfile:=search(s,'.;'+librarysearchpath+';'+exepath,found)+s;
  168. if (not externlink) and (not found) then
  169. Message1(exec_w_libfile_not_found,s);
  170. end;
  171. Procedure TLinker.AddObject(const S : String);
  172. begin
  173. ObjectFiles.Insert(FindObjectFile(s));
  174. end;
  175. Procedure TLinker.AddSharedLibrary(const S:String);
  176. begin
  177. SharedLibFiles.Insert (S);
  178. { SharedLibFiles.Insert(FindLibraryFile(s,target_os.sharedlibext)); }
  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:boolean):boolean;
  185. begin
  186. DoExec:=true;
  187. if not externlink then
  188. begin
  189. swapvectors;
  190. shell(command+' '+para);
  191. swapvectors;
  192. if (dosexitcode<>0) then
  193. begin
  194. Message(exec_w_error_while_linking);
  195. DoExec:=false;
  196. exit;
  197. end
  198. else
  199. if (dosError<>0) then
  200. begin
  201. Message(exec_w_cant_call_linker);
  202. ExternLink:=true;
  203. end;
  204. end;
  205. if externlink then
  206. begin
  207. if info then
  208. AsmRes.AddLinkCommand(Command,Para,ExeName)
  209. else
  210. AsmRes.AddLinkCommand(Command,Para,'');
  211. end;
  212. end;
  213. Function TLinker.WriteResponseFile : Boolean;
  214. Var
  215. LinkResponse : Text;
  216. i : longint;
  217. prtobj,s : string;
  218. begin
  219. { Open linkresponse and write header }
  220. assign(linkresponse,inputdir+LinkResName);
  221. rewrite(linkresponse);
  222. { set special options for some targets }
  223. prtobj:='prt0';
  224. case target_info.target of
  225. target_Win32 : prtobj:='';
  226. target_linux : begin
  227. if cs_profile in aktswitches then
  228. begin
  229. prtobj:='gprt0';
  230. AddSharedLibrary('gmon');
  231. AddSharedLibrary('c');
  232. end;
  233. end;
  234. end;
  235. writeln(linkresponse,target_link.inputstart);
  236. { add objectfiles, start with prt0 always }
  237. if prtobj<>'' then
  238. Writeln(linkresponse,FindObjectFile(prtobj));
  239. while not ObjectFiles.Empty do
  240. begin
  241. s:=ObjectFiles.Get;
  242. if s<>'' then
  243. Writeln(linkresponse,s);
  244. end;
  245. { Write sharedlibraries like -l<lib> }
  246. While not SharedLibFiles.Empty do
  247. begin
  248. S:=SharedLibFiles.Get;
  249. i:=Pos(target_os.sharedlibext,S);
  250. if i>0 then
  251. Delete(S,i,255);
  252. writeln(linkresponse,target_link.libprefix+s);
  253. end;
  254. writeln(linkresponse,target_link.inputend);
  255. { Write staticlibraries }
  256. if not StaticLibFiles.Empty then
  257. begin
  258. Writeln(LinkResponse,target_link.GroupStart);
  259. While not StaticLibFiles.Empty do
  260. begin
  261. S:=StaticLibFiles.Get;
  262. writeln(linkresponse,s)
  263. end;
  264. Writeln(LinkResponse,target_link.GroupEnd);
  265. end;
  266. { Close response }
  267. close(linkresponse);
  268. WriteResponseFile:=True;
  269. end;
  270. Function TLinker.MakeExecutable:boolean;
  271. var
  272. bindbin : string[80];
  273. bindfound : boolean;
  274. _stacksize,i,
  275. _heapsize : longint;
  276. s,s2 : string;
  277. dummy : file;
  278. success : boolean;
  279. begin
  280. {$ifdef linux}
  281. if LinkToC then
  282. begin
  283. AddObject('/usr/lib/crt0.o');
  284. AddObject('lprt');
  285. AddStaticLibrary('libc.a');
  286. AddStaticLibrary('libgcc.a');
  287. end;
  288. {$endif Linux}
  289. If not SharedLibFiles.Empty then
  290. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  291. if Strip then
  292. LinkOptions:=LinkOptions+target_link.stripopt;
  293. { Write used files and libraries }
  294. WriteResponseFile;
  295. { Call linker }
  296. if not externlink then
  297. Message1(exec_i_linking,ExeName);
  298. s:=target_link.linkcmd;
  299. Replace(s,'$EXE',exename);
  300. Replace(s,'$OPT',LinkOptions);
  301. Replace(s,'$RES',inputdir+LinkResName);
  302. success:=DoExec(FindLinker,s,true);
  303. {Bind}
  304. if target_info.target=target_os2 then
  305. begin
  306. {Calculate the stack and heap size in kilobytes, rounded upwards.}
  307. _stacksize:=(stacksize+1023) shr 10;
  308. {Minimum stacksize for EMX is 32K.}
  309. if _stacksize<32 then
  310. _stacksize:=32;
  311. str(_stacksize,s);
  312. _heapsize:=(heapsize+1023) shr 10;
  313. str(_heapsize,s2);
  314. bindbin:=FindExe('emxbind',bindfound);
  315. if (not bindfound) and (not externlink) then
  316. begin
  317. Message(exec_w_binder_not_found);
  318. externlink:=true;
  319. end;
  320. DoExec(bindbin,'-k'+s+' -o '+exename+'.exe '+exename+' -aim -s'+s2,false);
  321. end;
  322. {Remove ReponseFile}
  323. if (success) and (not externlink) then
  324. begin
  325. assign(dummy,LinkResName);
  326. {$I-}
  327. erase(dummy);
  328. {$I+}
  329. i:=ioresult;
  330. end;
  331. MakeExecutable:=success; { otherwise a recursive call to link method }
  332. end;
  333. Procedure TLinker.MakeStaticLibrary(const path:string);
  334. var
  335. arbin : string;
  336. arfound : boolean;
  337. i : word;
  338. f : file;
  339. Dir : searchrec;
  340. begin
  341. arbin:=FindExe('ar',arfound);
  342. if (not arfound) and (not externlink) then
  343. begin
  344. Message(exec_w_ar_not_found);
  345. externlink:=true;
  346. end;
  347. DoExec(arbin,'rs '+staticlibname+' '+FixPath(path)+'*'+target_info.objext,false);
  348. { Clean up }
  349. if (not writeasmfile) and (not externlink) then
  350. begin
  351. findfirst(FixPath(path)+'*'+target_info.objext,$20,Dir);
  352. while doserror=0 do
  353. begin
  354. assign(f,FixPath(path)+dir.name);
  355. {$I-}
  356. erase(f);
  357. {$I+}
  358. i:=ioresult;
  359. findnext(dir);
  360. end;
  361. {$I-}
  362. rmdir(path);
  363. {$I+}
  364. i:=ioresult;
  365. end;
  366. end;
  367. Procedure TLinker.MakeSharedLibrary;
  368. begin
  369. DoExec(FindLinker,' -shared -o '+sharedlibname+' link.res',false);
  370. end;
  371. end.
  372. {
  373. $Log$
  374. Revision 1.5 1998-05-04 20:19:54 peter
  375. * small fix for go32v2
  376. Revision 1.4 1998/05/04 17:54:25 peter
  377. + smartlinking works (only case jumptable left todo)
  378. * redesign of systems.pas to support assemblers and linkers
  379. + Unitname is now also in the PPU-file, increased version to 14
  380. Revision 1.3 1998/04/16 10:54:30 daniel
  381. * Fixed linking for OS/2.
  382. }