t_nwm.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. implementation
  64. uses
  65. cutils,
  66. verbose,systems,globtype,globals,
  67. symconst,script,
  68. fmodule,aasm,cpuasm,cpubase,symsym,
  69. import,export,link;
  70. type
  71. timportlibnetware=class(timportlib)
  72. procedure preparelib(const s:string);override;
  73. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  74. procedure importvariable(const varname,module:string;const name:string);override;
  75. procedure generatelib;override;
  76. end;
  77. texportlibnetware=class(texportlib)
  78. procedure preparelib(const s : string);override;
  79. procedure exportprocedure(hp : texported_item);override;
  80. procedure exportvar(hp : texported_item);override;
  81. procedure generatelib;override;
  82. end;
  83. tlinkernetware=class(tlinker)
  84. private
  85. Function WriteResponseFile(isdll:boolean) : Boolean;
  86. public
  87. constructor Create;
  88. procedure SetDefaultInfo;override;
  89. function MakeExecutable:boolean;override;
  90. end;
  91. {*****************************************************************************
  92. TIMPORTLIBNETWARE
  93. *****************************************************************************}
  94. procedure timportlibnetware.preparelib(const s : string);
  95. begin
  96. end;
  97. procedure timportlibnetware.importprocedure(const func,module : string;index : longint;const name : string);
  98. begin
  99. { insert sharedlibrary }
  100. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  101. { do nothing with the procedure, only set the mangledname }
  102. if name<>'' then
  103. aktprocsym.definition.setmangledname(name)
  104. else
  105. message(parser_e_empty_import_name);
  106. end;
  107. procedure timportlibnetware.importvariable(const varname,module:string;const name:string);
  108. begin
  109. { insert sharedlibrary }
  110. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  111. { reset the mangledname and turn off the dll_var option }
  112. aktvarsym.setmangledname(name);
  113. exclude(aktvarsym.varoptions,vo_is_dll_var);
  114. end;
  115. procedure timportlibnetware.generatelib;
  116. begin
  117. end;
  118. {*****************************************************************************
  119. TEXPORTLIBNETWARE
  120. *****************************************************************************}
  121. procedure texportlibnetware.preparelib(const s:string);
  122. begin
  123. end;
  124. procedure texportlibnetware.exportprocedure(hp : texported_item);
  125. var
  126. hp2 : texported_item;
  127. begin
  128. { first test the index value }
  129. if (hp.options and eo_index)<>0 then
  130. begin
  131. Comment(V_Error,'can''t export with index under netware');
  132. exit;
  133. end;
  134. { use pascal name is none specified }
  135. if (hp.options and eo_name)=0 then
  136. begin
  137. hp.name:=stringdup(hp.sym.name);
  138. hp.options:=hp.options or eo_name;
  139. end;
  140. { now place in correct order }
  141. hp2:=texported_item(current_module._exports.first);
  142. while assigned(hp2) and
  143. (hp.name^>hp2.name^) do
  144. hp2:=texported_item(hp2.next);
  145. { insert hp there !! }
  146. if assigned(hp2) and (hp2.name^=hp.name^) then
  147. begin
  148. { this is not allowed !! }
  149. Message1(parser_e_export_name_double,hp.name^);
  150. exit;
  151. end;
  152. if hp2=texported_item(current_module._exports.first) then
  153. current_module._exports.insert(hp)
  154. else if assigned(hp2) then
  155. begin
  156. hp.next:=hp2;
  157. hp.previous:=hp2.previous;
  158. if assigned(hp2.previous) then
  159. hp2.previous.next:=hp;
  160. hp2.previous:=hp;
  161. end
  162. else
  163. current_module._exports.concat(hp);
  164. end;
  165. procedure texportlibnetware.exportvar(hp : texported_item);
  166. begin
  167. hp.is_var:=true;
  168. exportprocedure(hp);
  169. end;
  170. procedure texportlibnetware.generatelib;
  171. var
  172. hp2 : texported_item;
  173. begin
  174. hp2:=texported_item(current_module._exports.first);
  175. while assigned(hp2) do
  176. begin
  177. if not hp2.is_var then
  178. begin
  179. {$ifdef i386}
  180. { place jump in codesegment }
  181. codeSegment.concat(Tai_align.Create_op(4,$90));
  182. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  183. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,newasmsymbol(hp2.sym.mangledname)));
  184. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  185. {$endif i386}
  186. end
  187. else
  188. Comment(V_Error,'Exporting of variables is not supported under netware');
  189. hp2:=texported_item(hp2.next);
  190. end;
  191. end;
  192. {*****************************************************************************
  193. TLINKERNETWARE
  194. *****************************************************************************}
  195. Constructor TLinkerNetware.Create;
  196. begin
  197. Inherited Create;
  198. end;
  199. procedure TLinkerNetware.SetDefaultInfo;
  200. begin
  201. with Info do
  202. begin
  203. ExeCmd[1]:='nlmconv -T$RES';
  204. {DllCmd[1]:='ld $OPT -shared -L. -o $EXE $RES';}
  205. DllCmd[2]:='strip --strip-unneeded $EXE';
  206. end;
  207. end;
  208. Function TLinkerNetware.WriteResponseFile(isdll:boolean) : Boolean;
  209. Var
  210. linkres : TLinkRes;
  211. i : longint;
  212. s,s2 : string;
  213. ProgNam : string [80];
  214. NlmNam : string [80];
  215. hp2 : texported_item; { for exports }
  216. begin
  217. WriteResponseFile:=False;
  218. ProgNam := current_module.exefilename^;
  219. i:=Pos(target_info.exeext,ProgNam);
  220. if i>0 then
  221. Delete(ProgNam,i,255);
  222. NlmNam := ProgNam + target_info.exeext;
  223. { Open link.res file }
  224. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  225. if Description <> '' then
  226. LinkRes.Add('DESCRIPTION "' + Description + '"');
  227. LinkRes.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  228. LinkRes.Add('SCREENNAME "' + ProgNam + '"'); { for that, we have }
  229. LinkRes.Add('THREADNAME "' + ProgNam + '"'); { to add comiler directives }
  230. if stacksize > 1024 then
  231. begin
  232. str (stacksize, s);
  233. LinkRes.Add ('STACKSIZE '+s);
  234. end;
  235. { add objectfiles, start with nwpre always }
  236. LinkRes.Add ('INPUT '+FindObjectFile('nwpre',''));
  237. { main objectfiles }
  238. while not ObjectFiles.Empty do
  239. begin
  240. s:=ObjectFiles.GetFirst;
  241. if s<>'' then
  242. LinkRes.Add ('INPUT ' + FindObjectFile (s,''));
  243. end;
  244. { output file (nlm) }
  245. LinkRes.Add ('OUTPUT ' + NlmNam);
  246. { start and stop-procedures }
  247. LinkRes.Add ('START _Prelude'); { defined in rtl/netware/nwpre.pp }
  248. LinkRes.Add ('EXIT _Stop');
  249. //if not (cs_link_strip in aktglobalswitches) then
  250. { ahhhggg: how do i detect if we have debug-symbols ? }
  251. LinkRes.Add ('DEBUG');
  252. { Write staticlibraries, is that correct ? }
  253. if not StaticLibFiles.Empty then
  254. begin
  255. While not StaticLibFiles.Empty do
  256. begin
  257. S:=lower (StaticLibFiles.GetFirst);
  258. if s<>'' then
  259. begin
  260. i:=Pos(target_info.staticlibext,S);
  261. if i>0 then
  262. Delete(S,i,255);
  263. S := S + '.imp';
  264. librarysearchpath.FindFile(S,s);
  265. LinkRes.Add('IMPORT @'+s);
  266. end
  267. end;
  268. end;
  269. if not SharedLibFiles.Empty then
  270. begin
  271. While not SharedLibFiles.Empty do
  272. begin
  273. {becuase of upper/lower case mix, we may get duplicate
  274. names but nlmconv ignores that.
  275. Here we are setting the import-files for nlmconv. I.e. for
  276. the module clib or clib.nlm we add IMPORT @clib.imp and also
  277. the module clib.nlm (autoload)
  278. ? may it be better to set autoload's via StaticLibFiles ? }
  279. S:=lower (SharedLibFiles.GetFirst);
  280. if s<>'' then
  281. begin
  282. s2:=s;
  283. i:=Pos(target_info.sharedlibext,S);
  284. if i>0 then
  285. Delete(S,i,255);
  286. S := S + '.imp';
  287. librarysearchpath.FindFile(S,s);
  288. LinkRes.Add('IMPORT @'+s);
  289. LinkRes.Add('MODULE '+s2);
  290. end
  291. end;
  292. end;
  293. { write exports }
  294. hp2:=texported_item(current_module._exports.first);
  295. while assigned(hp2) do
  296. begin
  297. if not hp2.is_var then
  298. begin
  299. { Export the Symbol
  300. Warning: The Symbol is converted to upper-case if not explicitly
  301. specified by >>Exports BlaBla NAME 'BlaBla';<< }
  302. Comment(V_Debug,'Exporting '+hp2.name^);
  303. LinkRes.Add ('EXPORT '+hp2.name^);
  304. end
  305. else
  306. { really ? }
  307. Comment(V_Error,'Exporting of variables is not supported under netware');
  308. hp2:=texported_item(hp2.next);
  309. end;
  310. { Write and Close response }
  311. linkres.writetodisk;
  312. LinkRes.Free;
  313. WriteResponseFile:=True;
  314. end;
  315. function TLinkerNetware.MakeExecutable:boolean;
  316. var
  317. binstr,
  318. cmdstr : string;
  319. success : boolean;
  320. DynLinkStr : string[60];
  321. StaticStr,
  322. StripStr : string[40];
  323. begin
  324. if not(cs_link_extern in aktglobalswitches) then
  325. Message1(exec_i_linking,current_module.exefilename^);
  326. { Create some replacements }
  327. StaticStr:='';
  328. StripStr:='';
  329. DynLinkStr:='';
  330. { Write used files and libraries }
  331. WriteResponseFile(false);
  332. { Call linker }
  333. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  334. Replace(cmdstr,'$EXE',current_module.exefilename^);
  335. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  336. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  337. Replace(cmdstr,'$STATIC',StaticStr);
  338. Replace(cmdstr,'$STRIP',StripStr);
  339. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  340. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  341. { Remove ReponseFile }
  342. if (success) and not(cs_link_extern in aktglobalswitches) then
  343. RemoveFile(outputexedir+Info.ResName);
  344. MakeExecutable:=success; { otherwise a recursive call to link method }
  345. end;
  346. {*****************************************************************************
  347. Initialize
  348. *****************************************************************************}
  349. const
  350. target_i386_netware_info : ttargetinfo =
  351. (
  352. target : target_i386_NETWARE;
  353. name : 'Netware for i386';
  354. shortname : 'Netware';
  355. flags : [];
  356. cpu : i386;
  357. unit_env : 'NETWAREUNITS';
  358. sharedlibext : '.nlm';
  359. staticlibext : '.a';
  360. sourceext : '.pp';
  361. pasext : '.pas';
  362. exeext : '.nlm';
  363. defext : '.def';
  364. scriptext : '.sh';
  365. smartext : '.sl';
  366. unitext : '.ppn';
  367. unitlibext : '.ppl';
  368. asmext : '.s';
  369. objext : '.on';
  370. resext : '.res';
  371. resobjext : '.or';
  372. libprefix : '';
  373. Cprefix : '';
  374. newline : #13#10;
  375. assem : as_i386_elf32;
  376. assemextern : as_i386_as;
  377. link : ld_i386_netware;
  378. linkextern : ld_i386_netware;
  379. ar : ar_gnu_ar;
  380. res : res_none;
  381. endian : endian_little;
  382. stackalignment : 4;
  383. maxCrecordalignment : 4;
  384. size_of_pointer : 4;
  385. size_of_longint : 4;
  386. heapsize : 256*1024;
  387. maxheapsize : 32768*1024;
  388. stacksize : 8192;
  389. DllScanSupported:false;
  390. use_bound_instruction : false;
  391. use_function_relative_addresses : true
  392. );
  393. initialization
  394. RegisterLinker(ld_i386_netware,TLinkerNetware);
  395. RegisterImport(target_i386_netware,TImportLibNetware);
  396. RegisterExport(target_i386_netware,TExportLibNetware);
  397. RegisterTarget(target_i386_netware_info);
  398. end.
  399. {
  400. $Log$
  401. Revision 1.3 2001-04-18 22:02:04 peter
  402. * registration of targets and assemblers
  403. Revision 1.2 2001/04/13 01:22:21 peter
  404. * symtable change to classes
  405. * range check generation and errors fixed, make cycle DEBUG=1 works
  406. * memory leaks fixed
  407. Revision 1.1 2001/02/26 19:43:11 peter
  408. * moved target units to subdir
  409. Revision 1.6 2001/02/20 21:41:16 peter
  410. * new fixfilename, findfile for unix. Look first for lowercase, then
  411. NormalCase and last for UPPERCASE names.
  412. Revision 1.5 2000/12/25 00:07:30 peter
  413. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  414. tlinkedlist objects)
  415. Revision 1.4 2000/11/29 00:30:42 florian
  416. * unused units removed from uses clause
  417. * some changes for widestrings
  418. Revision 1.3 2000/10/31 22:02:55 peter
  419. * symtable splitted, no real code changes
  420. Revision 1.2 2000/09/24 15:06:31 peter
  421. * use defines.inc
  422. Revision 1.1 2000/09/11 17:00:23 florian
  423. + first implementation of Netware Module support, thanks to
  424. Armin Diehl ([email protected]) for providing the patches
  425. }