t_linux.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. {
  2. $Id$
  3. Copyright (c) 1999 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. AddPathToList(LibrarySearchPath,'/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 $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 : TSearchPathString;
  203. s,s2 : 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(Info.ResName);
  235. { Write path to search libraries }
  236. if assigned(current_module^.locallibrarysearchpath) then
  237. begin
  238. HPath:=current_module^.locallibrarysearchpath^;
  239. while HPath<>'' do
  240. begin
  241. s2:=GetPathFromList(HPath);
  242. LinkRes.Add('SEARCH_DIR('+s2+')');
  243. end;
  244. end;
  245. HPath:=LibrarySearchPath;
  246. while HPath<>'' do
  247. begin
  248. s2:=GetPathFromList(HPath);
  249. LinkRes.Add('SEARCH_DIR('+s2+')');
  250. end;
  251. LinkRes.Add('INPUT(');
  252. { add objectfiles, start with prt0 always }
  253. if prtobj<>'' then
  254. LinkRes.AddFileName(FindObjectFile(prtobj));
  255. { try to add crti and crtbegin, they are normally not required, but
  256. adding can sometimes be usefull }
  257. s:=search('crtbegin.o',librarysearchpath,found)+'crtbegin.o';
  258. if found then
  259. LinkRes.AddFileName(s);
  260. s:=search('crti.o',librarysearchpath,found)+'crti.o';
  261. if found then
  262. LinkRes.AddFileName(s);
  263. { main objectfiles }
  264. while not ObjectFiles.Empty do
  265. begin
  266. s:=ObjectFiles.Get;
  267. if s<>'' then
  268. LinkRes.AddFileName(s);
  269. end;
  270. { objects which must be at the end }
  271. s:=search('crtend.o',librarysearchpath,found)+'crtend.o';
  272. if found then
  273. LinkRes.AddFileName(s);
  274. s:=search('crtn.o',librarysearchpath,found)+'crtn.o';
  275. if found then
  276. LinkRes.AddFileName(s);
  277. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  278. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  279. While not SharedLibFiles.Empty do
  280. begin
  281. S:=SharedLibFiles.Get;
  282. if s<>'c' then
  283. begin
  284. i:=Pos(target_os.sharedlibext,S);
  285. if i>0 then
  286. Delete(S,i,255);
  287. LinkRes.Add('-l'+s);
  288. end
  289. else
  290. begin
  291. linklibc:=true;
  292. linkdynamic:=false; { libc will include the ld-linux for us }
  293. end;
  294. end;
  295. { be sure that libc is the last lib }
  296. if linklibc then
  297. LinkRes.Add('-lc');
  298. if linkdynamic and (Info.DynamicLinker<>'') then
  299. LinkRes.AddFileName(Info.DynamicLinker);
  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 and Close response }
  313. linkres.writetodisk;
  314. linkres.done;
  315. WriteResponseFile:=True;
  316. end;
  317. function TLinkerLinux.MakeExecutable:boolean;
  318. var
  319. binstr,
  320. cmdstr : string;
  321. success : boolean;
  322. DynLinkStr : string[60];
  323. StripStr : string[40];
  324. begin
  325. if not(cs_link_extern in aktglobalswitches) then
  326. Message1(exec_i_linking,current_module^.exefilename^);
  327. { Create some replacements }
  328. StripStr:='';
  329. DynLinkStr:='';
  330. if (cs_link_strip in aktglobalswitches) then
  331. StripStr:='-s';
  332. If (Info.DynamicLinker<>'') and (not SharedLibFiles.Empty) then
  333. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  334. { Write used files and libraries }
  335. WriteResponseFile(false);
  336. { Call linker }
  337. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  338. Replace(cmdstr,'$EXE',current_module^.exefilename^);
  339. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  340. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  341. Replace(cmdstr,'$STRIP',StripStr);
  342. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  343. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  344. { Remove ReponseFile }
  345. if (success) and not(cs_link_extern in aktglobalswitches) then
  346. RemoveFile(current_module^.outpath^+Info.ResName);
  347. MakeExecutable:=success; { otherwise a recursive call to link method }
  348. end;
  349. Function TLinkerLinux.MakeSharedLibrary:boolean;
  350. var
  351. binstr,
  352. cmdstr : string;
  353. success : boolean;
  354. begin
  355. MakeSharedLibrary:=false;
  356. if not(cs_link_extern in aktglobalswitches) then
  357. Message1(exec_i_linking,current_module^.sharedlibfilename^);
  358. { Write used files and libraries }
  359. WriteResponseFile(true);
  360. { Call linker }
  361. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  362. Replace(cmdstr,'$EXE',current_module^.sharedlibfilename^);
  363. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  364. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  365. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  366. { Strip the library ? }
  367. if success and (cs_link_strip in aktglobalswitches) then
  368. begin
  369. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  370. Replace(cmdstr,'$EXE',current_module^.sharedlibfilename^);
  371. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  372. end;
  373. { Remove ReponseFile }
  374. if (success) and not(cs_link_extern in aktglobalswitches) then
  375. RemoveFile(current_module^.outpath^+Info.ResName);
  376. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  377. end;
  378. end.
  379. {
  380. $Log$
  381. Revision 1.3 1999-11-05 13:15:00 florian
  382. * some fixes to get the new cg compiling again
  383. Revision 1.2 1999/11/04 10:55:31 peter
  384. * TSearchPathString for the string type of the searchpaths, which is
  385. ansistring under FPC/Delphi
  386. Revision 1.1 1999/10/21 14:29:38 peter
  387. * redesigned linker object
  388. + library support for linux (only procedures can be exported)
  389. }