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