t_nwm.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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,i_nwm;
  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(vs:tvarsym;const name,module: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. end
  112. else
  113. message(parser_e_empty_import_name);
  114. end;
  115. procedure timportlibnetware.importvariable(vs:tvarsym;const name,module:string);
  116. begin
  117. { insert sharedlibrary }
  118. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  119. { reset the mangledname and turn off the dll_var option }
  120. vs.set_mangledname(name);
  121. exclude(vs.varoptions,vo_is_dll_var);
  122. end;
  123. procedure timportlibnetware.generatelib;
  124. begin
  125. end;
  126. {*****************************************************************************
  127. TEXPORTLIBNETWARE
  128. *****************************************************************************}
  129. procedure texportlibnetware.preparelib(const s:string);
  130. begin
  131. end;
  132. procedure texportlibnetware.exportprocedure(hp : texported_item);
  133. var
  134. hp2 : texported_item;
  135. begin
  136. { first test the index value }
  137. if (hp.options and eo_index)<>0 then
  138. begin
  139. Comment(V_Error,'can''t export with index under netware');
  140. exit;
  141. end;
  142. { use pascal name is none specified }
  143. if (hp.options and eo_name)=0 then
  144. begin
  145. hp.name:=stringdup(hp.sym.name);
  146. hp.options:=hp.options or eo_name;
  147. end;
  148. { now place in correct order }
  149. hp2:=texported_item(current_module._exports.first);
  150. while assigned(hp2) and
  151. (hp.name^>hp2.name^) do
  152. hp2:=texported_item(hp2.next);
  153. { insert hp there !! }
  154. if assigned(hp2) and (hp2.name^=hp.name^) then
  155. begin
  156. { this is not allowed !! }
  157. Message1(parser_e_export_name_double,hp.name^);
  158. exit;
  159. end;
  160. if hp2=texported_item(current_module._exports.first) then
  161. current_module._exports.insert(hp)
  162. else if assigned(hp2) then
  163. begin
  164. hp.next:=hp2;
  165. hp.previous:=hp2.previous;
  166. if assigned(hp2.previous) then
  167. hp2.previous.next:=hp;
  168. hp2.previous:=hp;
  169. end
  170. else
  171. current_module._exports.concat(hp);
  172. end;
  173. procedure texportlibnetware.exportvar(hp : texported_item);
  174. begin
  175. hp.is_var:=true;
  176. exportprocedure(hp);
  177. end;
  178. procedure texportlibnetware.generatelib;
  179. var
  180. hp2 : texported_item;
  181. begin
  182. hp2:=texported_item(current_module._exports.first);
  183. while assigned(hp2) do
  184. begin
  185. if (not hp2.is_var) and
  186. (hp2.sym.typ=procsym) then
  187. begin
  188. { the manglednames can already be the same when the procedure
  189. is declared with cdecl }
  190. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  191. begin
  192. {$ifdef i386}
  193. { place jump in codesegment }
  194. codesegment.concat(Tai_align.Create_op(4,$90));
  195. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  196. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,objectlibrary.newasmsymbol(tprocsym(hp2.sym).first_procdef.mangledname)));
  197. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  198. {$endif i386}
  199. end;
  200. end
  201. else
  202. //Comment(V_Error,'Exporting of variables is not supported under netware');
  203. Message1(parser_e_no_export_of_variables_for_target,'netware');
  204. hp2:=texported_item(hp2.next);
  205. end;
  206. end;
  207. {*****************************************************************************
  208. TLINKERNETWARE
  209. *****************************************************************************}
  210. Constructor TLinkerNetware.Create;
  211. begin
  212. Inherited Create;
  213. end;
  214. procedure TLinkerNetware.SetDefaultInfo;
  215. begin
  216. with Info do
  217. begin
  218. ExeCmd[1]:='nlmconv -T$RES';
  219. {DllCmd[2]:='strip --strip-unneeded $EXE';}
  220. end;
  221. end;
  222. Function TLinkerNetware.WriteResponseFile(isdll:boolean) : Boolean;
  223. Var
  224. linkres : TLinkRes;
  225. i : longint;
  226. s,s2 : string;
  227. ProgNam : string [80];
  228. NlmNam : string [80];
  229. hp2 : texported_item; { for exports }
  230. p : byte;
  231. begin
  232. WriteResponseFile:=False;
  233. ProgNam := current_module.exefilename^;
  234. i:=Pos(target_info.exeext,ProgNam);
  235. if i>0 then
  236. Delete(ProgNam,i,255);
  237. NlmNam := ProgNam + target_info.exeext;
  238. { Open link.res file }
  239. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  240. p := Pos ('"', Description);
  241. while (p > 0) do
  242. begin
  243. delete (Description,p,1);
  244. p := Pos ('"', Description);
  245. end;
  246. if Description <> '' then
  247. LinkRes.Add('DESCRIPTION "' + Description + '"');
  248. LinkRes.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  249. p := Pos ('"', nwscreenname);
  250. while (p > 0) do
  251. begin
  252. delete (nwscreenname,p,1);
  253. p := Pos ('"', nwscreenname);
  254. end;
  255. p := Pos ('"', nwthreadname);
  256. while (p > 0) do
  257. begin
  258. delete (nwthreadname,p,1);
  259. p := Pos ('"', nwthreadname);
  260. end;
  261. p := Pos ('"', nwcopyright);
  262. while (p > 0) do
  263. begin
  264. delete (nwcopyright,p,1);
  265. p := Pos ('"', nwcopyright);
  266. end;
  267. if nwscreenname <> '' then
  268. LinkRes.Add('SCREENNAME "' + nwscreenname + '"');
  269. if nwthreadname <> '' then
  270. LinkRes.Add('THREADNAME "' + nwthreadname + '"');
  271. if nwcopyright <> '' then
  272. LinkRes.Add('COPYRIGHT "' + nwcopyright + '"');
  273. if stacksize > 1024 then
  274. begin
  275. str (stacksize, s);
  276. LinkRes.Add ('STACKSIZE '+s);
  277. end;
  278. { add objectfiles, start with nwpre always }
  279. LinkRes.Add ('INPUT '+FindObjectFile('nwpre',''));
  280. { main objectfiles }
  281. while not ObjectFiles.Empty do
  282. begin
  283. s:=ObjectFiles.GetFirst;
  284. if s<>'' then
  285. LinkRes.Add ('INPUT ' + FindObjectFile (s,''));
  286. end;
  287. { output file (nlm) }
  288. LinkRes.Add ('OUTPUT ' + NlmNam);
  289. { start and stop-procedures }
  290. LinkRes.Add ('START _Prelude'); { defined in rtl/netware/nwpre.pp }
  291. LinkRes.Add ('EXIT _Stop');
  292. LinkRes.Add ('CHECK FPC_NW_CHECKFUNCTION');
  293. if not (cs_link_strip in aktglobalswitches) then
  294. LinkRes.Add ('DEBUG');
  295. { Write staticlibraries, is that correct ? }
  296. if not StaticLibFiles.Empty then
  297. begin
  298. While not StaticLibFiles.Empty do
  299. begin
  300. S:=lower (StaticLibFiles.GetFirst);
  301. if s<>'' then
  302. begin
  303. {ad: that's a hack !
  304. whith -XX we get the .a files as static libs (in addition to the
  305. imported libraries}
  306. if (pos ('.a',s) <> 0) OR (pos ('.A', s) <> 0) then
  307. begin
  308. LinkRes.Add ('INPUT '+FindObjectFile(s,''));
  309. end else
  310. begin
  311. i:=Pos(target_info.staticlibext,S);
  312. if i>0 then
  313. Delete(S,i,255);
  314. S := S + '.imp';
  315. librarysearchpath.FindFile(S,s);
  316. LinkRes.Add('IMPORT @'+s);
  317. end;
  318. end
  319. end;
  320. end;
  321. if not SharedLibFiles.Empty then
  322. begin
  323. While not SharedLibFiles.Empty do
  324. begin
  325. {becuase of upper/lower case mix, we may get duplicate
  326. names but nlmconv ignores that.
  327. Here we are setting the import-files for nlmconv. I.e. for
  328. the module clib or clib.nlm we add IMPORT @clib.imp and also
  329. the module clib.nlm (autoload)
  330. ? may it be better to set autoload's via StaticLibFiles ? }
  331. S:=lower (SharedLibFiles.GetFirst);
  332. if s<>'' then
  333. begin
  334. s2:=s;
  335. i:=Pos(target_info.sharedlibext,S);
  336. if i>0 then
  337. Delete(S,i,255);
  338. S := S + '.imp';
  339. librarysearchpath.FindFile(S,s);
  340. LinkRes.Add('IMPORT @'+s);
  341. LinkRes.Add('MODULE '+s2);
  342. end
  343. end;
  344. end;
  345. { write exports }
  346. hp2:=texported_item(current_module._exports.first);
  347. while assigned(hp2) do
  348. begin
  349. if not hp2.is_var then
  350. begin
  351. { Export the Symbol }
  352. Comment(V_Debug,'Exporting '+hp2.name^);
  353. LinkRes.Add ('EXPORT '+hp2.name^);
  354. end
  355. else
  356. { really, i think it is possible }
  357. Comment(V_Error,'Exporting of variables is not supported under netware');
  358. hp2:=texported_item(hp2.next);
  359. end;
  360. { Write and Close response }
  361. linkres.writetodisk;
  362. LinkRes.Free;
  363. WriteResponseFile:=True;
  364. end;
  365. function TLinkerNetware.MakeExecutable:boolean;
  366. var
  367. binstr,
  368. cmdstr : string;
  369. success : boolean;
  370. DynLinkStr : string[60];
  371. StaticStr,
  372. StripStr : string[40];
  373. begin
  374. if not(cs_link_extern in aktglobalswitches) then
  375. Message1(exec_i_linking,current_module.exefilename^);
  376. { Create some replacements }
  377. StaticStr:='';
  378. StripStr:='';
  379. DynLinkStr:='';
  380. { Write used files and libraries }
  381. WriteResponseFile(false);
  382. { Call linker }
  383. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  384. Replace(cmdstr,'$EXE',current_module.exefilename^);
  385. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  386. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  387. Replace(cmdstr,'$STATIC',StaticStr);
  388. Replace(cmdstr,'$STRIP',StripStr);
  389. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  390. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  391. { Remove ReponseFile }
  392. if (success) and not(cs_link_extern in aktglobalswitches) then
  393. RemoveFile(outputexedir+Info.ResName);
  394. MakeExecutable:=success; { otherwise a recursive call to link method }
  395. end;
  396. {*****************************************************************************
  397. Initialize
  398. *****************************************************************************}
  399. initialization
  400. RegisterExternalLinker(system_i386_netware_info,TLinkerNetware);
  401. RegisterImport(system_i386_netware,TImportLibNetware);
  402. RegisterExport(system_i386_netware,TExportLibNetware);
  403. RegisterTarget(system_i386_netware_info);
  404. end.
  405. {
  406. $Log$
  407. Revision 1.3 2002-11-17 16:32:04 carl
  408. * memory optimization (3-4%) : cleanup of tai fields,
  409. cleanup of tdef and tsym fields.
  410. * make it work for m68k
  411. Revision 1.2 2002/09/09 17:34:17 peter
  412. * tdicationary.replace added to replace and item in a dictionary. This
  413. is only allowed for the same name
  414. * varsyms are inserted in symtable before the types are parsed. This
  415. fixes the long standing "var longint : longint" bug
  416. - consume_idlist and idstringlist removed. The loops are inserted
  417. at the callers place and uses the symtable for duplicate id checking
  418. Revision 1.1 2002/09/06 15:03:50 carl
  419. * moved files to systems directory
  420. Revision 1.30 2002/09/03 16:26:29 daniel
  421. * Make Tprocdef.defs protected
  422. Revision 1.29 2002/08/12 15:08:44 carl
  423. + stab register indexes for powerpc (moved from gdb to cpubase)
  424. + tprocessor enumeration moved to cpuinfo
  425. + linker in target_info is now a class
  426. * many many updates for m68k (will soon start to compile)
  427. - removed some ifdef or correct them for correct cpu
  428. Revision 1.28 2002/08/11 14:32:32 peter
  429. * renamed current_library to objectlibrary
  430. Revision 1.27 2002/08/11 13:24:20 peter
  431. * saving of asmsymbols in ppu supported
  432. * asmsymbollist global is removed and moved into a new class
  433. tasmlibrarydata that will hold the info of a .a file which
  434. corresponds with a single module. Added librarydata to tmodule
  435. to keep the library info stored for the module. In the future the
  436. objectfiles will also be stored to the tasmlibrarydata class
  437. * all getlabel/newasmsymbol and friends are moved to the new class
  438. Revision 1.26 2002/07/26 21:15:46 florian
  439. * rewrote the system handling
  440. Revision 1.25 2002/07/01 18:46:35 peter
  441. * internal linker
  442. * reorganized aasm layer
  443. Revision 1.24 2002/05/18 13:34:27 peter
  444. * readded missing revisions
  445. Revision 1.23 2002/05/16 19:46:53 carl
  446. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  447. + try to fix temp allocation (still in ifdef)
  448. + generic constructor calls
  449. + start of tassembler / tmodulebase class cleanup
  450. Revision 1.21 2002/04/22 18:19:22 carl
  451. - remove use_bound_instruction field
  452. Revision 1.20 2002/04/20 21:43:18 carl
  453. * fix stack size for some targets
  454. + add offset to parameters from frame pointer info.
  455. - remove some unused stuff
  456. Revision 1.19 2002/04/19 15:46:05 peter
  457. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  458. in most cases and not written to the ppu
  459. * add mangeledname_prefix() routine to generate the prefix of
  460. manglednames depending on the current procedure, object and module
  461. * removed static procprefix since the mangledname is now build only
  462. on demand from tprocdef.mangledname
  463. Revision 1.18 2002/04/15 19:16:57 carl
  464. - remove size_of_pointer field
  465. Revision 1.17 2002/03/30 09:09:47 armin
  466. + support check-function for netware
  467. Revision 1.16 2002/03/29 17:19:51 armin
  468. + allow exports for netware
  469. Revision 1.15 2002/03/19 20:23:57 armin
  470. + smart linking now works with netware
  471. Revision 1.14 2002/03/04 17:54:59 peter
  472. * allow oridinal labels again
  473. Revision 1.13 2002/03/03 13:00:39 hajny
  474. * importprocedure fix by Armin Diehl
  475. }