t_nwm.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 and win32.
  19. (see http://home.arcor.de/armin.diehl/fpcnw for binutils working
  20. with win32) while not included in fpc-releases.
  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. $SCREENNAME : Sets the ScreenName
  26. $THREADNAME : Sets cirrent threadname
  27. Sorry, Displaying copyright does not work with nlmconv from gnu bunutils
  28. but there is a patch available.
  29. Exports will be handled like in win32:
  30. procedure bla;
  31. begin
  32. end;
  33. exports foo name 'Bar';
  34. The path to the import-Files (from netware-sdk, see developer.novell.com)
  35. must be specified by the library-path. All external modules are defined
  36. as autoload. (Note: the import-files have to be in unix-format for exe2nlm)
  37. By default, the most import files are included in freepascal.
  38. i.e. Procedure ConsolePrintf (p:pchar); cdecl; external 'clib.nlm';
  39. sets IMPORT @clib.imp and MODULE clib.
  40. If you dont have nlmconv, compile gnu-binutils with
  41. ./configure --enable-targets=i386-linux,i386-netware
  42. make all
  43. Debugging is currently only possible at assembler level with nwdbg, written
  44. by Jan Beulich. (or with my modified RDebug) Nwdbg supports symbols but it's
  45. not a source-level debugger. You can get nwdbg from developer.novell.com.
  46. To enter the debugger from your program, call _EnterDebugger (defined in unit system).
  47. A sample program:
  48. Program Hello;
  49. (*$DESCRIPTION HelloWorldNlm*)
  50. (*$VERSION 1.2.3*)
  51. (*$ScreenName Hello*)
  52. (*$M 8192,8192*)
  53. begin
  54. writeLn ('hello world');
  55. end.
  56. compile with:
  57. ppc386 -Tnetware hello
  58. ToDo:
  59. - No duplicate imports and autoloads
  60. - No debug symbols
  61. - libc support (needs new target)
  62. - prelude support (needs new compiler switch)
  63. - a lot of additional units from nwsdk
  64. ****************************************************************************
  65. }
  66. unit t_nwm;
  67. {$i fpcdefs.inc}
  68. interface
  69. implementation
  70. uses
  71. cutils,
  72. verbose,systems,globtype,globals,
  73. symconst,script,
  74. fmodule,aasmbase,aasmtai,aasmcpu,cpubase,symsym,
  75. import,export,link;
  76. type
  77. timportlibnetware=class(timportlib)
  78. procedure preparelib(const s:string);override;
  79. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  80. procedure importvariable(const varname,module:string;const name:string);override;
  81. procedure generatelib;override;
  82. end;
  83. texportlibnetware=class(texportlib)
  84. procedure preparelib(const s : string);override;
  85. procedure exportprocedure(hp : texported_item);override;
  86. procedure exportvar(hp : texported_item);override;
  87. procedure generatelib;override;
  88. end;
  89. tlinkernetware=class(texternallinker)
  90. private
  91. Function WriteResponseFile(isdll:boolean) : Boolean;
  92. public
  93. constructor Create;override;
  94. procedure SetDefaultInfo;override;
  95. function MakeExecutable:boolean;override;
  96. end;
  97. {*****************************************************************************
  98. TIMPORTLIBNETWARE
  99. *****************************************************************************}
  100. procedure timportlibnetware.preparelib(const s : string);
  101. begin
  102. end;
  103. procedure timportlibnetware.importprocedure(const func,module : string;index : longint;const name : string);
  104. begin
  105. { insert sharedlibrary }
  106. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  107. { do nothing with the procedure, only set the mangledname }
  108. if name<>'' then
  109. begin
  110. aktprocdef.setmangledname(name);
  111. aktprocdef.has_mangledname:=true;
  112. end
  113. else
  114. message(parser_e_empty_import_name);
  115. end;
  116. procedure timportlibnetware.importvariable(const varname,module:string;const name:string);
  117. begin
  118. { insert sharedlibrary }
  119. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  120. { reset the mangledname and turn off the dll_var option }
  121. aktvarsym.set_mangledname(name);
  122. exclude(aktvarsym.varoptions,vo_is_dll_var);
  123. end;
  124. procedure timportlibnetware.generatelib;
  125. begin
  126. end;
  127. {*****************************************************************************
  128. TEXPORTLIBNETWARE
  129. *****************************************************************************}
  130. procedure texportlibnetware.preparelib(const s:string);
  131. begin
  132. end;
  133. procedure texportlibnetware.exportprocedure(hp : texported_item);
  134. var
  135. hp2 : texported_item;
  136. begin
  137. { first test the index value }
  138. if (hp.options and eo_index)<>0 then
  139. begin
  140. Comment(V_Error,'can''t export with index under netware');
  141. exit;
  142. end;
  143. { use pascal name is none specified }
  144. if (hp.options and eo_name)=0 then
  145. begin
  146. hp.name:=stringdup(hp.sym.name);
  147. hp.options:=hp.options or eo_name;
  148. end;
  149. { now place in correct order }
  150. hp2:=texported_item(current_module._exports.first);
  151. while assigned(hp2) and
  152. (hp.name^>hp2.name^) do
  153. hp2:=texported_item(hp2.next);
  154. { insert hp there !! }
  155. if assigned(hp2) and (hp2.name^=hp.name^) then
  156. begin
  157. { this is not allowed !! }
  158. Message1(parser_e_export_name_double,hp.name^);
  159. exit;
  160. end;
  161. if hp2=texported_item(current_module._exports.first) then
  162. current_module._exports.insert(hp)
  163. else if assigned(hp2) then
  164. begin
  165. hp.next:=hp2;
  166. hp.previous:=hp2.previous;
  167. if assigned(hp2.previous) then
  168. hp2.previous.next:=hp;
  169. hp2.previous:=hp;
  170. end
  171. else
  172. current_module._exports.concat(hp);
  173. end;
  174. procedure texportlibnetware.exportvar(hp : texported_item);
  175. begin
  176. hp.is_var:=true;
  177. exportprocedure(hp);
  178. end;
  179. procedure texportlibnetware.generatelib;
  180. var
  181. hp2 : texported_item;
  182. begin
  183. hp2:=texported_item(current_module._exports.first);
  184. while assigned(hp2) do
  185. begin
  186. if (not hp2.is_var) and
  187. (hp2.sym.typ=procsym) then
  188. begin
  189. { the manglednames can already be the same when the procedure
  190. is declared with cdecl }
  191. if tprocsym(hp2.sym).defs^.def.mangledname<>hp2.name^ then
  192. begin
  193. {$ifdef i386}
  194. { place jump in codesegment }
  195. codesegment.concat(Tai_align.Create_op(4,$90));
  196. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  197. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,newasmsymbol(tprocsym(hp2.sym).defs^.def.mangledname)));
  198. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  199. {$endif i386}
  200. end;
  201. end
  202. else
  203. //Comment(V_Error,'Exporting of variables is not supported under netware');
  204. Message1(parser_e_no_export_of_variables_for_target,'netware');
  205. hp2:=texported_item(hp2.next);
  206. end;
  207. end;
  208. {*****************************************************************************
  209. TLINKERNETWARE
  210. *****************************************************************************}
  211. Constructor TLinkerNetware.Create;
  212. begin
  213. Inherited Create;
  214. end;
  215. procedure TLinkerNetware.SetDefaultInfo;
  216. begin
  217. with Info do
  218. begin
  219. ExeCmd[1]:='nlmconv -T$RES';
  220. {DllCmd[2]:='strip --strip-unneeded $EXE';}
  221. end;
  222. end;
  223. Function TLinkerNetware.WriteResponseFile(isdll:boolean) : Boolean;
  224. Var
  225. linkres : TLinkRes;
  226. i : longint;
  227. s,s2 : string;
  228. ProgNam : string [80];
  229. NlmNam : string [80];
  230. hp2 : texported_item; { for exports }
  231. p : byte;
  232. begin
  233. WriteResponseFile:=False;
  234. ProgNam := current_module.exefilename^;
  235. i:=Pos(target_info.exeext,ProgNam);
  236. if i>0 then
  237. Delete(ProgNam,i,255);
  238. NlmNam := ProgNam + target_info.exeext;
  239. { Open link.res file }
  240. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  241. p := Pos ('"', Description);
  242. while (p > 0) do
  243. begin
  244. delete (Description,p,1);
  245. p := Pos ('"', Description);
  246. end;
  247. if Description <> '' then
  248. LinkRes.Add('DESCRIPTION "' + Description + '"');
  249. LinkRes.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  250. p := Pos ('"', nwscreenname);
  251. while (p > 0) do
  252. begin
  253. delete (nwscreenname,p,1);
  254. p := Pos ('"', nwscreenname);
  255. end;
  256. p := Pos ('"', nwthreadname);
  257. while (p > 0) do
  258. begin
  259. delete (nwthreadname,p,1);
  260. p := Pos ('"', nwthreadname);
  261. end;
  262. p := Pos ('"', nwcopyright);
  263. while (p > 0) do
  264. begin
  265. delete (nwcopyright,p,1);
  266. p := Pos ('"', nwcopyright);
  267. end;
  268. if nwscreenname <> '' then
  269. LinkRes.Add('SCREENNAME "' + nwscreenname + '"');
  270. if nwthreadname <> '' then
  271. LinkRes.Add('THREADNAME "' + nwthreadname + '"');
  272. if nwcopyright <> '' then
  273. LinkRes.Add('COPYRIGHT "' + nwcopyright + '"');
  274. if stacksize > 1024 then
  275. begin
  276. str (stacksize, s);
  277. LinkRes.Add ('STACKSIZE '+s);
  278. end;
  279. { add objectfiles, start with nwpre always }
  280. LinkRes.Add ('INPUT '+FindObjectFile('nwpre',''));
  281. { main objectfiles }
  282. while not ObjectFiles.Empty do
  283. begin
  284. s:=ObjectFiles.GetFirst;
  285. if s<>'' then
  286. LinkRes.Add ('INPUT ' + FindObjectFile (s,''));
  287. end;
  288. { output file (nlm) }
  289. LinkRes.Add ('OUTPUT ' + NlmNam);
  290. { start and stop-procedures }
  291. LinkRes.Add ('START _Prelude'); { defined in rtl/netware/nwpre.pp }
  292. LinkRes.Add ('EXIT _Stop');
  293. LinkRes.Add ('CHECK FPC_NW_CHECKFUNCTION');
  294. if not (cs_link_strip in aktglobalswitches) then
  295. LinkRes.Add ('DEBUG');
  296. { Write staticlibraries, is that correct ? }
  297. if not StaticLibFiles.Empty then
  298. begin
  299. While not StaticLibFiles.Empty do
  300. begin
  301. S:=lower (StaticLibFiles.GetFirst);
  302. if s<>'' then
  303. begin
  304. {ad: that's a hack !
  305. whith -XX we get the .a files as static libs (in addition to the
  306. imported libraries}
  307. if (pos ('.a',s) <> 0) OR (pos ('.A', s) <> 0) then
  308. begin
  309. LinkRes.Add ('INPUT '+FindObjectFile(s,''));
  310. end else
  311. begin
  312. i:=Pos(target_info.staticlibext,S);
  313. if i>0 then
  314. Delete(S,i,255);
  315. S := S + '.imp';
  316. librarysearchpath.FindFile(S,s);
  317. LinkRes.Add('IMPORT @'+s);
  318. end;
  319. end
  320. end;
  321. end;
  322. if not SharedLibFiles.Empty then
  323. begin
  324. While not SharedLibFiles.Empty do
  325. begin
  326. {becuase of upper/lower case mix, we may get duplicate
  327. names but nlmconv ignores that.
  328. Here we are setting the import-files for nlmconv. I.e. for
  329. the module clib or clib.nlm we add IMPORT @clib.imp and also
  330. the module clib.nlm (autoload)
  331. ? may it be better to set autoload's via StaticLibFiles ? }
  332. S:=lower (SharedLibFiles.GetFirst);
  333. if s<>'' then
  334. begin
  335. s2:=s;
  336. i:=Pos(target_info.sharedlibext,S);
  337. if i>0 then
  338. Delete(S,i,255);
  339. S := S + '.imp';
  340. librarysearchpath.FindFile(S,s);
  341. LinkRes.Add('IMPORT @'+s);
  342. LinkRes.Add('MODULE '+s2);
  343. end
  344. end;
  345. end;
  346. { write exports }
  347. hp2:=texported_item(current_module._exports.first);
  348. while assigned(hp2) do
  349. begin
  350. if not hp2.is_var then
  351. begin
  352. { Export the Symbol }
  353. Comment(V_Debug,'Exporting '+hp2.name^);
  354. LinkRes.Add ('EXPORT '+hp2.name^);
  355. end
  356. else
  357. { really, i think it is possible }
  358. Comment(V_Error,'Exporting of variables is not supported under netware');
  359. hp2:=texported_item(hp2.next);
  360. end;
  361. { Write and Close response }
  362. linkres.writetodisk;
  363. LinkRes.Free;
  364. WriteResponseFile:=True;
  365. end;
  366. function TLinkerNetware.MakeExecutable:boolean;
  367. var
  368. binstr,
  369. cmdstr : string;
  370. success : boolean;
  371. DynLinkStr : string[60];
  372. StaticStr,
  373. StripStr : string[40];
  374. begin
  375. if not(cs_link_extern in aktglobalswitches) then
  376. Message1(exec_i_linking,current_module.exefilename^);
  377. { Create some replacements }
  378. StaticStr:='';
  379. StripStr:='';
  380. DynLinkStr:='';
  381. { Write used files and libraries }
  382. WriteResponseFile(false);
  383. { Call linker }
  384. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  385. Replace(cmdstr,'$EXE',current_module.exefilename^);
  386. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  387. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  388. Replace(cmdstr,'$STATIC',StaticStr);
  389. Replace(cmdstr,'$STRIP',StripStr);
  390. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  391. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  392. { Remove ReponseFile }
  393. if (success) and not(cs_link_extern in aktglobalswitches) then
  394. RemoveFile(outputexedir+Info.ResName);
  395. MakeExecutable:=success; { otherwise a recursive call to link method }
  396. end;
  397. {*****************************************************************************
  398. Initialize
  399. *****************************************************************************}
  400. const
  401. target_i386_netware_info : ttargetinfo =
  402. (
  403. target : target_i386_NETWARE;
  404. name : 'Netware for i386';
  405. shortname : 'Netware';
  406. flags : [];
  407. cpu : cpu_i386;
  408. unit_env : 'NETWAREUNITS';
  409. extradefines : '';
  410. sourceext : '.pp';
  411. pasext : '.pas';
  412. exeext : '.nlm';
  413. defext : '.def';
  414. scriptext : '.sh';
  415. smartext : '.sl';
  416. unitext : '.ppn';
  417. unitlibext : '.ppl';
  418. asmext : '.s';
  419. objext : '.on';
  420. resext : '.res';
  421. resobjext : '.or';
  422. sharedlibext : '.nlm';
  423. staticlibext : '.a';
  424. staticlibprefix : '';
  425. sharedlibprefix : '';
  426. sharedClibext : '.nlm';
  427. staticClibext : '.a';
  428. staticClibprefix : '';
  429. sharedClibprefix : '';
  430. Cprefix : '';
  431. newline : #13#10;
  432. dirsep : '\';
  433. files_case_relevent : false;
  434. assem : as_i386_elf32;
  435. assemextern : as_i386_as;
  436. link : ld_i386_netware;
  437. linkextern : ld_i386_netware;
  438. ar : ar_gnu_ar;
  439. res : res_none;
  440. script : script_unix;
  441. endian : endian_little;
  442. alignment :
  443. (
  444. procalign : 4;
  445. loopalign : 4;
  446. jumpalign : 0;
  447. constalignmin : 0;
  448. constalignmax : 1;
  449. varalignmin : 0;
  450. varalignmax : 1;
  451. localalignmin : 0;
  452. localalignmax : 1;
  453. paraalign : 4;
  454. recordalignmin : 0;
  455. recordalignmax : 2;
  456. maxCrecordalign : 4
  457. );
  458. first_parm_offset : 8;
  459. heapsize : 256*1024;
  460. stacksize : 8192;
  461. DllScanSupported:false;
  462. use_function_relative_addresses : true
  463. );
  464. initialization
  465. RegisterLinker(ld_i386_netware,TLinkerNetware);
  466. RegisterImport(target_i386_netware,TImportLibNetware);
  467. RegisterExport(target_i386_netware,TExportLibNetware);
  468. RegisterTarget(target_i386_netware_info);
  469. end.
  470. {
  471. $Log$
  472. Revision 1.25 2002-07-01 18:46:35 peter
  473. * internal linker
  474. * reorganized aasm layer
  475. Revision 1.24 2002/05/18 13:34:27 peter
  476. * readded missing revisions
  477. Revision 1.23 2002/05/16 19:46:53 carl
  478. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  479. + try to fix temp allocation (still in ifdef)
  480. + generic constructor calls
  481. + start of tassembler / tmodulebase class cleanup
  482. Revision 1.21 2002/04/22 18:19:22 carl
  483. - remove use_bound_instruction field
  484. Revision 1.20 2002/04/20 21:43:18 carl
  485. * fix stack size for some targets
  486. + add offset to parameters from frame pointer info.
  487. - remove some unused stuff
  488. Revision 1.19 2002/04/19 15:46:05 peter
  489. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  490. in most cases and not written to the ppu
  491. * add mangeledname_prefix() routine to generate the prefix of
  492. manglednames depending on the current procedure, object and module
  493. * removed static procprefix since the mangledname is now build only
  494. on demand from tprocdef.mangledname
  495. Revision 1.18 2002/04/15 19:16:57 carl
  496. - remove size_of_pointer field
  497. Revision 1.17 2002/03/30 09:09:47 armin
  498. + support check-function for netware
  499. Revision 1.16 2002/03/29 17:19:51 armin
  500. + allow exports for netware
  501. Revision 1.15 2002/03/19 20:23:57 armin
  502. + smart linking now works with netware
  503. Revision 1.14 2002/03/04 17:54:59 peter
  504. * allow oridinal labels again
  505. Revision 1.13 2002/03/03 13:00:39 hajny
  506. * importprocedure fix by Armin Diehl
  507. }