t_linux.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. { place jump in codesegment }
  148. codesegment^.concat(new(pai_align,init_op(4,$90)));
  149. codesegment^.concat(new(pai_symbol,initname_global(hp2^.name^,0)));
  150. codesegment^.concat(new(paicpu,op_sym(A_JMP,S_NO,newasmsymbol(hp2^.sym^.mangledname))));
  151. codesegment^.concat(new(pai_symbol_end,initname(hp2^.name^)));
  152. end
  153. else
  154. Comment(V_Error,'Exporting of variables is not supported under linux');
  155. hp2:=pexported_item(hp2^.next);
  156. end;
  157. end;
  158. {*****************************************************************************
  159. TLINKERLINUX
  160. *****************************************************************************}
  161. Constructor TLinkerLinux.Init;
  162. begin
  163. Inherited Init;
  164. AddPathToList(LibrarySearchPath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  165. end;
  166. procedure TLinkerLinux.SetDefaultInfo;
  167. {
  168. This will also detect which libc version will be used
  169. }
  170. begin
  171. Glibc2:=false;
  172. Glibc21:=false;
  173. with Info do
  174. begin
  175. ExeCmd[1]:='ld $OPT $DYNLINK $STRIP -L. -o $EXE $RES';
  176. DllCmd[1]:='ld $OPT -shared -L. -o $EXE $RES';
  177. DllCmd[2]:='strip --strip-unneeded $EXE';
  178. { first try glibc2 }
  179. DynamicLinker:='/lib/ld-linux.so.2';
  180. if FileExists(DynamicLinker) then
  181. begin
  182. Glibc2:=true;
  183. { also glibc 2.1 / 2.1.1 / 2.1.2 ? }
  184. if FileExists('/lib/ld-2.1.so') or
  185. FileExists('/lib/ld-2.1.1.so') or
  186. FileExists('/lib/ld-2.1.2.so') then
  187. Glibc21:=true;
  188. end
  189. else
  190. DynamicLinker:='/lib/ld-linux.so.1';
  191. end;
  192. end;
  193. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  194. Var
  195. linkres : TLinkRes;
  196. i : longint;
  197. cprtobj,
  198. gprtobj,
  199. prtobj : string[80];
  200. HPath : TSearchPathString;
  201. s,s2 : string;
  202. found,
  203. linkdynamic,
  204. linklibc : boolean;
  205. begin
  206. WriteResponseFile:=False;
  207. { set special options for some targets }
  208. linkdynamic:=not(SharedLibFiles.empty);
  209. linklibc:=SharedLibFiles.Find('c');
  210. prtobj:='prt0';
  211. cprtobj:='cprt0';
  212. gprtobj:='gprt0';
  213. if glibc21 then
  214. begin
  215. cprtobj:='cprt21';
  216. gprtobj:='gprt21';
  217. end;
  218. if cs_profile in aktmoduleswitches then
  219. begin
  220. prtobj:=gprtobj;
  221. if not glibc2 then
  222. AddSharedLibrary('gmon');
  223. AddSharedLibrary('c');
  224. linklibc:=true;
  225. end
  226. else
  227. begin
  228. if linklibc then
  229. prtobj:=cprtobj;
  230. end;
  231. { Open link.res file }
  232. LinkRes.Init(Info.ResName);
  233. { Write path to search libraries }
  234. if assigned(current_module^.locallibrarysearchpath) then
  235. begin
  236. HPath:=current_module^.locallibrarysearchpath^;
  237. while HPath<>'' do
  238. begin
  239. s2:=GetPathFromList(HPath);
  240. LinkRes.Add('SEARCH_DIR('+s2+')');
  241. end;
  242. end;
  243. HPath:=LibrarySearchPath;
  244. while HPath<>'' do
  245. begin
  246. s2:=GetPathFromList(HPath);
  247. LinkRes.Add('SEARCH_DIR('+s2+')');
  248. end;
  249. LinkRes.Add('INPUT(');
  250. { add objectfiles, start with prt0 always }
  251. if prtobj<>'' then
  252. LinkRes.AddFileName(FindObjectFile(prtobj));
  253. { try to add crti and crtbegin, they are normally not required, but
  254. adding can sometimes be usefull }
  255. s:=search('crtbegin.o',librarysearchpath,found)+'crtbegin.o';
  256. if found then
  257. LinkRes.AddFileName(s);
  258. s:=search('crti.o',librarysearchpath,found)+'crti.o';
  259. if found then
  260. LinkRes.AddFileName(s);
  261. { main objectfiles }
  262. while not ObjectFiles.Empty do
  263. begin
  264. s:=ObjectFiles.Get;
  265. if s<>'' then
  266. LinkRes.AddFileName(s);
  267. end;
  268. { objects which must be at the end }
  269. s:=search('crtend.o',librarysearchpath,found)+'crtend.o';
  270. if found then
  271. LinkRes.AddFileName(s);
  272. s:=search('crtn.o',librarysearchpath,found)+'crtn.o';
  273. if found then
  274. LinkRes.AddFileName(s);
  275. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  276. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  277. While not SharedLibFiles.Empty do
  278. begin
  279. S:=SharedLibFiles.Get;
  280. if s<>'c' then
  281. begin
  282. i:=Pos(target_os.sharedlibext,S);
  283. if i>0 then
  284. Delete(S,i,255);
  285. LinkRes.Add('-l'+s);
  286. end
  287. else
  288. begin
  289. linklibc:=true;
  290. linkdynamic:=false; { libc will include the ld-linux for us }
  291. end;
  292. end;
  293. { be sure that libc is the last lib }
  294. if linklibc then
  295. LinkRes.Add('-lc');
  296. if linkdynamic and (Info.DynamicLinker<>'') then
  297. LinkRes.AddFileName(Info.DynamicLinker);
  298. LinkRes.Add(')');
  299. { Write staticlibraries }
  300. if not StaticLibFiles.Empty then
  301. begin
  302. LinkRes.Add('GROUP(');
  303. While not StaticLibFiles.Empty do
  304. begin
  305. S:=StaticLibFiles.Get;
  306. LinkRes.AddFileName(s)
  307. end;
  308. LinkRes.Add(')');
  309. end;
  310. { Write and Close response }
  311. linkres.writetodisk;
  312. linkres.done;
  313. WriteResponseFile:=True;
  314. end;
  315. function TLinkerLinux.MakeExecutable:boolean;
  316. var
  317. binstr,
  318. cmdstr : string;
  319. success : boolean;
  320. DynLinkStr : string[60];
  321. StripStr : string[40];
  322. begin
  323. if not(cs_link_extern in aktglobalswitches) then
  324. Message1(exec_i_linking,current_module^.exefilename^);
  325. { Create some replacements }
  326. StripStr:='';
  327. DynLinkStr:='';
  328. if (cs_link_strip in aktglobalswitches) then
  329. StripStr:='-s';
  330. If (Info.DynamicLinker<>'') and (not SharedLibFiles.Empty) then
  331. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  332. { Write used files and libraries }
  333. WriteResponseFile(false);
  334. { Call linker }
  335. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  336. Replace(cmdstr,'$EXE',current_module^.exefilename^);
  337. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  338. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  339. Replace(cmdstr,'$STRIP',StripStr);
  340. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  341. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  342. { Remove ReponseFile }
  343. if (success) and not(cs_link_extern in aktglobalswitches) then
  344. RemoveFile(current_module^.outpath^+Info.ResName);
  345. MakeExecutable:=success; { otherwise a recursive call to link method }
  346. end;
  347. Function TLinkerLinux.MakeSharedLibrary:boolean;
  348. var
  349. binstr,
  350. cmdstr : string;
  351. success : boolean;
  352. begin
  353. MakeSharedLibrary:=false;
  354. if not(cs_link_extern in aktglobalswitches) then
  355. Message1(exec_i_linking,current_module^.sharedlibfilename^);
  356. { Write used files and libraries }
  357. WriteResponseFile(true);
  358. { Call linker }
  359. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  360. Replace(cmdstr,'$EXE',current_module^.sharedlibfilename^);
  361. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  362. Replace(cmdstr,'$RES',current_module^.outpath^+Info.ResName);
  363. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  364. { Strip the library ? }
  365. if success and (cs_link_strip in aktglobalswitches) then
  366. begin
  367. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  368. Replace(cmdstr,'$EXE',current_module^.sharedlibfilename^);
  369. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  370. end;
  371. { Remove ReponseFile }
  372. if (success) and not(cs_link_extern in aktglobalswitches) then
  373. RemoveFile(current_module^.outpath^+Info.ResName);
  374. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  375. end;
  376. end.
  377. {
  378. $Log$
  379. Revision 1.2 1999-11-04 10:55:31 peter
  380. * TSearchPathString for the string type of the searchpaths, which is
  381. ansistring under FPC/Delphi
  382. Revision 1.1 1999/10/21 14:29:38 peter
  383. * redesigned linker object
  384. + library support for linux (only procedures can be exported)
  385. }