t_nwm.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. pimportlibnetware=^timportlibnetware;
  67. timportlibnetware=object(timportlib)
  68. procedure preparelib(const s:string);virtual;
  69. procedure importprocedure(const func,module:string;index:longint;const name:string);virtual;
  70. procedure importvariable(const varname,module:string;const name:string);virtual;
  71. procedure generatelib;virtual;
  72. end;
  73. pexportlibnetware=^texportlibnetware;
  74. texportlibnetware=object(texportlib)
  75. procedure preparelib(const s : string);virtual;
  76. procedure exportprocedure(hp : pexported_item);virtual;
  77. procedure exportvar(hp : pexported_item);virtual;
  78. procedure generatelib;virtual;
  79. end;
  80. plinkernetware=^tlinkernetware;
  81. tlinkernetware=object(tlinker)
  82. private
  83. Function WriteResponseFile(isdll:boolean) : Boolean;
  84. public
  85. constructor Init;
  86. procedure SetDefaultInfo;virtual;
  87. function MakeExecutable:boolean;virtual;
  88. end;
  89. implementation
  90. uses
  91. cutils,verbose,cobjects,systems,globtype,globals,
  92. symconst,script,
  93. fmodule,aasm,cpuasm,cpubase,symtable{$IFDEF NEWST},symbols{$ENDIF NEWST};
  94. {*****************************************************************************
  95. TIMPORTLIBNETWARE
  96. *****************************************************************************}
  97. procedure timportlibnetware.preparelib(const s : string);
  98. begin
  99. end;
  100. procedure timportlibnetware.importprocedure(const func,module : string;index : longint;const name : string);
  101. begin
  102. { insert sharedlibrary }
  103. current_module^.linkothersharedlibs.insert(SplitName(module),link_allways);
  104. { do nothing with the procedure, only set the mangledname }
  105. if name<>'' then
  106. aktprocsym^.definition^.setmangledname(name)
  107. else
  108. message(parser_e_empty_import_name);
  109. end;
  110. procedure timportlibnetware.importvariable(const varname,module:string;const name:string);
  111. begin
  112. { insert sharedlibrary }
  113. current_module^.linkothersharedlibs.insert(SplitName(module),link_allways);
  114. { reset the mangledname and turn off the dll_var option }
  115. aktvarsym^.setmangledname(name);
  116. exclude(aktvarsym^.varoptions,vo_is_dll_var);
  117. end;
  118. procedure timportlibnetware.generatelib;
  119. begin
  120. end;
  121. {*****************************************************************************
  122. TEXPORTLIBNETWARE
  123. *****************************************************************************}
  124. procedure texportlibnetware.preparelib(const s:string);
  125. begin
  126. end;
  127. procedure texportlibnetware.exportprocedure(hp : pexported_item);
  128. var
  129. hp2 : pexported_item;
  130. begin
  131. { first test the index value }
  132. if (hp^.options and eo_index)<>0 then
  133. begin
  134. Comment(V_Error,'can''t export with index under netware');
  135. exit;
  136. end;
  137. { use pascal name is none specified }
  138. if (hp^.options and eo_name)=0 then
  139. begin
  140. hp^.name:=stringdup(hp^.sym^.name);
  141. hp^.options:=hp^.options or eo_name;
  142. end;
  143. { now place in correct order }
  144. hp2:=pexported_item(current_module^._exports^.first);
  145. while assigned(hp2) and
  146. (hp^.name^>hp2^.name^) do
  147. hp2:=pexported_item(hp2^.next);
  148. { insert hp there !! }
  149. if assigned(hp2) and (hp2^.name^=hp^.name^) then
  150. begin
  151. { this is not allowed !! }
  152. Message1(parser_e_export_name_double,hp^.name^);
  153. exit;
  154. end;
  155. if hp2=pexported_item(current_module^._exports^.first) then
  156. current_module^._exports^.insert(hp)
  157. else if assigned(hp2) then
  158. begin
  159. hp^.next:=hp2;
  160. hp^.previous:=hp2^.previous;
  161. if assigned(hp2^.previous) then
  162. hp2^.previous^.next:=hp;
  163. hp2^.previous:=hp;
  164. end
  165. else
  166. current_module^._exports^.concat(hp);
  167. end;
  168. procedure texportlibnetware.exportvar(hp : pexported_item);
  169. begin
  170. hp^.is_var:=true;
  171. exportprocedure(hp);
  172. end;
  173. procedure texportlibnetware.generatelib;
  174. var
  175. hp2 : pexported_item;
  176. begin
  177. hp2:=pexported_item(current_module^._exports^.first);
  178. while assigned(hp2) do
  179. begin
  180. if not hp2^.is_var then
  181. begin
  182. {$ifdef i386}
  183. { place jump in codesegment }
  184. codesegment^.concat(new(pai_align,init_op(4,$90)));
  185. codesegment^.concat(new(pai_symbol,initname_global(hp2^.name^,0)));
  186. codesegment^.concat(new(paicpu,op_sym(A_JMP,S_NO,newasmsymbol(hp2^.sym^.mangledname))));
  187. codesegment^.concat(new(pai_symbol_end,initname(hp2^.name^)));
  188. {$endif i386}
  189. end
  190. else
  191. Comment(V_Error,'Exporting of variables is not supported under netware');
  192. hp2:=pexported_item(hp2^.next);
  193. end;
  194. end;
  195. {*****************************************************************************
  196. TLINKERNETWARE
  197. *****************************************************************************}
  198. Constructor TLinkerNetware.Init;
  199. begin
  200. Inherited Init;
  201. end;
  202. procedure TLinkerNetware.SetDefaultInfo;
  203. begin
  204. with Info do
  205. begin
  206. ExeCmd[1]:='nlmconv -T$RES';
  207. {DllCmd[1]:='ld $OPT -shared -L. -o $EXE $RES';}
  208. DllCmd[2]:='strip --strip-unneeded $EXE';
  209. end;
  210. end;
  211. Function TLinkerNetware.WriteResponseFile(isdll:boolean) : Boolean;
  212. Var
  213. linkres : TLinkRes;
  214. i : longint;
  215. s,s2 : string;
  216. found : boolean;
  217. ProgNam : string [80];
  218. NlmNam : string [80];
  219. hp2 : pexported_item; { for exports }
  220. begin
  221. WriteResponseFile:=False;
  222. ProgNam := current_module^.exefilename^;
  223. i:=Pos(target_os.exeext,ProgNam);
  224. if i>0 then
  225. Delete(ProgNam,i,255);
  226. NlmNam := ProgNam + target_os.exeext;
  227. { Open link.res file }
  228. LinkRes.Init(outputexedir+Info.ResName);
  229. if Description <> '' then
  230. LinkRes.Add('DESCRIPTION "' + Description + '"');
  231. LinkRes.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  232. LinkRes.Add('SCREENNAME "' + ProgNam + '"'); { for that, we have }
  233. LinkRes.Add('THREADNAME "' + ProgNam + '"'); { to add comiler directives }
  234. if stacksize > 1024 then
  235. begin
  236. str (stacksize, s);
  237. LinkRes.Add ('STACKSIZE '+s);
  238. end;
  239. { add objectfiles, start with nwpre always }
  240. LinkRes.Add ('INPUT '+FindObjectFile('nwpre',''));
  241. { main objectfiles }
  242. while not ObjectFiles.Empty do
  243. begin
  244. s:=ObjectFiles.Get;
  245. if s<>'' then
  246. LinkRes.Add ('INPUT ' + FindObjectFile (s,''));
  247. end;
  248. { output file (nlm) }
  249. LinkRes.Add ('OUTPUT ' + NlmNam);
  250. { start and stop-procedures }
  251. LinkRes.Add ('START _Prelude'); { defined in rtl/netware/nwpre.pp }
  252. LinkRes.Add ('EXIT _Stop');
  253. //if not (cs_link_strip in aktglobalswitches) then
  254. { ahhhggg: how do i detect if we have debug-symbols ? }
  255. LinkRes.Add ('DEBUG');
  256. { Write staticlibraries, is that correct ? }
  257. if not StaticLibFiles.Empty then
  258. begin
  259. While not StaticLibFiles.Empty do
  260. begin
  261. S:=lower (StaticLibFiles.Get);
  262. if s<>'' then
  263. begin
  264. i:=Pos(target_os.staticlibext,S);
  265. if i>0 then
  266. Delete(S,i,255);
  267. S := S + '.imp';
  268. S := librarysearchpath.FindFile(S,found)+S;
  269. LinkRes.Add('IMPORT @'+s);
  270. end
  271. end;
  272. end;
  273. if not SharedLibFiles.Empty then
  274. begin
  275. While not SharedLibFiles.Empty do
  276. begin
  277. {becuase of upper/lower case mix, we may get duplicate
  278. names but nlmconv ignores that.
  279. Here we are setting the import-files for nlmconv. I.e. for
  280. the module clib or clib.nlm we add IMPORT @clib.imp and also
  281. the module clib.nlm (autoload)
  282. ? may it be better to set autoload's via StaticLibFiles ? }
  283. S:=lower (SharedLibFiles.Get);
  284. if s<>'' then
  285. begin
  286. s2:=s;
  287. i:=Pos(target_os.sharedlibext,S);
  288. if i>0 then
  289. Delete(S,i,255);
  290. S := S + '.imp';
  291. S := librarysearchpath.FindFile(S,found)+S;
  292. LinkRes.Add('IMPORT @'+s);
  293. LinkRes.Add('MODULE '+s2);
  294. end
  295. end;
  296. end;
  297. { write exports }
  298. hp2:=pexported_item(current_module^._exports^.first);
  299. while assigned(hp2) do
  300. begin
  301. if not hp2^.is_var then
  302. begin
  303. { Export the Symbol
  304. Warning: The Symbol is converted to upper-case if not explicitly
  305. specified by >>Exports BlaBla NAME 'BlaBla';<< }
  306. Comment(V_Debug,'Exporting '+hp2^.name^);
  307. LinkRes.Add ('EXPORT '+hp2^.name^);
  308. end
  309. else
  310. { really ? }
  311. Comment(V_Error,'Exporting of variables is not supported under netware');
  312. hp2:=pexported_item(hp2^.next);
  313. end;
  314. { Write and Close response }
  315. linkres.writetodisk;
  316. linkres.done;
  317. WriteResponseFile:=True;
  318. end;
  319. function TLinkerNetware.MakeExecutable:boolean;
  320. var
  321. binstr,
  322. cmdstr : string;
  323. success : boolean;
  324. DynLinkStr : string[60];
  325. StaticStr,
  326. StripStr : string[40];
  327. begin
  328. if not(cs_link_extern in aktglobalswitches) then
  329. Message1(exec_i_linking,current_module^.exefilename^);
  330. { Create some replacements }
  331. StaticStr:='';
  332. StripStr:='';
  333. DynLinkStr:='';
  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',outputexedir+Info.ResName);
  341. Replace(cmdstr,'$STATIC',StaticStr);
  342. Replace(cmdstr,'$STRIP',StripStr);
  343. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  344. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  345. { Remove ReponseFile }
  346. if (success) and not(cs_link_extern in aktglobalswitches) then
  347. RemoveFile(outputexedir+Info.ResName);
  348. MakeExecutable:=success; { otherwise a recursive call to link method }
  349. end;
  350. end.
  351. {
  352. $Log$
  353. Revision 1.2 2000-09-24 15:06:31 peter
  354. * use defines.inc
  355. Revision 1.1 2000/09/11 17:00:23 florian
  356. + first implementation of Netware Module support, thanks to
  357. Armin Diehl ([email protected]) for providing the patches
  358. }