t_nwm.pas 18 KB

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