t_linux.pas 13 KB

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