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. interface
  21. uses
  22. import,export,link;
  23. type
  24. pimportliblinux=^timportliblinux;
  25. timportliblinux=object(timportlib)
  26. procedure preparelib(const s:string);virtual;
  27. procedure importprocedure(const func,module:string;index:longint;const name:string);virtual;
  28. procedure importvariable(const varname,module:string;const name:string);virtual;
  29. procedure generatelib;virtual;
  30. end;
  31. pexportliblinux=^texportliblinux;
  32. texportliblinux=object(texportlib)
  33. procedure preparelib(const s : string);virtual;
  34. procedure exportprocedure(hp : pexported_item);virtual;
  35. procedure exportvar(hp : pexported_item);virtual;
  36. procedure generatelib;virtual;
  37. end;
  38. plinkerlinux=^tlinkerlinux;
  39. tlinkerlinux=object(tlinker)
  40. private
  41. Glibc2,
  42. Glibc21 : boolean;
  43. Function WriteResponseFile(isdll:boolean) : Boolean;
  44. public
  45. constructor Init;
  46. procedure SetDefaultInfo;virtual;
  47. function MakeExecutable:boolean;virtual;
  48. function MakeSharedLibrary:boolean;virtual;
  49. end;
  50. implementation
  51. uses
  52. verbose,strings,cobjects,systems,globtype,globals,
  53. symconst,script,
  54. files,aasm,cpuasm,cpubase,symtable;
  55. {*****************************************************************************
  56. TIMPORTLIBLINUX
  57. *****************************************************************************}
  58. procedure timportliblinux.preparelib(const s : string);
  59. begin
  60. end;
  61. procedure timportliblinux.importprocedure(const func,module : string;index : longint;const name : string);
  62. begin
  63. { insert sharedlibrary }
  64. current_module^.linkothersharedlibs.insert(SplitName(module),link_allways);
  65. { do nothing with the procedure, only set the mangledname }
  66. if name<>'' then
  67. aktprocsym^.definition^.setmangledname(name)
  68. else
  69. Message(parser_e_empty_import_name);
  70. end;
  71. procedure timportliblinux.importvariable(const varname,module:string;const name:string);
  72. begin
  73. { insert sharedlibrary }
  74. current_module^.linkothersharedlibs.insert(SplitName(module),link_allways);
  75. { reset the mangledname and turn off the dll_var option }
  76. aktvarsym^.setmangledname(name);
  77. {$ifdef INCLUDEOK}
  78. exclude(aktvarsym^.varoptions,vo_is_dll_var);
  79. {$else}
  80. aktvarsym^.varoptions:=aktvarsym^.varoptions-[vo_is_dll_var];
  81. {$endif}
  82. end;
  83. procedure timportliblinux.generatelib;
  84. begin
  85. end;
  86. {*****************************************************************************
  87. TEXPORTLIBLINUX
  88. *****************************************************************************}
  89. procedure texportliblinux.preparelib(const s:string);
  90. begin
  91. end;
  92. procedure texportliblinux.exportprocedure(hp : pexported_item);
  93. var
  94. hp2 : pexported_item;
  95. begin
  96. { first test the index value }
  97. if (hp^.options and eo_index)<>0 then
  98. begin
  99. Comment(V_Error,'can''t export with index under linux');
  100. exit;
  101. end;
  102. { use pascal name is none specified }
  103. if (hp^.options and eo_name)=0 then
  104. begin
  105. hp^.name:=stringdup(hp^.sym^.name);
  106. hp^.options:=hp^.options or eo_name;
  107. end;
  108. { now place in correct order }
  109. hp2:=pexported_item(current_module^._exports^.first);
  110. while assigned(hp2) and
  111. (hp^.name^>hp2^.name^) do
  112. hp2:=pexported_item(hp2^.next);
  113. { insert hp there !! }
  114. if assigned(hp2) and (hp2^.name^=hp^.name^) then
  115. begin
  116. { this is not allowed !! }
  117. Message1(parser_e_export_name_double,hp^.name^);
  118. exit;
  119. end;
  120. if hp2=pexported_item(current_module^._exports^.first) then
  121. current_module^._exports^.insert(hp)
  122. else if assigned(hp2) then
  123. begin
  124. hp^.next:=hp2;
  125. hp^.previous:=hp2^.previous;
  126. if assigned(hp2^.previous) then
  127. hp2^.previous^.next:=hp;
  128. hp2^.previous:=hp;
  129. end
  130. else
  131. current_module^._exports^.concat(hp);
  132. end;
  133. procedure texportliblinux.exportvar(hp : pexported_item);
  134. begin
  135. hp^.is_var:=true;
  136. exportprocedure(hp);
  137. end;
  138. procedure texportliblinux.generatelib;
  139. var
  140. hp2 : pexported_item;
  141. begin
  142. hp2:=pexported_item(current_module^._exports^.first);
  143. while assigned(hp2) do
  144. begin
  145. if not hp2^.is_var then
  146. begin
  147. {$ifdef i386}
  148. { place jump in codesegment }
  149. codesegment^.concat(new(pai_align,init_op(4,$90)));
  150. codesegment^.concat(new(pai_symbol,initname_global(hp2^.name^,0)));
  151. codesegment^.concat(new(paicpu,op_sym(A_JMP,S_NO,newasmsymbol(hp2^.sym^.mangledname))));
  152. codesegment^.concat(new(pai_symbol_end,initname(hp2^.name^)));
  153. {$endif i386}
  154. end
  155. else
  156. Comment(V_Error,'Exporting of variables is not supported under linux');
  157. hp2:=pexported_item(hp2^.next);
  158. end;
  159. end;
  160. {*****************************************************************************
  161. TLINKERLINUX
  162. *****************************************************************************}
  163. Constructor TLinkerLinux.Init;
  164. begin
  165. Inherited Init;
  166. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  167. end;
  168. procedure TLinkerLinux.SetDefaultInfo;
  169. {
  170. This will also detect which libc version will be used
  171. }
  172. begin
  173. Glibc2:=false;
  174. Glibc21:=false;
  175. with Info do
  176. begin
  177. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $RES';
  178. DllCmd[1]:='ld $OPT -shared -L. -o $EXE $RES';
  179. DllCmd[2]:='strip --strip-unneeded $EXE';
  180. { first try glibc2 }
  181. DynamicLinker:='/lib/ld-linux.so.2';
  182. if FileExists(DynamicLinker) then
  183. begin
  184. Glibc2:=true;
  185. { also glibc 2.1 / 2.1.1 / 2.1.2 ? }
  186. if FileExists('/lib/ld-2.1.so') or
  187. FileExists('/lib/ld-2.1.1.so') or
  188. FileExists('/lib/ld-2.1.2.so') then
  189. Glibc21:=true;
  190. end
  191. else
  192. DynamicLinker:='/lib/ld-linux.so.1';
  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, they are normally not required, but
  253. adding can sometimes be usefull }
  254. s:=librarysearchpath.FindFile('crtbegin.o',found)+'crtbegin.o';
  255. if found then
  256. LinkRes.AddFileName(s);
  257. s:=librarysearchpath.FindFile('crti.o',found)+'crti.o';
  258. if found then
  259. LinkRes.AddFileName(s);
  260. { main objectfiles }
  261. while not ObjectFiles.Empty do
  262. begin
  263. s:=ObjectFiles.Get;
  264. if s<>'' then
  265. LinkRes.AddFileName(s);
  266. end;
  267. { objects which must be at the end }
  268. s:=librarysearchpath.FindFile('crtend.o',found)+'crtend.o';
  269. if found then
  270. LinkRes.AddFileName(s);
  271. s:=librarysearchpath.FindFile('crtn.o',found)+'crtn.o';
  272. if found then
  273. LinkRes.AddFileName(s);
  274. LinkRes.Add(')');
  275. { Write staticlibraries }
  276. if not StaticLibFiles.Empty then
  277. begin
  278. LinkRes.Add('GROUP(');
  279. While not StaticLibFiles.Empty do
  280. begin
  281. S:=StaticLibFiles.Get;
  282. LinkRes.AddFileName(s)
  283. end;
  284. LinkRes.Add(')');
  285. end;
  286. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  287. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  288. if not SharedLibFiles.Empty then
  289. begin
  290. LinkRes.Add('INPUT(');
  291. While not SharedLibFiles.Empty do
  292. begin
  293. S:=SharedLibFiles.Get;
  294. if s<>'c' then
  295. begin
  296. i:=Pos(target_os.sharedlibext,S);
  297. if i>0 then
  298. Delete(S,i,255);
  299. LinkRes.Add('-l'+s);
  300. end
  301. else
  302. begin
  303. linklibc:=true;
  304. linkdynamic:=false; { libc will include the ld-linux for us }
  305. end;
  306. end;
  307. { be sure that libc is the last lib }
  308. if linklibc then
  309. LinkRes.Add('-lc');
  310. { when we have -static for the linker the we also need libgcc }
  311. if (cs_link_staticflag in aktglobalswitches) then
  312. LinkRes.Add('-lgcc');
  313. if linkdynamic and (Info.DynamicLinker<>'') then
  314. LinkRes.AddFileName(Info.DynamicLinker);
  315. LinkRes.Add(')');
  316. end;
  317. { Write and Close response }
  318. linkres.writetodisk;
  319. linkres.done;
  320. WriteResponseFile:=True;
  321. end;
  322. function TLinkerLinux.MakeExecutable:boolean;
  323. var
  324. binstr,
  325. cmdstr : string;
  326. success : boolean;
  327. DynLinkStr : string[60];
  328. StaticStr,
  329. StripStr : string[40];
  330. begin
  331. if not(cs_link_extern in aktglobalswitches) then
  332. Message1(exec_i_linking,current_module^.exefilename^);
  333. { Create some replacements }
  334. StaticStr:='';
  335. StripStr:='';
  336. DynLinkStr:='';
  337. if (cs_link_staticflag in aktglobalswitches) then
  338. StaticStr:='-static';
  339. if (cs_link_strip in aktglobalswitches) then
  340. StripStr:='-s';
  341. If (cs_profile in aktmoduleswitches) or
  342. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  343. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  344. { Write used files and libraries }
  345. WriteResponseFile(false);
  346. { Call linker }
  347. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  348. Replace(cmdstr,'$EXE',current_module^.exefilename^);
  349. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  350. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  351. Replace(cmdstr,'$STATIC',StaticStr);
  352. Replace(cmdstr,'$STRIP',StripStr);
  353. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  354. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  355. { Remove ReponseFile }
  356. if (success) and not(cs_link_extern in aktglobalswitches) then
  357. RemoveFile(outputexedir+Info.ResName);
  358. MakeExecutable:=success; { otherwise a recursive call to link method }
  359. end;
  360. Function TLinkerLinux.MakeSharedLibrary:boolean;
  361. var
  362. binstr,
  363. cmdstr : string;
  364. success : boolean;
  365. begin
  366. MakeSharedLibrary:=false;
  367. if not(cs_link_extern in aktglobalswitches) then
  368. Message1(exec_i_linking,current_module^.sharedlibfilename^);
  369. { Write used files and libraries }
  370. WriteResponseFile(true);
  371. { Call linker }
  372. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  373. Replace(cmdstr,'$EXE',current_module^.sharedlibfilename^);
  374. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  375. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  376. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  377. { Strip the library ? }
  378. if success and (cs_link_strip in aktglobalswitches) then
  379. begin
  380. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  381. Replace(cmdstr,'$EXE',current_module^.sharedlibfilename^);
  382. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  383. end;
  384. { Remove ReponseFile }
  385. if (success) and not(cs_link_extern in aktglobalswitches) then
  386. RemoveFile(outputexedir+Info.ResName);
  387. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  388. end;
  389. end.
  390. {
  391. $Log$
  392. Revision 1.9 2000-02-09 10:35:48 peter
  393. * -Xt option to link staticly against c libs
  394. Revision 1.8 2000/01/11 09:52:07 peter
  395. * fixed placing of .sl directories
  396. * use -b again for base-file selection
  397. * fixed group writing for linux with smartlinking
  398. Revision 1.7 2000/01/09 00:55:51 pierre
  399. * GROUP of smartlink units put before the C libraries
  400. to allow for smartlinking code that uses C code.
  401. Revision 1.6 2000/01/07 01:14:42 peter
  402. * updated copyright to 2000
  403. Revision 1.5 1999/11/16 23:39:04 peter
  404. * use outputexedir for link.res location
  405. Revision 1.4 1999/11/12 11:03:50 peter
  406. * searchpaths changed to stringqueue object
  407. Revision 1.3 1999/11/05 13:15:00 florian
  408. * some fixes to get the new cg compiling again
  409. Revision 1.2 1999/11/04 10:55:31 peter
  410. * TSearchPathString for the string type of the searchpaths, which is
  411. ansistring under FPC/Delphi
  412. Revision 1.1 1999/10/21 14:29:38 peter
  413. * redesigned linker object
  414. + library support for linux (only procedures can be exported)
  415. }