t_nwm.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. Currently generating NetWare-NLM's only work under Linux and win32.
  18. (see http://home.arcor.de/armin.diehl/fpcnw for binutils working
  19. with win32) while not included in fpc-releases.
  20. The following compiler-swiches are supported for NetWare:
  21. $DESCRIPTION : NLM-Description, will be displayed at load-time
  22. $M : For Stack-Size, Heap-Size will be ignored
  23. 32K is the accepted minimum
  24. $VERSION x.x.x : Sets Major, Minor and Revision
  25. $SCREENNAME : Sets the ScreenName
  26. $THREADNAME : Sets current threadname
  27. Displaying copyright does not work with nlmconv from gnu bunutils
  28. version less that 2.13
  29. Additional parameters for the nlmvonv-inputfile can be passed with
  30. -k, i.e. -kREENTRANT will add the option REENTRANT to the nlmconv
  31. inputfile. A ; will be converted into a newline
  32. Exports will be handled like in win32:
  33. procedure bla;
  34. begin
  35. end;
  36. exports foo name 'Bar';
  37. The path to the import-Files must be specified by the library-path.
  38. All external modules are defined as autoload. (Note: the import-files have
  39. to be in unix-format for exe2nlm)
  40. By default, the most import files are included in freepascal.
  41. i.e. Procedure ConsolePrintf (p:pchar); cdecl; external 'clib.nlm';
  42. sets IMPORT @clib.imp and MODULE clib.
  43. Function simply defined as external work without generating autoload but
  44. you will get a warnung from nlmconv.
  45. If you dont have nlmconv, compile gnu-binutils with
  46. ./configure --enable-targets=i386-linux,i386-netware
  47. make all
  48. Debugging is possible with gdb and a converter from gdb to ndi available
  49. at http://home.arcor.de/armin.diehl/gdbnw
  50. A sample program:
  51. Program Hello;
  52. (*$DESCRIPTION HelloWorldNlm*)
  53. (*$VERSION 1.2.3*)
  54. (*$ScreenName Hello*)
  55. (*$M 60000,60000*)
  56. begin
  57. writeLn ('hello world');
  58. end.
  59. compile with:
  60. ppc386 -Tnetware hello
  61. ToDo:
  62. - No duplicate imports and autoloads
  63. - libc support (needs new target)
  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,symdef,
  75. import,export,link,i_nwm;
  76. type
  77. timportlibnetware=class(timportlib)
  78. procedure preparelib(const s:string);override;
  79. procedure importprocedure(aprocdef:tprocdef;const 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. NLMConvLinkFile: TLinkRes; {for second pass, fist pass is ld}
  92. Function WriteResponseFile(isdll:boolean) : Boolean;
  93. public
  94. constructor Create;override;
  95. procedure SetDefaultInfo;override;
  96. function MakeExecutable:boolean;override;
  97. end;
  98. Const tmpLinkFileName = 'link~tmp._o_';
  99. minStackSize = 32768;
  100. {*****************************************************************************
  101. TIMPORTLIBNETWARE
  102. *****************************************************************************}
  103. procedure timportlibnetware.preparelib(const s : string);
  104. begin
  105. end;
  106. procedure timportlibnetware.importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);
  107. begin
  108. { insert sharedlibrary }
  109. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  110. { do nothing with the procedure, only set the mangledname }
  111. if name<>'' then
  112. begin
  113. aprocdef.setmangledname(name);
  114. end
  115. else
  116. message(parser_e_empty_import_name);
  117. end;
  118. procedure timportlibnetware.importvariable(vs:tvarsym;const name,module:string);
  119. begin
  120. { insert sharedlibrary }
  121. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  122. { reset the mangledname and turn off the dll_var option }
  123. vs.set_mangledname(name);
  124. exclude(vs.varoptions,vo_is_dll_var);
  125. end;
  126. procedure timportlibnetware.generatelib;
  127. begin
  128. end;
  129. {*****************************************************************************
  130. TEXPORTLIBNETWARE
  131. *****************************************************************************}
  132. procedure texportlibnetware.preparelib(const s:string);
  133. begin
  134. end;
  135. procedure texportlibnetware.exportprocedure(hp : texported_item);
  136. var
  137. hp2 : texported_item;
  138. begin
  139. { first test the index value }
  140. if (hp.options and eo_index)<>0 then
  141. begin
  142. Comment(V_Error,'can''t export with index under netware');
  143. exit;
  144. end;
  145. { use pascal name is none specified }
  146. if (hp.options and eo_name)=0 then
  147. begin
  148. hp.name:=stringdup(hp.sym.name);
  149. hp.options:=hp.options or eo_name;
  150. end;
  151. { now place in correct order }
  152. hp2:=texported_item(current_module._exports.first);
  153. while assigned(hp2) and
  154. (hp.name^>hp2.name^) do
  155. hp2:=texported_item(hp2.next);
  156. { insert hp there !! }
  157. if assigned(hp2) and (hp2.name^=hp.name^) then
  158. begin
  159. { this is not allowed !! }
  160. Message1(parser_e_export_name_double,hp.name^);
  161. exit;
  162. end;
  163. if hp2=texported_item(current_module._exports.first) then
  164. current_module._exports.insert(hp)
  165. else if assigned(hp2) then
  166. begin
  167. hp.next:=hp2;
  168. hp.previous:=hp2.previous;
  169. if assigned(hp2.previous) then
  170. hp2.previous.next:=hp;
  171. hp2.previous:=hp;
  172. end
  173. else
  174. current_module._exports.concat(hp);
  175. end;
  176. procedure texportlibnetware.exportvar(hp : texported_item);
  177. begin
  178. hp.is_var:=true;
  179. exportprocedure(hp);
  180. end;
  181. procedure texportlibnetware.generatelib;
  182. var
  183. hp2 : texported_item;
  184. begin
  185. hp2:=texported_item(current_module._exports.first);
  186. while assigned(hp2) do
  187. begin
  188. if (not hp2.is_var) and
  189. (hp2.sym.typ=procsym) then
  190. begin
  191. { the manglednames can already be the same when the procedure
  192. is declared with cdecl }
  193. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  194. begin
  195. {$ifdef i386}
  196. { place jump in codesegment }
  197. codesegment.concat(Tai_align.Create_op(4,$90));
  198. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  199. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,objectlibrary.newasmsymbol(tprocsym(hp2.sym).first_procdef.mangledname,AB_EXTERNAL,AT_FUNCTION)));
  200. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  201. {$endif i386}
  202. end;
  203. end
  204. else
  205. //Comment(V_Error,'Exporting of variables is not supported under netware');
  206. Message1(parser_e_no_export_of_variables_for_target,'netware');
  207. hp2:=texported_item(hp2.next);
  208. end;
  209. end;
  210. {*****************************************************************************
  211. TLINKERNETWARE
  212. *****************************************************************************}
  213. Constructor TLinkerNetware.Create;
  214. begin
  215. Inherited Create;
  216. end;
  217. procedure TLinkerNetware.SetDefaultInfo;
  218. begin
  219. with Info do
  220. begin
  221. ExeCmd[1]:= 'ld -Ur -T $RES $STRIP -o $TMPOBJ';
  222. if source_info.system<>target_info.system Then
  223. ExeCmd[2]:='nlmconv -m i386nw -T$RES'
  224. else
  225. ExeCmd[2]:='nlmconv -T$RES';
  226. end;
  227. end;
  228. Function TLinkerNetware.WriteResponseFile(isdll:boolean) : Boolean;
  229. Var
  230. linkres : TLinkRes;
  231. i : longint;
  232. s,s2,s3 : string;
  233. ProgNam : string [80];
  234. NlmNam : string [80];
  235. hp2 : texported_item; { for exports }
  236. p : byte;
  237. begin
  238. WriteResponseFile:=False;
  239. ProgNam := current_module.exefilename^;
  240. i:=Pos(target_info.exeext,ProgNam);
  241. if i>0 then
  242. Delete(ProgNam,i,255);
  243. NlmNam := ProgNam + target_info.exeext;
  244. { Open link.res file }
  245. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName); {for ld}
  246. NLMConvLinkFile:=TLinkRes.Create(outputexedir+'n'+Info.ResName); {for nlmconv, written in CreateExeFile}
  247. p := Pos ('"', Description);
  248. while (p > 0) do
  249. begin
  250. delete (Description,p,1);
  251. p := Pos ('"', Description);
  252. end;
  253. if Description <> '' then
  254. NLMConvLinkFile.Add('DESCRIPTION "' + Description + '"');
  255. NLMConvLinkFile.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  256. p := Pos ('"', nwscreenname);
  257. while (p > 0) do
  258. begin
  259. delete (nwscreenname,p,1);
  260. p := Pos ('"', nwscreenname);
  261. end;
  262. p := Pos ('"', nwthreadname);
  263. while (p > 0) do
  264. begin
  265. delete (nwthreadname,p,1);
  266. p := Pos ('"', nwthreadname);
  267. end;
  268. p := Pos ('"', nwcopyright);
  269. while (p > 0) do
  270. begin
  271. delete (nwcopyright,p,1);
  272. p := Pos ('"', nwcopyright);
  273. end;
  274. if nwscreenname <> '' then
  275. NLMConvLinkFile.Add('SCREENNAME "' + nwscreenname + '"');
  276. if nwthreadname <> '' then
  277. NLMConvLinkFile.Add('THREADNAME "' + nwthreadname + '"');
  278. if nwcopyright <> '' then
  279. NLMConvLinkFile.Add('COPYRIGHT "' + nwcopyright + '"');
  280. if stacksize < minStackSize then stacksize := minStackSize;
  281. str (stacksize, s);
  282. NLMConvLinkFile.Add ('STACKSIZE '+s);
  283. NLMConvLinkFile.Add ('INPUT '+tmpLinkFileName);
  284. { add objectfiles, start with nwpre always }
  285. LinkRes.Add ('INPUT (');
  286. s2 := FindObjectFile('nwpre','',false);
  287. Comment (V_Debug,'adding Object File '+s2);
  288. LinkRes.Add (s2);
  289. { main objectfiles, add to linker input }
  290. while not ObjectFiles.Empty do
  291. begin
  292. s:=ObjectFiles.GetFirst;
  293. if s<>'' then
  294. begin
  295. s2 := FindObjectFile (s,'',false);
  296. Comment (V_Debug,'adding Object File '+s2);
  297. LinkRes.Add (s2);
  298. end;
  299. end;
  300. { output file (nlm), add to nlmconv }
  301. NLMConvLinkFile.Add ('OUTPUT ' + NlmNam);
  302. { start and stop-procedures }
  303. NLMConvLinkFile.Add ('START _Prelude'); { defined in rtl/netware/nwpre.as }
  304. NLMConvLinkFile.Add ('EXIT _Stop'); { nwpre.as }
  305. NLMConvLinkFile.Add ('CHECK FPC_NW_CHECKFUNCTION'); { system.pp }
  306. if not (cs_link_strip in aktglobalswitches) then
  307. begin
  308. NLMConvLinkFile.Add ('DEBUG');
  309. Comment(V_Debug,'DEBUG');
  310. end;
  311. { Write staticlibraries, is that correct ? }
  312. if not StaticLibFiles.Empty then
  313. begin
  314. While not StaticLibFiles.Empty do
  315. begin
  316. S:=lower (StaticLibFiles.GetFirst);
  317. if s<>'' then
  318. begin
  319. {ad: that's a hack !
  320. whith -XX we get the .a files as static libs (in addition to the
  321. imported libraries}
  322. if (pos ('.a',s) <> 0) OR (pos ('.A', s) <> 0) then
  323. begin
  324. S2 := FindObjectFile(s,'',false);
  325. LinkRes.Add (S2);
  326. Comment(V_Debug,'adding Object File (StaticLibFiles) '+S2);
  327. end else
  328. begin
  329. i:=Pos(target_info.staticlibext,S);
  330. if i>0 then
  331. Delete(S,i,255);
  332. S := S + '.imp'; S2 := '';
  333. librarysearchpath.FindFile(S,S2);
  334. NLMConvLinkFile.Add('IMPORT @'+S2);
  335. Comment(V_Debug,'IMPORT @'+s2);
  336. end;
  337. end
  338. end;
  339. end;
  340. if not SharedLibFiles.Empty then
  341. begin
  342. While not SharedLibFiles.Empty do
  343. begin
  344. {becuase of upper/lower case mix, we may get duplicate
  345. names but nlmconv ignores that.
  346. Here we are setting the import-files for nlmconv. I.e. for
  347. the module clib or clib.nlm we add IMPORT @clib.imp and also
  348. the module clib.nlm (autoload)
  349. ? may it be better to set autoload's via StaticLibFiles ? }
  350. S:=lower (SharedLibFiles.GetFirst);
  351. if s<>'' then
  352. begin
  353. s2:=s;
  354. i:=Pos(target_info.sharedlibext,S);
  355. if i>0 then
  356. Delete(S,i,255);
  357. S := S + '.imp';
  358. librarysearchpath.FindFile(S,S3);
  359. NLMConvLinkFile.Add('IMPORT @'+S3);
  360. NLMConvLinkFile.Add('MODULE '+s2);
  361. Comment(V_Debug,'MODULE '+S2);
  362. Comment(V_Debug,'IMPORT @'+S3);
  363. end
  364. end;
  365. end;
  366. { write exports }
  367. hp2:=texported_item(current_module._exports.first);
  368. while assigned(hp2) do
  369. begin
  370. if not hp2.is_var then
  371. begin
  372. { Export the Symbol }
  373. Comment(V_Debug,'EXPORT '+hp2.name^);
  374. NLMConvLinkFile.Add ('EXPORT '+hp2.name^);
  375. end
  376. else
  377. { really, i think it is possible }
  378. Comment(V_Error,'Exporting of variables is not supported under netware');
  379. hp2:=texported_item(hp2.next);
  380. end;
  381. { Write and Close response for ld, response for nlmconv is in NLMConvLinkFile(not written) }
  382. linkres.Add (')');
  383. linkres.writetodisk;
  384. LinkRes.Free;
  385. { pass options from -k to nlmconv, ; is interpreted as newline }
  386. s := ParaLinkOptions;
  387. while(Length(s) > 0) and (s[1] = ' ') do
  388. delete (s,1,1);
  389. p := pos ('"',s);
  390. while p > 0 do
  391. begin
  392. delete (s,p,1);
  393. p := pos ('"',s);
  394. end;
  395. p := pos (';',s);
  396. while p > 0 do
  397. begin
  398. s2 := copy(s,1,p-1);
  399. comment (V_Debug,'adding "'+s2+'" to nlmvonv input');
  400. NLMConvLinkFile.Add(s2);
  401. delete (s,1,p);
  402. p := pos (';',s);
  403. end;
  404. if s <> '' then
  405. begin
  406. comment (V_Debug,'adding "'+s+'" to nlmvonv input');
  407. NLMConvLinkFile.Add(s);
  408. end;
  409. WriteResponseFile:=True;
  410. end;
  411. function TLinkerNetware.MakeExecutable:boolean;
  412. var
  413. binstr,
  414. cmdstr : string;
  415. success : boolean;
  416. StripStr : string[2];
  417. begin
  418. if not(cs_link_extern in aktglobalswitches) then
  419. Message1(exec_i_linking,current_module.exefilename^);
  420. { Create some replacements }
  421. StripStr:='';
  422. if (cs_link_strip in aktglobalswitches) then
  423. StripStr:='-s';
  424. { Write used files and libraries and create Headerfile for
  425. nlmconv in NLMConvLinkFile }
  426. WriteResponseFile(false);
  427. { Call linker, this will generate a new object file that will be passed
  428. to nlmconv. Otherwise we could not create nlms without debug info }
  429. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  430. Replace(cmdstr,'$EXE',current_module.exefilename^);
  431. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  432. Replace(cmdstr,'$STRIP',StripStr);
  433. Replace(cmdstr,'$TMPOBJ',outputexedir+tmpLinkFileName);
  434. Comment (v_debug,'Executing '+BinStr+' '+cmdstr);
  435. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  436. { Remove ReponseFile }
  437. if (success) and not(cs_link_extern in aktglobalswitches) then
  438. RemoveFile(outputexedir+Info.ResName);
  439. { Call nlmconv }
  440. if success then
  441. begin
  442. NLMConvLinkFile.writetodisk;
  443. NLMConvLinkFile.Free;
  444. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  445. Replace(cmdstr,'$RES',outputexedir+'n'+Info.ResName);
  446. Comment (v_debug,'Executing '+BinStr+' '+cmdstr);
  447. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  448. if (success) and not(cs_link_extern in aktglobalswitches) then
  449. RemoveFile(outputexedir+'n'+Info.ResName);
  450. {always remove the temp object file}
  451. RemoveFile(outputexedir+tmpLinkFileName);
  452. end;
  453. MakeExecutable:=success; { otherwise a recursive call to link method }
  454. end;
  455. {*****************************************************************************
  456. Initialize
  457. *****************************************************************************}
  458. initialization
  459. RegisterExternalLinker(system_i386_netware_info,TLinkerNetware);
  460. RegisterImport(system_i386_netware,TImportLibNetware);
  461. RegisterExport(system_i386_netware,TExportLibNetware);
  462. RegisterTarget(system_i386_netware_info);
  463. end.
  464. {
  465. $Log$
  466. Revision 1.11 2004-06-20 08:55:32 florian
  467. * logs truncated
  468. Revision 1.10 2004/03/02 00:36:33 olle
  469. * big transformation of Tai_[const_]Symbol.Create[data]name*
  470. }