link.pas 15 KB

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