t_nwm.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit implements support import,export,link routines
  5. for the (i386) Netware 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. First Implementation 10 Sept 2000 Armin Diehl
  18. Currently generating NetWare-NLM's only work under Linux. This is
  19. because nlmconf from binutils does not work with i.e. win32 coff
  20. object files. It works fine with ELF-Objects.
  21. The following compiler-swiches are supported for NetWare:
  22. $DESCRIPTION : NLM-Description, will be displayed at load-time
  23. $M : For Stack-Size, Heap-Size will be ignored
  24. $VERSION x.x.x : Sets Major, Minor and Revision
  25. Sorry, Displaying copyright does not work with nlmconv from gnu bunutils.
  26. Exports will be handled like in win32:
  27. procedure bla;
  28. begin
  29. end;
  30. exports bla name 'bla';
  31. Without Name 'bla' this will be exported in upper-case.
  32. The path to the import-Files (from netware-sdk, see developer.novell.com)
  33. must be specified by the library-path. All external modules are defined
  34. as autoload.
  35. i.e. Procedure ConsolePrintf (p:pchar); cdecl; external 'clib.nlm';
  36. sets IMPORT @clib.imp and MODULE clib.
  37. If you dont have nlmconv, compile gnu-binutils with
  38. ./configure --enable-targets=i386-linux,i386-netware
  39. make all
  40. Debugging is currently only possible at assembler level with nwdbg, written
  41. by Jan Beulich. Nwdbg supports symbols but it's not a source-level
  42. debugger. You can get nwdbg from developer.novell.com. To enter the
  43. debugger from your program, define "EnterDebugger" as external cdecl and
  44. call it. Int3 will not work with Netware 5.
  45. A sample program:
  46. Program Hello;
  47. (*$DESCRIPTION HelloWorldNlm*)
  48. (*$VERSION 1.2.2*)
  49. (*$M 8192,8192*)
  50. begin
  51. writeLn ('hello world');
  52. end.
  53. compile with:
  54. ppc386 -Tnetware hello
  55. ToDo:
  56. - No duplicate imports and autoloads
  57. - Screen and Thread-Names
  58. ****************************************************************************
  59. }
  60. unit t_nwm;
  61. {$i defines.inc}
  62. interface
  63. uses
  64. import,export,link;
  65. type
  66. timportlibnetware=class(timportlib)
  67. procedure preparelib(const s:string);override;
  68. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  69. procedure importvariable(const varname,module:string;const name:string);override;
  70. procedure generatelib;override;
  71. end;
  72. texportlibnetware=class(texportlib)
  73. procedure preparelib(const s : string);override;
  74. procedure exportprocedure(hp : texported_item);override;
  75. procedure exportvar(hp : texported_item);override;
  76. procedure generatelib;override;
  77. end;
  78. tlinkernetware=class(tlinker)
  79. private
  80. Function WriteResponseFile(isdll:boolean) : Boolean;
  81. public
  82. constructor Create;
  83. procedure SetDefaultInfo;override;
  84. function MakeExecutable:boolean;override;
  85. end;
  86. implementation
  87. uses
  88. cutils,
  89. verbose,systems,globtype,globals,
  90. symconst,script,
  91. fmodule,aasm,cpuasm,cpubase,symsym;
  92. {*****************************************************************************
  93. TIMPORTLIBNETWARE
  94. *****************************************************************************}
  95. procedure timportlibnetware.preparelib(const s : string);
  96. begin
  97. end;
  98. procedure timportlibnetware.importprocedure(const func,module : string;index : longint;const name : string);
  99. begin
  100. { insert sharedlibrary }
  101. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  102. { do nothing with the procedure, only set the mangledname }
  103. if name<>'' then
  104. aktprocsym^.definition^.setmangledname(name)
  105. else
  106. message(parser_e_empty_import_name);
  107. end;
  108. procedure timportlibnetware.importvariable(const varname,module:string;const name:string);
  109. begin
  110. { insert sharedlibrary }
  111. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  112. { reset the mangledname and turn off the dll_var option }
  113. aktvarsym^.setmangledname(name);
  114. exclude(aktvarsym^.varoptions,vo_is_dll_var);
  115. end;
  116. procedure timportlibnetware.generatelib;
  117. begin
  118. end;
  119. {*****************************************************************************
  120. TEXPORTLIBNETWARE
  121. *****************************************************************************}
  122. procedure texportlibnetware.preparelib(const s:string);
  123. begin
  124. end;
  125. procedure texportlibnetware.exportprocedure(hp : texported_item);
  126. var
  127. hp2 : texported_item;
  128. begin
  129. { first test the index value }
  130. if (hp.options and eo_index)<>0 then
  131. begin
  132. Comment(V_Error,'can''t export with index under netware');
  133. exit;
  134. end;
  135. { use pascal name is none specified }
  136. if (hp.options and eo_name)=0 then
  137. begin
  138. hp.name:=stringdup(hp.sym^.name);
  139. hp.options:=hp.options or eo_name;
  140. end;
  141. { now place in correct order }
  142. hp2:=texported_item(current_module._exports.first);
  143. while assigned(hp2) and
  144. (hp.name^>hp2.name^) do
  145. hp2:=texported_item(hp2.next);
  146. { insert hp there !! }
  147. if assigned(hp2) and (hp2.name^=hp.name^) then
  148. begin
  149. { this is not allowed !! }
  150. Message1(parser_e_export_name_double,hp.name^);
  151. exit;
  152. end;
  153. if hp2=texported_item(current_module._exports.first) then
  154. current_module._exports.insert(hp)
  155. else if assigned(hp2) then
  156. begin
  157. hp.next:=hp2;
  158. hp.previous:=hp2.previous;
  159. if assigned(hp2.previous) then
  160. hp2.previous.next:=hp;
  161. hp2.previous:=hp;
  162. end
  163. else
  164. current_module._exports.concat(hp);
  165. end;
  166. procedure texportlibnetware.exportvar(hp : texported_item);
  167. begin
  168. hp.is_var:=true;
  169. exportprocedure(hp);
  170. end;
  171. procedure texportlibnetware.generatelib;
  172. var
  173. hp2 : texported_item;
  174. begin
  175. hp2:=texported_item(current_module._exports.first);
  176. while assigned(hp2) do
  177. begin
  178. if not hp2.is_var then
  179. begin
  180. {$ifdef i386}
  181. { place jump in codesegment }
  182. codeSegment.concat(Tai_align.Create_op(4,$90));
  183. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  184. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,newasmsymbol(hp2.sym^.mangledname)));
  185. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  186. {$endif i386}
  187. end
  188. else
  189. Comment(V_Error,'Exporting of variables is not supported under netware');
  190. hp2:=texported_item(hp2.next);
  191. end;
  192. end;
  193. {*****************************************************************************
  194. TLINKERNETWARE
  195. *****************************************************************************}
  196. Constructor TLinkerNetware.Create;
  197. begin
  198. Inherited Create;
  199. end;
  200. procedure TLinkerNetware.SetDefaultInfo;
  201. begin
  202. with Info do
  203. begin
  204. ExeCmd[1]:='nlmconv -T$RES';
  205. {DllCmd[1]:='ld $OPT -shared -L. -o $EXE $RES';}
  206. DllCmd[2]:='strip --strip-unneeded $EXE';
  207. end;
  208. end;
  209. Function TLinkerNetware.WriteResponseFile(isdll:boolean) : Boolean;
  210. Var
  211. linkres : TLinkRes;
  212. i : longint;
  213. s,s2 : string;
  214. found : boolean;
  215. ProgNam : string [80];
  216. NlmNam : string [80];
  217. hp2 : texported_item; { for exports }
  218. begin
  219. WriteResponseFile:=False;
  220. ProgNam := current_module.exefilename^;
  221. i:=Pos(target_os.exeext,ProgNam);
  222. if i>0 then
  223. Delete(ProgNam,i,255);
  224. NlmNam := ProgNam + target_os.exeext;
  225. { Open link.res file }
  226. LinkRes.Init(outputexedir+Info.ResName);
  227. if Description <> '' then
  228. LinkRes.Add('DESCRIPTION "' + Description + '"');
  229. LinkRes.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  230. LinkRes.Add('SCREENNAME "' + ProgNam + '"'); { for that, we have }
  231. LinkRes.Add('THREADNAME "' + ProgNam + '"'); { to add comiler directives }
  232. if stacksize > 1024 then
  233. begin
  234. str (stacksize, s);
  235. LinkRes.Add ('STACKSIZE '+s);
  236. end;
  237. { add objectfiles, start with nwpre always }
  238. LinkRes.Add ('INPUT '+FindObjectFile('nwpre',''));
  239. { main objectfiles }
  240. while not ObjectFiles.Empty do
  241. begin
  242. s:=ObjectFiles.GetFirst;
  243. if s<>'' then
  244. LinkRes.Add ('INPUT ' + FindObjectFile (s,''));
  245. end;
  246. { output file (nlm) }
  247. LinkRes.Add ('OUTPUT ' + NlmNam);
  248. { start and stop-procedures }
  249. LinkRes.Add ('START _Prelude'); { defined in rtl/netware/nwpre.pp }
  250. LinkRes.Add ('EXIT _Stop');
  251. //if not (cs_link_strip in aktglobalswitches) then
  252. { ahhhggg: how do i detect if we have debug-symbols ? }
  253. LinkRes.Add ('DEBUG');
  254. { Write staticlibraries, is that correct ? }
  255. if not StaticLibFiles.Empty then
  256. begin
  257. While not StaticLibFiles.Empty do
  258. begin
  259. S:=lower (StaticLibFiles.GetFirst);
  260. if s<>'' then
  261. begin
  262. i:=Pos(target_os.staticlibext,S);
  263. if i>0 then
  264. Delete(S,i,255);
  265. S := S + '.imp';
  266. S := librarysearchpath.FindFile(S,found)+S;
  267. LinkRes.Add('IMPORT @'+s);
  268. end
  269. end;
  270. end;
  271. if not SharedLibFiles.Empty then
  272. begin
  273. While not SharedLibFiles.Empty do
  274. begin
  275. {becuase of upper/lower case mix, we may get duplicate
  276. names but nlmconv ignores that.
  277. Here we are setting the import-files for nlmconv. I.e. for
  278. the module clib or clib.nlm we add IMPORT @clib.imp and also
  279. the module clib.nlm (autoload)
  280. ? may it be better to set autoload's via StaticLibFiles ? }
  281. S:=lower (SharedLibFiles.GetFirst);
  282. if s<>'' then
  283. begin
  284. s2:=s;
  285. i:=Pos(target_os.sharedlibext,S);
  286. if i>0 then
  287. Delete(S,i,255);
  288. S := S + '.imp';
  289. S := librarysearchpath.FindFile(S,found)+S;
  290. LinkRes.Add('IMPORT @'+s);
  291. LinkRes.Add('MODULE '+s2);
  292. end
  293. end;
  294. end;
  295. { write exports }
  296. hp2:=texported_item(current_module._exports.first);
  297. while assigned(hp2) do
  298. begin
  299. if not hp2.is_var then
  300. begin
  301. { Export the Symbol
  302. Warning: The Symbol is converted to upper-case if not explicitly
  303. specified by >>Exports BlaBla NAME 'BlaBla';<< }
  304. Comment(V_Debug,'Exporting '+hp2.name^);
  305. LinkRes.Add ('EXPORT '+hp2.name^);
  306. end
  307. else
  308. { really ? }
  309. Comment(V_Error,'Exporting of variables is not supported under netware');
  310. hp2:=texported_item(hp2.next);
  311. end;
  312. { Write and Close response }
  313. linkres.writetodisk;
  314. linkres.done;
  315. WriteResponseFile:=True;
  316. end;
  317. function TLinkerNetware.MakeExecutable:boolean;
  318. var
  319. binstr,
  320. cmdstr : string;
  321. success : boolean;
  322. DynLinkStr : string[60];
  323. StaticStr,
  324. StripStr : string[40];
  325. begin
  326. if not(cs_link_extern in aktglobalswitches) then
  327. Message1(exec_i_linking,current_module.exefilename^);
  328. { Create some replacements }
  329. StaticStr:='';
  330. StripStr:='';
  331. DynLinkStr:='';
  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',outputexedir+Info.ResName);
  339. Replace(cmdstr,'$STATIC',StaticStr);
  340. Replace(cmdstr,'$STRIP',StripStr);
  341. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  342. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  343. { Remove ReponseFile }
  344. if (success) and not(cs_link_extern in aktglobalswitches) then
  345. RemoveFile(outputexedir+Info.ResName);
  346. MakeExecutable:=success; { otherwise a recursive call to link method }
  347. end;
  348. end.
  349. {
  350. $Log$
  351. Revision 1.5 2000-12-25 00:07:30 peter
  352. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  353. tlinkedlist objects)
  354. Revision 1.4 2000/11/29 00:30:42 florian
  355. * unused units removed from uses clause
  356. * some changes for widestrings
  357. Revision 1.3 2000/10/31 22:02:55 peter
  358. * symtable splitted, no real code changes
  359. Revision 1.2 2000/09/24 15:06:31 peter
  360. * use defines.inc
  361. Revision 1.1 2000/09/11 17:00:23 florian
  362. + first implementation of Netware Module support, thanks to
  363. Armin Diehl ([email protected]) for providing the patches
  364. }