t_nwm.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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;override;
  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. p : byte;
  217. begin
  218. WriteResponseFile:=False;
  219. ProgNam := current_module.exefilename^;
  220. i:=Pos(target_info.exeext,ProgNam);
  221. if i>0 then
  222. Delete(ProgNam,i,255);
  223. NlmNam := ProgNam + target_info.exeext;
  224. { Open link.res file }
  225. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  226. p := Pos ('"', Description);
  227. while (p > 0) do
  228. begin
  229. delete (Description,p,1);
  230. p := Pos ('"', Description);
  231. end;
  232. if Description <> '' then
  233. LinkRes.Add('DESCRIPTION "' + Description + '"');
  234. LinkRes.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  235. {if nwscreenname = '' then nwscreenname := ProgNam;
  236. if nwthreadname = '' then nwthreadname := ProgNam;}
  237. p := Pos ('"', nwscreenname);
  238. while (p > 0) do
  239. begin
  240. delete (nwscreenname,p,1);
  241. p := Pos ('"', nwscreenname);
  242. end;
  243. p := Pos ('"', nwthreadname);
  244. while (p > 0) do
  245. begin
  246. delete (nwthreadname,p,1);
  247. p := Pos ('"', nwthreadname);
  248. end;
  249. p := Pos ('"', nwcopyright);
  250. while (p > 0) do
  251. begin
  252. delete (nwcopyright,p,1);
  253. p := Pos ('"', nwcopyright);
  254. end;
  255. if nwscreenname <> '' then
  256. LinkRes.Add('SCREENNAME "' + nwscreenname + '"');
  257. if nwthreadname <> '' then
  258. LinkRes.Add('THREADNAME "' + nwthreadname + '"');
  259. if nwcopyright <> '' then
  260. LinkRes.Add('COPYRIGHT "' + nwcopyright + '"');
  261. if stacksize > 1024 then
  262. begin
  263. str (stacksize, s);
  264. LinkRes.Add ('STACKSIZE '+s);
  265. end;
  266. { add objectfiles, start with nwpre always }
  267. LinkRes.Add ('INPUT '+FindObjectFile('nwpre',''));
  268. { main objectfiles }
  269. while not ObjectFiles.Empty do
  270. begin
  271. s:=ObjectFiles.GetFirst;
  272. if s<>'' then
  273. LinkRes.Add ('INPUT ' + FindObjectFile (s,''));
  274. end;
  275. { output file (nlm) }
  276. LinkRes.Add ('OUTPUT ' + NlmNam);
  277. { start and stop-procedures }
  278. LinkRes.Add ('START _Prelude'); { defined in rtl/netware/nwpre.pp }
  279. LinkRes.Add ('EXIT _Stop');
  280. //if not (cs_link_strip in aktglobalswitches) then
  281. { ahhhggg: how do i detect if we have debug-symbols ? }
  282. LinkRes.Add ('DEBUG');
  283. { Write staticlibraries, is that correct ? }
  284. if not StaticLibFiles.Empty then
  285. begin
  286. While not StaticLibFiles.Empty do
  287. begin
  288. S:=lower (StaticLibFiles.GetFirst);
  289. if s<>'' then
  290. begin
  291. i:=Pos(target_info.staticlibext,S);
  292. if i>0 then
  293. Delete(S,i,255);
  294. S := S + '.imp';
  295. librarysearchpath.FindFile(S,s);
  296. LinkRes.Add('IMPORT @'+s);
  297. end
  298. end;
  299. end;
  300. if not SharedLibFiles.Empty then
  301. begin
  302. While not SharedLibFiles.Empty do
  303. begin
  304. {becuase of upper/lower case mix, we may get duplicate
  305. names but nlmconv ignores that.
  306. Here we are setting the import-files for nlmconv. I.e. for
  307. the module clib or clib.nlm we add IMPORT @clib.imp and also
  308. the module clib.nlm (autoload)
  309. ? may it be better to set autoload's via StaticLibFiles ? }
  310. S:=lower (SharedLibFiles.GetFirst);
  311. if s<>'' then
  312. begin
  313. s2:=s;
  314. i:=Pos(target_info.sharedlibext,S);
  315. if i>0 then
  316. Delete(S,i,255);
  317. S := S + '.imp';
  318. librarysearchpath.FindFile(S,s);
  319. LinkRes.Add('IMPORT @'+s);
  320. LinkRes.Add('MODULE '+s2);
  321. end
  322. end;
  323. end;
  324. { write exports }
  325. hp2:=texported_item(current_module._exports.first);
  326. while assigned(hp2) do
  327. begin
  328. if not hp2.is_var then
  329. begin
  330. { Export the Symbol
  331. Warning: The Symbol is converted to upper-case if not explicitly
  332. specified by >>Exports BlaBla NAME 'BlaBla';<< }
  333. Comment(V_Debug,'Exporting '+hp2.name^);
  334. LinkRes.Add ('EXPORT '+hp2.name^);
  335. end
  336. else
  337. { really ? }
  338. Comment(V_Error,'Exporting of variables is not supported under netware');
  339. hp2:=texported_item(hp2.next);
  340. end;
  341. { Write and Close response }
  342. linkres.writetodisk;
  343. LinkRes.Free;
  344. WriteResponseFile:=True;
  345. end;
  346. function TLinkerNetware.MakeExecutable:boolean;
  347. var
  348. binstr,
  349. cmdstr : string;
  350. success : boolean;
  351. DynLinkStr : string[60];
  352. StaticStr,
  353. StripStr : string[40];
  354. begin
  355. if not(cs_link_extern in aktglobalswitches) then
  356. Message1(exec_i_linking,current_module.exefilename^);
  357. { Create some replacements }
  358. StaticStr:='';
  359. StripStr:='';
  360. DynLinkStr:='';
  361. { Write used files and libraries }
  362. WriteResponseFile(false);
  363. { Call linker }
  364. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  365. Replace(cmdstr,'$EXE',current_module.exefilename^);
  366. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  367. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  368. Replace(cmdstr,'$STATIC',StaticStr);
  369. Replace(cmdstr,'$STRIP',StripStr);
  370. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  371. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  372. { Remove ReponseFile }
  373. if (success) and not(cs_link_extern in aktglobalswitches) then
  374. RemoveFile(outputexedir+Info.ResName);
  375. MakeExecutable:=success; { otherwise a recursive call to link method }
  376. end;
  377. {*****************************************************************************
  378. Initialize
  379. *****************************************************************************}
  380. const
  381. target_i386_netware_info : ttargetinfo =
  382. (
  383. target : target_i386_NETWARE;
  384. name : 'Netware for i386';
  385. shortname : 'Netware';
  386. flags : [];
  387. cpu : i386;
  388. unit_env : 'NETWAREUNITS';
  389. extradefines : '';
  390. sharedlibext : '.nlm';
  391. staticlibext : '.a';
  392. sourceext : '.pp';
  393. pasext : '.pas';
  394. exeext : '.nlm';
  395. defext : '.def';
  396. scriptext : '.sh';
  397. smartext : '.sl';
  398. unitext : '.ppn';
  399. unitlibext : '.ppl';
  400. asmext : '.s';
  401. objext : '.on';
  402. resext : '.res';
  403. resobjext : '.or';
  404. staticlibprefix : '';
  405. sharedlibprefix : '';
  406. Cprefix : '';
  407. newline : #13#10;
  408. assem : as_i386_elf32;
  409. assemextern : as_i386_as;
  410. link : ld_i386_netware;
  411. linkextern : ld_i386_netware;
  412. ar : ar_gnu_ar;
  413. res : res_none;
  414. endian : endian_little;
  415. alignment :
  416. (
  417. procalign : 4;
  418. loopalign : 4;
  419. jumpalign : 0;
  420. constalignmin : 0;
  421. constalignmax : 1;
  422. varalignmin : 0;
  423. varalignmax : 1;
  424. localalignmin : 0;
  425. localalignmax : 1;
  426. paraalign : 4;
  427. recordalignmin : 0;
  428. recordalignmax : 2;
  429. maxCrecordalign : 4
  430. );
  431. size_of_pointer : 4;
  432. size_of_longint : 4;
  433. heapsize : 256*1024;
  434. maxheapsize : 32768*1024;
  435. stacksize : 8192;
  436. DllScanSupported:false;
  437. use_bound_instruction : false;
  438. use_function_relative_addresses : true
  439. );
  440. initialization
  441. RegisterLinker(ld_i386_netware,TLinkerNetware);
  442. RegisterImport(target_i386_netware,TImportLibNetware);
  443. RegisterExport(target_i386_netware,TExportLibNetware);
  444. RegisterTarget(target_i386_netware_info);
  445. end.
  446. {
  447. $Log$
  448. Revision 1.8 2001-07-01 20:16:20 peter
  449. * alignmentinfo record added
  450. * -Oa argument supports more alignment settings that can be specified
  451. per type: PROC,LOOP,VARMIN,VARMAX,CONSTMIN,CONSTMAX,RECORDMIN
  452. RECORDMAX,LOCALMIN,LOCALMAX. It is possible to set the mimimum
  453. required alignment and the maximum usefull alignment. The final
  454. alignment will be choosen per variable size dependent on these
  455. settings
  456. Revision 1.7 2001/06/28 19:46:25 peter
  457. * added override and virtual for constructors
  458. Revision 1.6 2001/06/03 15:15:32 peter
  459. * dllprt0 stub for linux shared libs
  460. * pass -init and -fini for linux shared libs
  461. * libprefix splitted into staticlibprefix and sharedlibprefix
  462. Revision 1.5 2001/06/02 19:22:44 peter
  463. * extradefines field added
  464. Revision 1.4 2001/05/30 21:35:49 peter
  465. * netware patches for copyright, screenname, threadname directives
  466. Revision 1.3 2001/04/18 22:02:04 peter
  467. * registration of targets and assemblers
  468. Revision 1.2 2001/04/13 01:22:21 peter
  469. * symtable change to classes
  470. * range check generation and errors fixed, make cycle DEBUG=1 works
  471. * memory leaks fixed
  472. Revision 1.1 2001/02/26 19:43:11 peter
  473. * moved target units to subdir
  474. Revision 1.6 2001/02/20 21:41:16 peter
  475. * new fixfilename, findfile for unix. Look first for lowercase, then
  476. NormalCase and last for UPPERCASE names.
  477. Revision 1.5 2000/12/25 00:07:30 peter
  478. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  479. tlinkedlist objects)
  480. Revision 1.4 2000/11/29 00:30:42 florian
  481. * unused units removed from uses clause
  482. * some changes for widestrings
  483. Revision 1.3 2000/10/31 22:02:55 peter
  484. * symtable splitted, no real code changes
  485. Revision 1.2 2000/09/24 15:06:31 peter
  486. * use defines.inc
  487. Revision 1.1 2000/09/11 17:00:23 florian
  488. + first implementation of Netware Module support, thanks to
  489. Armin Diehl ([email protected]) for providing the patches
  490. }