link.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. {OS/2 linker supports -l, but not for import libraries.
  206. For now, this fix should do it, as we don't support other libraries yet
  207. but we need to think of something better.}
  208. if target_info.target=target_OS2 then
  209. writeln(linkresponse,s)
  210. else
  211. Writeln (LinkResponse,'-l'+S);
  212. end;
  213. { Write End of response file }
  214. if target_info.target in [target_WIN32,target_linux] then
  215. Writeln (LinkResponse,')');
  216. { Close response }
  217. close(linkresponse);
  218. WriteResponseFile:=True;
  219. end;
  220. Function TLinker.link:boolean;
  221. var
  222. bindbin : string[80];
  223. bindfound : boolean;
  224. _stacksize,i,
  225. _heapsize : longint;
  226. s,s2 : string[10];
  227. dummy : file;
  228. success : boolean;
  229. begin
  230. {$ifdef linux}
  231. if LinkToC then
  232. begin
  233. AddObjectFile('/usr/lib/crt0.o');
  234. AddObjectFile(FindObjectFile('lprt'));
  235. AddLibraryFile('libc.a');
  236. AddLibraryFile('libgcc.a');
  237. end;
  238. {$endif Linux}
  239. { Create Linkoptions }
  240. case target_info.target of
  241. target_GO32V1:
  242. LinkOptions:=LinkOptions+' -oformat coff-go32';
  243. target_GO32V2:
  244. LinkOptions:=LinkOptions+' -oformat coff-go32-exe';
  245. target_linux: begin
  246. if cs_profile in aktswitches then
  247. begin
  248. AddLibraryFile('gmon');
  249. AddLibraryFile('c');
  250. end;
  251. end;
  252. end;
  253. {$ifdef linux}
  254. If not LibFiles.Empty then
  255. LinkOptions:='-dynamic-linker='+DynamicLinker+' '+LinkOptions;
  256. {$endif linux}
  257. if Strip then
  258. LinkOptions:=LinkOptions+' -s';
  259. { Write used files and libraries }
  260. WriteResponseFile;
  261. { Call linker }
  262. if not externlink then
  263. Message1(exec_i_linking,ExeName);
  264. {$ifdef linux}
  265. success:=DoExec(FindLinker,LinkOptions+' -o '+exename+' '+inputdir+LinkResName);
  266. {$else}
  267. if target_info.target=target_WIN32 then
  268. success:=DoExec(FindLinker,LinkOptions+' -o '+exename+' '+inputdir+LinkResName)
  269. else
  270. success:=DoExec(FindLinker,LinkOptions+' -o '+exename+' @'+inputdir+LinkResName);
  271. {$endif}
  272. {Bind}
  273. if target_info.target=target_os2 then
  274. begin
  275. {Calculate the stack and heap size in kilobytes, rounded upwards.}
  276. _stacksize:=(stacksize+1023) shr 10;
  277. {Minimum stacksize for EMX is 32K.}
  278. if _stacksize<32 then
  279. _stacksize:=32;
  280. str(_stacksize,s);
  281. _heapsize:=(heapsize+1023) shr 10;
  282. str(_heapsize,s2);
  283. bindbin:=FindExe('emxbind',bindfound);
  284. if (not bindfound) and (not externlink) then
  285. begin
  286. Message(exec_w_binder_not_found);
  287. externlink:=true;
  288. end;
  289. DoExec(bindbin,'-k'+s+' -o '+exename+'.exe '+exename+' -aim -s'+s2);
  290. end;
  291. if (success) and (not externlink) then
  292. begin
  293. assign(dummy,LinkResName);
  294. {$I-}
  295. erase(dummy);
  296. {$I+}
  297. i:=ioresult;
  298. end;
  299. link:=success; { otherwise a recursive call to link method }
  300. end;
  301. Procedure TLinker.Make_Library;
  302. var
  303. {$ifndef linux}
  304. arbin : string;
  305. arfound : boolean;
  306. {$endif}
  307. begin
  308. if cs_shared_lib in initswitches then
  309. begin
  310. WriteResponseFile;
  311. {$ifdef linux}
  312. DoExec(FindLinker,' -o '+libname+'.so -shared link.res');
  313. {$else}
  314. arbin:=FindExe('ar',arfound);
  315. if (not arfound) and (not externlink) then
  316. begin
  317. Message(exec_w_ar_not_found);
  318. externlink:=true;
  319. end;
  320. DoExec(arbin,'rs '+libname+'.a');
  321. {$endif}
  322. end;
  323. end;
  324. end.
  325. {
  326. $Log$
  327. Revision 1.3 1998-04-16 10:54:30 daniel
  328. * Fixed linking for OS/2.
  329. Revision 1.2 1998/03/30 09:50:49 michael
  330. + fix for library support.
  331. Revision 1.1.1.1 1998/03/25 11:18:13 root
  332. * Restored version
  333. Revision 1.31 1998/03/13 22:45:58 florian
  334. * small bug fixes applied
  335. Revision 1.30 1998/03/11 22:22:52 florian
  336. * Fixed circular unit uses, when the units are not in the current dir (from Peter)
  337. * -i shows correct info, not <lf> anymore (from Peter)
  338. * linking with shared libs works again (from Peter)
  339. Revision 1.29 1998/03/10 16:27:39 pierre
  340. * better line info in stabs debug
  341. * symtabletype and lexlevel separated into two fields of tsymtable
  342. + ifdef MAKELIB for direct library output, not complete
  343. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  344. working
  345. + ifdef TESTFUNCRET for setting func result in underfunction, not
  346. working
  347. Revision 1.28 1998/03/10 01:17:19 peter
  348. * all files have the same header
  349. * messages are fully implemented, EXTDEBUG uses Comment()
  350. + AG... files for the Assembler generation
  351. Revision 1.27 1998/03/05 22:43:47 florian
  352. * some win32 support stuff added
  353. Revision 1.26 1998/03/04 01:35:04 peter
  354. * messages for unit-handling and assembler/linker
  355. * the compiler compiles without -dGDB, but doesn't work yet
  356. + -vh for Hint
  357. Revision 1.25 1998/03/02 01:48:42 peter
  358. * renamed target_DOS to target_GO32V1
  359. + new verbose system, merged old errors and verbose units into one new
  360. verbose.pas, so errors.pas is obsolete
  361. Revision 1.24 1998/03/01 22:46:12 florian
  362. + some win95 linking stuff
  363. * a couple of bugs fixed:
  364. bug0055,bug0058,bug0059,bug0064,bug0072,bug0093,bug0095,bug0098
  365. Revision 1.23 1998/02/28 03:56:15 carl
  366. + replaced target_info.short_name by target_info.target (a bit faster)
  367. Revision 1.22 1998/02/26 11:57:09 daniel
  368. * New assembler optimizations commented out, because of bugs.
  369. * Use of dir-/name- and extstr.
  370. Revision 1.21 1998/02/25 20:26:41 michael
  371. + fixed linking for linux
  372. Revision 1.20 1998/02/24 14:20:53 peter
  373. + tstringcontainer.empty
  374. * ld -T option restored for linux
  375. * libraries are placed before the objectfiles in a .PPU file
  376. * removed 'uses link' from files.pas
  377. Revision 1.19 1998/02/23 02:54:23 carl
  378. * bugfix of recusrive call to link
  379. Revision 1.18 1998/02/22 23:03:18 peter
  380. * renamed msource->mainsource and name->unitname
  381. * optimized filename handling, filename is not seperate anymore with
  382. path+name+ext, this saves stackspace and a lot of fsplit()'s
  383. * recompiling of some units in libraries fixed
  384. * shared libraries are working again
  385. + $LINKLIB <lib> to support automatic linking to libraries
  386. + libraries are saved/read from the ppufile, also allows more libraries
  387. per ppufile
  388. Revision 1.17 1998/02/19 00:11:00 peter
  389. * fixed -g to work again
  390. * fixed some typos with the scriptobject
  391. Revision 1.16 1998/02/18 13:48:16 michael
  392. + Implemented an OS independent AsmRes object.
  393. Revision 1.15 1998/02/18 08:55:26 michael
  394. * Removed double declaration of LinkerOptions
  395. Revision 1.14 1998/02/17 21:20:50 peter
  396. + Script unit
  397. + __EXIT is called again to exit a program
  398. - target_info.link/assembler calls
  399. * linking works again for dos
  400. * optimized a few filehandling functions
  401. * fixed stabs generation for procedures
  402. Revision 1.13 1998/02/16 13:46:40 michael
  403. + Further integration of linker object:
  404. - all options pertaining to linking go directly to linker object
  405. - removed redundant variables/procedures, especially in OS_TARG...
  406. Revision 1.12 1998/02/16 12:51:31 michael
  407. + Implemented linker object
  408. Revision 1.11 1998/02/15 21:16:21 peter
  409. * all assembler outputs supported by assemblerobject
  410. * cleanup with assembleroutputs, better .ascii generation
  411. * help_constructor/destructor are now added to the externals
  412. - generation of asmresponse is not outputformat depended
  413. Revision 1.10 1998/02/14 01:45:21 peter
  414. * more fixes
  415. - pmode target is removed
  416. - search_as_ld is removed, this is done in the link.pas/assemble.pas
  417. + findexe() to search for an executable (linker,assembler,binder)
  418. Revision 1.9 1998/02/13 22:26:28 peter
  419. * fixed a few SigSegv's
  420. * INIT$$ was not written for linux!
  421. * assembling and linking works again for linux and dos
  422. + assembler object, only attasmi3 supported yet
  423. * restore pp.pas with AddPath etc.
  424. Revision 1.8 1998/02/13 10:35:09 daniel
  425. * Made Motorola version compilable.
  426. * Fixed optimizer
  427. >>>>>>> h:/cvs/compiler/link.pas
  428. Revision 1.7 1998/02/02 00:55:32 peter
  429. * defdatei -> deffile and some german comments to english
  430. * search() accepts : as seperater under linux
  431. * search for ppc.cfg doesn't open a file (and let it open)
  432. * reorganize the reading of parameters/file a bit
  433. * all the PPC_ environments are now for all platforms
  434. Revision 1.6 1998/02/01 15:02:11 florian
  435. * swapvectors around exec inserted
  436. Revision 1.5 1998/01/28 13:48:39 michael
  437. + Initial implementation for making libs from within FPC. Not tested, as compiler does not run
  438. Revision 1.4 1998/01/25 18:45:43 peter
  439. + Search for as and ld at startup
  440. + source_info works the same as target_info
  441. + externlink allows only external linking
  442. Revision 1.3 1998/01/24 00:36:07 florian
  443. + small fix to get it working with DOS (dynamiclinker isn't declared for dos)
  444. Revision 1.2 1998/01/23 22:19:17 michael
  445. + Implemented setting of dynamic linker name (linux only).
  446. Declared Make_library
  447. -Fd switch sets linker (linux only)
  448. * Reinstated -E option of Pierre
  449. Revision 1.1 1998/01/23 17:57:41 michael
  450. + Initial implementation.
  451. }