t_linux.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit implements support import,export,link routines
  5. for the (i386) Linux target
  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 t_linux;
  20. {$i defines.inc}
  21. interface
  22. uses
  23. import,export,link;
  24. type
  25. timportliblinux=class(timportlib)
  26. procedure preparelib(const s:string);override;
  27. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  28. procedure importvariable(const varname,module:string;const name:string);override;
  29. procedure generatelib;override;
  30. end;
  31. texportliblinux=class(texportlib)
  32. procedure preparelib(const s : string);override;
  33. procedure exportprocedure(hp : texported_item);override;
  34. procedure exportvar(hp : texported_item);override;
  35. procedure generatelib;override;
  36. end;
  37. tlinkerlinux=class(tlinker)
  38. private
  39. Glibc2,
  40. Glibc21 : boolean;
  41. Function WriteResponseFile(isdll:boolean) : Boolean;
  42. public
  43. constructor Create;
  44. procedure SetDefaultInfo;override;
  45. function MakeExecutable:boolean;override;
  46. function MakeSharedLibrary:boolean;override;
  47. end;
  48. implementation
  49. uses
  50. cutils,cclasses,
  51. verbose,systems,globtype,globals,
  52. symconst,script,
  53. fmodule,aasm,cpuasm,cpubase,symsym;
  54. {*****************************************************************************
  55. TIMPORTLIBLINUX
  56. *****************************************************************************}
  57. procedure timportliblinux.preparelib(const s : string);
  58. begin
  59. end;
  60. procedure timportliblinux.importprocedure(const func,module : string;index : longint;const name : string);
  61. begin
  62. { insert sharedlibrary }
  63. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  64. { do nothing with the procedure, only set the mangledname }
  65. if name<>'' then
  66. aktprocsym^.definition^.setmangledname(name)
  67. else
  68. message(parser_e_empty_import_name);
  69. end;
  70. procedure timportliblinux.importvariable(const varname,module:string;const name:string);
  71. begin
  72. { insert sharedlibrary }
  73. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  74. { reset the mangledname and turn off the dll_var option }
  75. aktvarsym^.setmangledname(name);
  76. exclude(aktvarsym^.varoptions,vo_is_dll_var);
  77. end;
  78. procedure timportliblinux.generatelib;
  79. begin
  80. end;
  81. {*****************************************************************************
  82. TEXPORTLIBLINUX
  83. *****************************************************************************}
  84. procedure texportliblinux.preparelib(const s:string);
  85. begin
  86. end;
  87. procedure texportliblinux.exportprocedure(hp : texported_item);
  88. var
  89. hp2 : texported_item;
  90. begin
  91. { first test the index value }
  92. if (hp.options and eo_index)<>0 then
  93. begin
  94. Message1(parser_e_no_export_with_index_for_target,'linux');
  95. exit;
  96. end;
  97. { use pascal name is none specified }
  98. if (hp.options and eo_name)=0 then
  99. begin
  100. hp.name:=stringdup(hp.sym^.name);
  101. hp.options:=hp.options or eo_name;
  102. end;
  103. { now place in correct order }
  104. hp2:=texported_item(current_module._exports.first);
  105. while assigned(hp2) and
  106. (hp.name^>hp2.name^) do
  107. hp2:=texported_item(hp2.next);
  108. { insert hp there !! }
  109. if assigned(hp2) and (hp2.name^=hp.name^) then
  110. begin
  111. { this is not allowed !! }
  112. Message1(parser_e_export_name_double,hp.name^);
  113. exit;
  114. end;
  115. if hp2=texported_item(current_module._exports.first) then
  116. current_module._exports.concat(hp)
  117. else if assigned(hp2) then
  118. begin
  119. hp.next:=hp2;
  120. hp.previous:=hp2.previous;
  121. if assigned(hp2.previous) then
  122. hp2.previous.next:=hp;
  123. hp2.previous:=hp;
  124. end
  125. else
  126. current_module._exports.concat(hp);
  127. end;
  128. procedure texportliblinux.exportvar(hp : texported_item);
  129. begin
  130. hp.is_var:=true;
  131. exportprocedure(hp);
  132. end;
  133. procedure texportliblinux.generatelib;
  134. var
  135. hp2 : texported_item;
  136. begin
  137. hp2:=texported_item(current_module._exports.first);
  138. while assigned(hp2) do
  139. begin
  140. if not hp2.is_var then
  141. begin
  142. {$ifdef i386}
  143. { place jump in codesegment }
  144. codesegment.concat(Tai_align.Create_op(4,$90));
  145. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  146. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,newasmsymbol(hp2.sym^.mangledname)));
  147. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  148. {$endif i386}
  149. end
  150. else
  151. Message1(parser_e_no_export_of_variables_for_target,'linux');
  152. hp2:=texported_item(hp2.next);
  153. end;
  154. end;
  155. {*****************************************************************************
  156. TLINKERLINUX
  157. *****************************************************************************}
  158. Constructor TLinkerLinux.Create;
  159. begin
  160. Inherited Create;
  161. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  162. end;
  163. procedure TLinkerLinux.SetDefaultInfo;
  164. {
  165. This will also detect which libc version will be used
  166. }
  167. begin
  168. Glibc2:=false;
  169. Glibc21:=false;
  170. with Info do
  171. begin
  172. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $RES';
  173. DllCmd[1]:='ld $OPT -shared -L. -o $EXE $RES';
  174. DllCmd[2]:='strip --strip-unneeded $EXE';
  175. { first try glibc2 }
  176. DynamicLinker:='/lib/ld-linux.so.2';
  177. if FileExists(DynamicLinker) then
  178. begin
  179. Glibc2:=true;
  180. { Check for 2.0 files, else use the glibc 2.1 stub }
  181. if FileExists('/lib/ld-2.0.*') then
  182. Glibc21:=false
  183. else
  184. Glibc21:=true;
  185. end
  186. else
  187. DynamicLinker:='/lib/ld-linux.so.1';
  188. {$ifdef BSD}
  189. DynamicLinker:='';
  190. {$endif}
  191. end;
  192. end;
  193. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  194. Var
  195. linkres : TLinkRes;
  196. i : longint;
  197. cprtobj,
  198. gprtobj,
  199. prtobj : string[80];
  200. HPath : TStringListItem;
  201. s : string;
  202. found,
  203. linkdynamic,
  204. linklibc : boolean;
  205. begin
  206. WriteResponseFile:=False;
  207. { set special options for some targets }
  208. linkdynamic:=not(SharedLibFiles.empty);
  209. linklibc:=(SharedLibFiles.Find('c')<>nil);
  210. prtobj:='prt0';
  211. cprtobj:='cprt0';
  212. gprtobj:='gprt0';
  213. if glibc21 then
  214. begin
  215. cprtobj:='cprt21';
  216. gprtobj:='gprt21';
  217. end;
  218. if cs_profile in aktmoduleswitches then
  219. begin
  220. prtobj:=gprtobj;
  221. if not glibc2 then
  222. AddSharedLibrary('gmon');
  223. AddSharedLibrary('c');
  224. linklibc:=true;
  225. end
  226. else
  227. begin
  228. if linklibc then
  229. prtobj:=cprtobj;
  230. end;
  231. { Open link.res file }
  232. LinkRes.Init(outputexedir+Info.ResName);
  233. { Write path to search libraries }
  234. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  235. while assigned(HPath) do
  236. begin
  237. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  238. HPath:=TStringListItem(HPath.Next);
  239. end;
  240. HPath:=TStringListItem(LibrarySearchPath.First);
  241. while assigned(HPath) do
  242. begin
  243. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  244. HPath:=TStringListItem(HPath.Next);
  245. end;
  246. LinkRes.Add('INPUT(');
  247. { add objectfiles, start with prt0 always }
  248. if prtobj<>'' then
  249. LinkRes.AddFileName(FindObjectFile(prtobj,''));
  250. { try to add crti and crtbegin if linking to C }
  251. if linklibc then
  252. begin
  253. s:=librarysearchpath.FindFile('crtbegin.o',found)+'crtbegin.o';
  254. if found then
  255. LinkRes.AddFileName(s);
  256. s:=librarysearchpath.FindFile('crti.o',found)+'crti.o';
  257. if found then
  258. LinkRes.AddFileName(s);
  259. end;
  260. { main objectfiles }
  261. while not ObjectFiles.Empty do
  262. begin
  263. s:=ObjectFiles.GetFirst;
  264. if s<>'' then
  265. LinkRes.AddFileName(s);
  266. end;
  267. { objects which must be at the end }
  268. if linklibc then
  269. begin
  270. s:=librarysearchpath.FindFile('crtend.o',found)+'crtend.o';
  271. if found then
  272. LinkRes.AddFileName(s);
  273. s:=librarysearchpath.FindFile('crtn.o',found)+'crtn.o';
  274. if found then
  275. LinkRes.AddFileName(s);
  276. end;
  277. LinkRes.Add(')');
  278. { Write staticlibraries }
  279. if not StaticLibFiles.Empty then
  280. begin
  281. LinkRes.Add('GROUP(');
  282. While not StaticLibFiles.Empty do
  283. begin
  284. S:=StaticLibFiles.GetFirst;
  285. LinkRes.AddFileName(s)
  286. end;
  287. LinkRes.Add(')');
  288. end;
  289. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  290. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  291. if not SharedLibFiles.Empty then
  292. begin
  293. LinkRes.Add('INPUT(');
  294. While not SharedLibFiles.Empty do
  295. begin
  296. S:=SharedLibFiles.GetFirst;
  297. if s<>'c' then
  298. begin
  299. i:=Pos(target_os.sharedlibext,S);
  300. if i>0 then
  301. Delete(S,i,255);
  302. LinkRes.Add('-l'+s);
  303. end
  304. else
  305. begin
  306. linklibc:=true;
  307. linkdynamic:=false; { libc will include the ld-linux for us }
  308. end;
  309. end;
  310. { be sure that libc is the last lib }
  311. if linklibc then
  312. LinkRes.Add('-lc');
  313. { when we have -static for the linker the we also need libgcc }
  314. if (cs_link_staticflag in aktglobalswitches) then
  315. LinkRes.Add('-lgcc');
  316. if linkdynamic and (Info.DynamicLinker<>'') then
  317. LinkRes.AddFileName(Info.DynamicLinker);
  318. LinkRes.Add(')');
  319. end;
  320. { Write and Close response }
  321. linkres.writetodisk;
  322. linkres.done;
  323. WriteResponseFile:=True;
  324. end;
  325. function TLinkerLinux.MakeExecutable:boolean;
  326. var
  327. binstr,
  328. cmdstr : string;
  329. success : boolean;
  330. DynLinkStr : string[60];
  331. StaticStr,
  332. StripStr : string[40];
  333. begin
  334. if not(cs_link_extern in aktglobalswitches) then
  335. Message1(exec_i_linking,current_module.exefilename^);
  336. { Create some replacements }
  337. StaticStr:='';
  338. StripStr:='';
  339. DynLinkStr:='';
  340. if (cs_link_staticflag in aktglobalswitches) then
  341. StaticStr:='-static';
  342. if (cs_link_strip in aktglobalswitches) then
  343. StripStr:='-s';
  344. If (cs_profile in aktmoduleswitches) or
  345. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  346. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  347. { Write used files and libraries }
  348. WriteResponseFile(false);
  349. { Call linker }
  350. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  351. Replace(cmdstr,'$EXE',current_module.exefilename^);
  352. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  353. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  354. Replace(cmdstr,'$STATIC',StaticStr);
  355. Replace(cmdstr,'$STRIP',StripStr);
  356. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  357. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  358. { Remove ReponseFile }
  359. if (success) and not(cs_link_extern in aktglobalswitches) then
  360. RemoveFile(outputexedir+Info.ResName);
  361. MakeExecutable:=success; { otherwise a recursive call to link method }
  362. end;
  363. Function TLinkerLinux.MakeSharedLibrary:boolean;
  364. var
  365. binstr,
  366. cmdstr : string;
  367. success : boolean;
  368. begin
  369. MakeSharedLibrary:=false;
  370. if not(cs_link_extern in aktglobalswitches) then
  371. Message1(exec_i_linking,current_module.sharedlibfilename^);
  372. { Write used files and libraries }
  373. WriteResponseFile(true);
  374. { Call linker }
  375. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  376. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  377. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  378. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  379. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  380. { Strip the library ? }
  381. if success and (cs_link_strip in aktglobalswitches) then
  382. begin
  383. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  384. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  385. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  386. end;
  387. { Remove ReponseFile }
  388. if (success) and not(cs_link_extern in aktglobalswitches) then
  389. RemoveFile(outputexedir+Info.ResName);
  390. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  391. end;
  392. end.
  393. {
  394. $Log$
  395. Revision 1.9 2000-12-25 00:07:30 peter
  396. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  397. tlinkedlist objects)
  398. Revision 1.8 2000/10/31 22:02:54 peter
  399. * symtable splitted, no real code changes
  400. Revision 1.7 2000/09/24 21:33:47 peter
  401. * message updates merges
  402. Revision 1.6 2000/09/24 15:06:31 peter
  403. * use defines.inc
  404. Revision 1.5 2000/09/10 20:26:55 peter
  405. * bsd patches from marco
  406. Revision 1.4 2000/08/27 16:11:54 peter
  407. * moved some util functions from globals,cobjects to cutils
  408. * splitted files into finput,fmodule
  409. Revision 1.3 2000/07/13 12:08:28 michael
  410. + patched to 1.1.0 with former 1.09patch from peter
  411. Revision 1.2 2000/07/13 11:32:50 michael
  412. + removed logs
  413. }