t_linux.pas 13 KB

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