link.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 TLinker = Object
  23. { Internal variables. Don't access directly }
  24. {$ifdef linux}
  25. LinkToC : Boolean; { Should we link to the C libs? }
  26. GccLibraryPath : String; { Where is GCCLIB ? }
  27. DynamicLinker : String; { What Dynamic linker ? }
  28. {$endif}
  29. OFiles, LibFiles : TStringContainer;
  30. Strip : Boolean; { Strip symbols ? }
  31. MakeLib : Boolean; { If unit : Make library ?}
  32. ExeName, { FileName of the exe to be created }
  33. LibName : String; { FileName of the lib to be created }
  34. LinkResName : String[32]; { Name of response file }
  35. LinkOptions : String; { Additional options to the linker }
  36. LibrarySearchPath : String; { Where to look for libraries }
  37. { Methods }
  38. Constructor Init;
  39. Procedure SetFileName(const s:string);
  40. function FindObjectFile(s : string) : string;
  41. Procedure AddLibraryFile(S : String);
  42. Procedure AddObjectFile(S : String);
  43. Function FindLinker : String; { Find linker, sets Name }
  44. Function DoExec(const command,para:string):boolean;
  45. Function WriteResponseFile : Boolean;
  46. Function Link:boolean;
  47. Procedure Make_Library;
  48. end;
  49. PLinker=^TLinker;
  50. Var Linker : TLinker;
  51. Implementation
  52. uses
  53. Script,globals,systems,dos,verbose;
  54. Constructor TLinker.Init;
  55. begin
  56. OFiles.Init;
  57. LibFiles.Init;
  58. OFiles.Doubles:=False;
  59. LibFiles.Doubles:=False;
  60. Strip:=false;
  61. LinkOptions:='';
  62. LinkResName:='link.res';
  63. ExeName:='';
  64. LibName:='';
  65. {$ifdef linux}
  66. LinkToC:=False;
  67. LibrarySearchPath:='';
  68. DynamicLinker:='/lib/ld-linux.so.1';
  69. {$endif}
  70. end;
  71. Procedure TLinker.SetFileName(const s:string);
  72. var
  73. path:dirstr;
  74. name:namestr;
  75. ext:extstr;
  76. begin
  77. FSplit(s,path,name,ext);
  78. LibName:=Path+Name+target_info.DllExt;
  79. ExeName:=Path+Name+target_info.ExeExt;
  80. end;
  81. var
  82. LastLDBin : string;
  83. Function TLinker.FindLinker:string;
  84. var
  85. ldfound : boolean;
  86. begin
  87. if LastLDBin='' then
  88. begin
  89. if (target_info.target=target_WIN32) then
  90. { the win32 linker has another name to allow cross compiling between }
  91. { DOS and Win32, I think it should be possible to compile an ld }
  92. { with handles coff and pe, but I don't know how (FK) }
  93. LastLDBin:=FindExe('ldw',ldfound)
  94. else
  95. LastLDBin:=FindExe('ld',ldfound);
  96. if (not ldfound) and (not externlink) then
  97. begin
  98. Message1(exec_w_linker_not_found,LastLDBin);
  99. externlink:=true;
  100. end;
  101. if ldfound then
  102. Message1(exec_u_using_linker,LastLDBin);
  103. end;
  104. FindLinker:=LastLDBin;
  105. end;
  106. { searches an object file }
  107. function TLinker.FindObjectFile(s:string) : string;
  108. var
  109. found : boolean;
  110. begin
  111. if pos('.',s)=0 then
  112. s:=s+target_info.objext;
  113. s:=FixFileName(s);
  114. if FileExists(s) then
  115. begin
  116. Findobjectfile:=s;
  117. exit;
  118. end;
  119. findobjectfile:=search(s,'.;'+unitsearchpath+';'+exepath,found)+s;
  120. if (not externasm) and (not found) then
  121. Message1(exec_e_objfile_not_found,s);
  122. end;
  123. Procedure TLInker.AddObjectFile (S : String);
  124. begin
  125. if pos('.',s)=0 then
  126. s:=s+target_info.objext;
  127. s:=FixFileName(s);
  128. OFiles.Insert (S);
  129. end;
  130. Procedure TLInker.AddLibraryFile(S:String);
  131. begin
  132. if pos('.',s)=0 then
  133. s:=s+target_info.dllext;
  134. LibFiles.Insert (S);
  135. end;
  136. Function TLinker.DoExec(const command,para:string):boolean;
  137. begin
  138. DoExec:=true;
  139. if not externlink then
  140. begin
  141. swapvectors;
  142. exec(command,para);
  143. swapvectors;
  144. if (dosexitcode<>0) then
  145. begin
  146. Message(exec_w_error_while_linking);
  147. DoExec:=false;
  148. exit;
  149. end
  150. else
  151. if (dosError<>0) then
  152. begin
  153. Message(exec_w_cant_call_linker);
  154. ExternLink:=true;
  155. end;
  156. end;
  157. if externlink then
  158. AsmRes.AddLinkCommand (Command,Para,ExeName);
  159. end;
  160. Function TLinker.WriteResponseFile : Boolean;
  161. Var
  162. LinkResponse : Text;
  163. i : longint;
  164. prtobj,s : string;
  165. begin
  166. { Open linkresponse and write header }
  167. assign(linkresponse,inputdir+LinkResName);
  168. rewrite(linkresponse);
  169. { Write Header and set runtime object (prt0) }
  170. case target_info.target of
  171. target_WIN32 : begin
  172. prtobj:='';
  173. writeln(linkresponse,'INPUT (');
  174. end;
  175. target_linux : begin
  176. if cs_profile in aktswitches then
  177. prtobj:='gprt0'
  178. else
  179. prtobj:='prt0';
  180. {$ifdef Linux}
  181. if LinkToC then
  182. writeln(linkresponse,'SEARCH_DIR ('+GCCLibraryPath +')');
  183. {$endif}
  184. writeln(linkresponse,'INPUT (');
  185. end;
  186. else
  187. prtobj:='prt0';
  188. end;
  189. { add objectfiles, start with prt0 always }
  190. if prtobj<>'' then
  191. Writeln(linkresponse,FindObjectFile(prtobj));
  192. while not OFiles.Empty do
  193. begin
  194. s:=Findobjectfile(OFiles.Get);
  195. if s<>'' then
  196. Writeln(linkresponse,s);
  197. end;
  198. { Write libraries like -l<lib> }
  199. While not LibFiles.Empty do
  200. begin
  201. S:=LibFiles.Get;
  202. i:=Pos(target_info.dllext,S);
  203. if i>0 then
  204. Delete(S,i,255);
  205. Writeln (LinkResponse,'-l'+S);
  206. end;
  207. { Write End of response file }
  208. if target_info.target in [target_WIN32,target_linux] then
  209. Writeln (LinkResponse,')');
  210. { Close response }
  211. close(linkresponse);
  212. WriteResponseFile:=True;
  213. end;
  214. Function TLinker.link:boolean;
  215. var
  216. bindbin : string[80];
  217. bindfound : boolean;
  218. _stacksize,i,
  219. _heapsize : longint;
  220. s,s2 : string[10];
  221. dummy : file;
  222. success : boolean;
  223. begin
  224. {$ifdef linux}
  225. if LinkToC then
  226. begin
  227. AddObjectFile('/usr/lib/crt0.o');
  228. AddObjectFile(FindObjectFile('lprt'));
  229. AddLibraryFile('libc.a');
  230. AddLibraryFile('libgcc.a');
  231. end;
  232. {$endif Linux}
  233. { Create Linkoptions }
  234. case target_info.target of
  235. target_GO32V1:
  236. LinkOptions:=LinkOptions+' -oformat coff-go32';
  237. target_GO32V2:
  238. LinkOptions:=LinkOptions+' -oformat coff-go32-exe';
  239. target_linux: begin
  240. if cs_profile in aktswitches then
  241. begin
  242. AddLibraryFile('gmon');
  243. AddLibraryFile('c');
  244. end;
  245. end;
  246. end;
  247. {$ifdef linux}
  248. If not LibFiles.Empty then
  249. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  250. {$endif linux}
  251. if Strip then
  252. LinkOptions:=LinkOptions+' -s';
  253. { Write used files and libraries }
  254. WriteResponseFile;
  255. { Call linker }
  256. if not externlink then
  257. Message1(exec_i_linking,ExeName);
  258. {$ifdef linux}
  259. success:=DoExec(FindLinker,LinkOptions+' -o '+exename+' '+inputdir+LinkResName);
  260. {$else}
  261. if target_info.target=target_WIN32 then
  262. success:=DoExec(FindLinker,LinkOptions+' -o '+exename+' '+inputdir+LinkResName)
  263. else
  264. success:=DoExec(FindLinker,LinkOptions+' -o '+exename+' @'+inputdir+LinkResName);
  265. {$endif}
  266. {Bind}
  267. if target_info.target=target_os2 then
  268. begin
  269. {Calculate the stack and heap size in kilobytes, rounded upwards.}
  270. _stacksize:=(stacksize+1023) shr 10;
  271. {Minimum stacksize for EMX is 32K.}
  272. if _stacksize<32 then
  273. _stacksize:=32;
  274. str(_stacksize,s);
  275. _heapsize:=(heapsize+1023) shr 10;
  276. str(_heapsize,s2);
  277. bindbin:=FindExe('emxbind',bindfound);
  278. if (not bindfound) and (not externlink) then
  279. begin
  280. Message(exec_w_binder_not_found);
  281. externlink:=true;
  282. end;
  283. DoExec(bindbin,'-k'+s+' -o '+exename+'.exe '+exename+' -aim -s'+s2);
  284. end;
  285. if (success) and (not externlink) then
  286. begin
  287. assign(dummy,LinkResName);
  288. {$I-}
  289. erase(dummy);
  290. {$I+}
  291. i:=ioresult;
  292. end;
  293. link:=success; { otherwise a recursive call to link method }
  294. end;
  295. Procedure TLinker.Make_Library;
  296. var
  297. {$ifndef linux}
  298. arbin : string;
  299. arfound : boolean;
  300. {$endif}
  301. begin
  302. if cs_shared_lib in initswitches then
  303. begin
  304. WriteResponseFile;
  305. {$ifdef linux}
  306. DoExec(FindLinker,' -o '+libname+'.so -shared link.res');
  307. {$else}
  308. arbin:=FindExe('ar',arfound);
  309. if (not arfound) and (not externlink) then
  310. begin
  311. Message(exec_w_ar_not_found);
  312. externlink:=true;
  313. end;
  314. DoExec(arbin,'rs '+libname+'.a');
  315. {$endif}
  316. end;
  317. end;
  318. end.
  319. {
  320. $Log$
  321. Revision 1.2 1998-03-30 09:50:49 michael
  322. + fix for library support.
  323. Revision 1.1.1.1 1998/03/25 11:18:13 root
  324. * Restored version
  325. Revision 1.31 1998/03/13 22:45:58 florian
  326. * small bug fixes applied
  327. Revision 1.30 1998/03/11 22:22:52 florian
  328. * Fixed circular unit uses, when the units are not in the current dir (from Peter)
  329. * -i shows correct info, not <lf> anymore (from Peter)
  330. * linking with shared libs works again (from Peter)
  331. Revision 1.29 1998/03/10 16:27:39 pierre
  332. * better line info in stabs debug
  333. * symtabletype and lexlevel separated into two fields of tsymtable
  334. + ifdef MAKELIB for direct library output, not complete
  335. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  336. working
  337. + ifdef TESTFUNCRET for setting func result in underfunction, not
  338. working
  339. Revision 1.28 1998/03/10 01:17:19 peter
  340. * all files have the same header
  341. * messages are fully implemented, EXTDEBUG uses Comment()
  342. + AG... files for the Assembler generation
  343. Revision 1.27 1998/03/05 22:43:47 florian
  344. * some win32 support stuff added
  345. Revision 1.26 1998/03/04 01:35:04 peter
  346. * messages for unit-handling and assembler/linker
  347. * the compiler compiles without -dGDB, but doesn't work yet
  348. + -vh for Hint
  349. Revision 1.25 1998/03/02 01:48:42 peter
  350. * renamed target_DOS to target_GO32V1
  351. + new verbose system, merged old errors and verbose units into one new
  352. verbose.pas, so errors.pas is obsolete
  353. Revision 1.24 1998/03/01 22:46:12 florian
  354. + some win95 linking stuff
  355. * a couple of bugs fixed:
  356. bug0055,bug0058,bug0059,bug0064,bug0072,bug0093,bug0095,bug0098
  357. Revision 1.23 1998/02/28 03:56:15 carl
  358. + replaced target_info.short_name by target_info.target (a bit faster)
  359. Revision 1.22 1998/02/26 11:57:09 daniel
  360. * New assembler optimizations commented out, because of bugs.
  361. * Use of dir-/name- and extstr.
  362. Revision 1.21 1998/02/25 20:26:41 michael
  363. + fixed linking for linux
  364. Revision 1.20 1998/02/24 14:20:53 peter
  365. + tstringcontainer.empty
  366. * ld -T option restored for linux
  367. * libraries are placed before the objectfiles in a .PPU file
  368. * removed 'uses link' from files.pas
  369. Revision 1.19 1998/02/23 02:54:23 carl
  370. * bugfix of recusrive call to link
  371. Revision 1.18 1998/02/22 23:03:18 peter
  372. * renamed msource->mainsource and name->unitname
  373. * optimized filename handling, filename is not seperate anymore with
  374. path+name+ext, this saves stackspace and a lot of fsplit()'s
  375. * recompiling of some units in libraries fixed
  376. * shared libraries are working again
  377. + $LINKLIB <lib> to support automatic linking to libraries
  378. + libraries are saved/read from the ppufile, also allows more libraries
  379. per ppufile
  380. Revision 1.17 1998/02/19 00:11:00 peter
  381. * fixed -g to work again
  382. * fixed some typos with the scriptobject
  383. Revision 1.16 1998/02/18 13:48:16 michael
  384. + Implemented an OS independent AsmRes object.
  385. Revision 1.15 1998/02/18 08:55:26 michael
  386. * Removed double declaration of LinkerOptions
  387. Revision 1.14 1998/02/17 21:20:50 peter
  388. + Script unit
  389. + __EXIT is called again to exit a program
  390. - target_info.link/assembler calls
  391. * linking works again for dos
  392. * optimized a few filehandling functions
  393. * fixed stabs generation for procedures
  394. Revision 1.13 1998/02/16 13:46:40 michael
  395. + Further integration of linker object:
  396. - all options pertaining to linking go directly to linker object
  397. - removed redundant variables/procedures, especially in OS_TARG...
  398. Revision 1.12 1998/02/16 12:51:31 michael
  399. + Implemented linker object
  400. Revision 1.11 1998/02/15 21:16:21 peter
  401. * all assembler outputs supported by assemblerobject
  402. * cleanup with assembleroutputs, better .ascii generation
  403. * help_constructor/destructor are now added to the externals
  404. - generation of asmresponse is not outputformat depended
  405. Revision 1.10 1998/02/14 01:45:21 peter
  406. * more fixes
  407. - pmode target is removed
  408. - search_as_ld is removed, this is done in the link.pas/assemble.pas
  409. + findexe() to search for an executable (linker,assembler,binder)
  410. Revision 1.9 1998/02/13 22:26:28 peter
  411. * fixed a few SigSegv's
  412. * INIT$$ was not written for linux!
  413. * assembling and linking works again for linux and dos
  414. + assembler object, only attasmi3 supported yet
  415. * restore pp.pas with AddPath etc.
  416. Revision 1.8 1998/02/13 10:35:09 daniel
  417. * Made Motorola version compilable.
  418. * Fixed optimizer
  419. >>>>>>> h:/cvs/compiler/link.pas
  420. Revision 1.7 1998/02/02 00:55:32 peter
  421. * defdatei -> deffile and some german comments to english
  422. * search() accepts : as seperater under linux
  423. * search for ppc.cfg doesn't open a file (and let it open)
  424. * reorganize the reading of parameters/file a bit
  425. * all the PPC_ environments are now for all platforms
  426. Revision 1.6 1998/02/01 15:02:11 florian
  427. * swapvectors around exec inserted
  428. Revision 1.5 1998/01/28 13:48:39 michael
  429. + Initial implementation for making libs from within FPC. Not tested, as compiler does not run
  430. Revision 1.4 1998/01/25 18:45:43 peter
  431. + Search for as and ld at startup
  432. + source_info works the same as target_info
  433. + externlink allows only external linking
  434. Revision 1.3 1998/01/24 00:36:07 florian
  435. + small fix to get it working with DOS (dynamiclinker isn't declared for dos)
  436. Revision 1.2 1998/01/23 22:19:17 michael
  437. + Implemented setting of dynamic linker name (linux only).
  438. Declared Make_library
  439. -Fd switch sets linker (linux only)
  440. * Reinstated -E option of Pierre
  441. Revision 1.1 1998/01/23 17:57:41 michael
  442. + Initial implementation.
  443. }