t_nwm.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. SysUtils,
  71. cutils,cfileutils,
  72. verbose,systems,globtype,globals,
  73. symconst,script,
  74. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,symsym,symdef,
  75. import,export,link,i_nwm,ogbase
  76. {$ifdef netware} ,dos {$endif}
  77. ;
  78. type
  79. timportlibnetware=class(timportlib)
  80. procedure generatelib;override;
  81. end;
  82. texportlibnetware=class(texportlib)
  83. procedure preparelib(const s : string);override;
  84. procedure exportprocedure(hp : texported_item);override;
  85. procedure exportvar(hp : texported_item);override;
  86. procedure generatelib;override;
  87. end;
  88. tlinkernetware=class(texternallinker)
  89. private
  90. NLMConvLinkFile: TLinkRes; {for second pass, fist pass is ld}
  91. Function WriteResponseFile(isdll:boolean) : Boolean;
  92. public
  93. constructor Create;override;
  94. procedure SetDefaultInfo;override;
  95. function MakeExecutable:boolean;override;
  96. end;
  97. Const tmpLinkFileName = 'link~tmp._o_';
  98. minStackSize = 32768;
  99. {*****************************************************************************
  100. TIMPORTLIBNETWARE
  101. *****************************************************************************}
  102. procedure timportlibnetware.generatelib;
  103. var
  104. i : longint;
  105. ImportLibrary : TImportLibrary;
  106. begin
  107. for i:=0 to current_module.ImportLibraryList.Count-1 do
  108. begin
  109. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  110. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  111. end;
  112. end;
  113. {*****************************************************************************
  114. TEXPORTLIBNETWARE
  115. *****************************************************************************}
  116. procedure texportlibnetware.preparelib(const s:string);
  117. begin
  118. end;
  119. procedure texportlibnetware.exportprocedure(hp : texported_item);
  120. var
  121. hp2 : texported_item;
  122. begin
  123. { first test the index value }
  124. if (hp.options and eo_index)<>0 then
  125. begin
  126. Comment(V_Error,'can''t export with index under netware');
  127. exit;
  128. end;
  129. { use pascal name is none specified }
  130. if (hp.options and eo_name)=0 then
  131. begin
  132. hp.name:=stringdup(hp.sym.name);
  133. hp.options:=hp.options or eo_name;
  134. end;
  135. { now place in correct order }
  136. hp2:=texported_item(current_module._exports.first);
  137. while assigned(hp2) and
  138. (hp.name^>hp2.name^) do
  139. hp2:=texported_item(hp2.next);
  140. { insert hp there !! }
  141. if assigned(hp2) and (hp2.name^=hp.name^) then
  142. begin
  143. { this is not allowed !! }
  144. Message1(parser_e_export_name_double,hp.name^);
  145. exit;
  146. end;
  147. if hp2=texported_item(current_module._exports.first) then
  148. current_module._exports.insert(hp)
  149. else if assigned(hp2) then
  150. begin
  151. hp.next:=hp2;
  152. hp.previous:=hp2.previous;
  153. if assigned(hp2.previous) then
  154. hp2.previous.next:=hp;
  155. hp2.previous:=hp;
  156. end
  157. else
  158. current_module._exports.concat(hp);
  159. end;
  160. procedure texportlibnetware.exportvar(hp : texported_item);
  161. begin
  162. hp.is_var:=true;
  163. exportprocedure(hp);
  164. end;
  165. procedure texportlibnetware.generatelib;
  166. var
  167. hp2 : texported_item;
  168. begin
  169. hp2:=texported_item(current_module._exports.first);
  170. while assigned(hp2) do
  171. begin
  172. if (not hp2.is_var) and
  173. (hp2.sym.typ=procsym) then
  174. begin
  175. { the manglednames can already be the same when the procedure
  176. is declared with cdecl }
  177. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  178. begin
  179. {$ifdef i386}
  180. { place jump in al_procedures }
  181. current_asmdata.asmlists[al_procedures].concat(Tai_align.Create_op(4,$90));
  182. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  183. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(tprocsym(hp2.sym).first_procdef.mangledname)));
  184. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  185. {$endif i386}
  186. end;
  187. end
  188. else
  189. //Comment(V_Error,'Exporting of variables is not supported under netware');
  190. Message1(parser_e_no_export_of_variables_for_target,'netware');
  191. hp2:=texported_item(hp2.next);
  192. end;
  193. end;
  194. {*****************************************************************************
  195. TLINKERNETWARE
  196. *****************************************************************************}
  197. Constructor TLinkerNetware.Create;
  198. begin
  199. Inherited Create;
  200. end;
  201. procedure TLinkerNetware.SetDefaultInfo;
  202. begin
  203. with Info do
  204. begin
  205. {$ifndef netware}
  206. ExeCmd[1]:= FindUtil(utilsprefix+'ld') + ' -Ur -T $RES $STRIP -o $TMPOBJ';
  207. ExeCmd[2]:= FindUtil(utilsprefix+'nlmconv') + ' -T$RES';
  208. {$else}
  209. {for running on netware we need absolute pathes since ld has another working directory}
  210. ExeCmd[1]:= FindUtil(utilsprefix+'ld') + ' -Ur -T '+FExpand(outputexedir+Info.ResName)+' $STRIP -o '+Fexpand(outputexedir+tmpLinkFileName);
  211. ExeCmd[2]:= FindUtil(utilsprefix+'nlmconv') + ' -T'+FExpand(outputexedir+'n'+Info.ResName);
  212. {$endif}
  213. end;
  214. end;
  215. Function TLinkerNetware.WriteResponseFile(isdll:boolean) : Boolean;
  216. Var
  217. linkres : TLinkRes;
  218. i : longint;
  219. s,s2,s3 : string;
  220. ProgNam : string [80];
  221. NlmNam : string [80];
  222. hp2 : texported_item; { for exports }
  223. p : byte;
  224. begin
  225. WriteResponseFile:=False;
  226. ProgNam := current_module.exefilename^;
  227. i:=Pos(target_info.exeext,ProgNam);
  228. if i>0 then
  229. Delete(ProgNam,i,255);
  230. NlmNam := ProgNam + target_info.exeext;
  231. { Open link.res file }
  232. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName); {for ld}
  233. NLMConvLinkFile:=TLinkRes.Create(outputexedir+'n'+Info.ResName); {for nlmconv, written in CreateExeFile}
  234. p := Pos ('"', Description);
  235. while (p > 0) do
  236. begin
  237. delete (Description,p,1);
  238. p := Pos ('"', Description);
  239. end;
  240. if Description <> '' then
  241. NLMConvLinkFile.Add('DESCRIPTION "' + Description + '"');
  242. NLMConvLinkFile.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  243. p := Pos ('"', nwscreenname);
  244. while (p > 0) do
  245. begin
  246. delete (nwscreenname,p,1);
  247. p := Pos ('"', nwscreenname);
  248. end;
  249. p := Pos ('"', nwthreadname);
  250. while (p > 0) do
  251. begin
  252. delete (nwthreadname,p,1);
  253. p := Pos ('"', nwthreadname);
  254. end;
  255. p := Pos ('"', nwcopyright);
  256. while (p > 0) do
  257. begin
  258. delete (nwcopyright,p,1);
  259. p := Pos ('"', nwcopyright);
  260. end;
  261. if nwscreenname <> '' then
  262. NLMConvLinkFile.Add('SCREENNAME "' + nwscreenname + '"');
  263. if nwthreadname <> '' then
  264. NLMConvLinkFile.Add('THREADNAME "' + nwthreadname + '"');
  265. if nwcopyright <> '' then
  266. NLMConvLinkFile.Add('COPYRIGHT "' + nwcopyright + '"');
  267. if stacksize < minStackSize then stacksize := minStackSize;
  268. str (stacksize, s);
  269. NLMConvLinkFile.Add ('STACKSIZE '+s);
  270. {$ifndef netware}
  271. NLMConvLinkFile.Add ('INPUT '+outputexedir+tmpLinkFileName);
  272. {$else}
  273. NLMConvLinkFile.Add ('INPUT '+FExpand(outputexedir+tmpLinkFileName));
  274. {$endif}
  275. { add objectfiles, start with nwpre always }
  276. LinkRes.Add ('INPUT(');
  277. s2 := FindObjectFile('nwpre','',false);
  278. Comment (V_Debug,'adding Object File '+s2);
  279. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  280. { main objectfiles, add to linker input }
  281. while not ObjectFiles.Empty do
  282. begin
  283. s:=ObjectFiles.GetFirst;
  284. if s<>'' then
  285. begin
  286. s2 := FindObjectFile (s,'',false);
  287. Comment (V_Debug,'adding Object File '+s2);
  288. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  289. end;
  290. end;
  291. LinkRes.Add (')');
  292. { output file (nlm), add to nlmconv }
  293. {$ifndef netware}
  294. NLMConvLinkFile.Add ('OUTPUT ' + NlmNam);
  295. {$else}
  296. NLMConvLinkFile.Add ('OUTPUT ' + FExpand(NlmNam));
  297. {$endif}
  298. { start and stop-procedures }
  299. NLMConvLinkFile.Add ('START _Prelude'); { defined in rtl/netware/nwpre.as }
  300. NLMConvLinkFile.Add ('EXIT _Stop'); { nwpre.as }
  301. NLMConvLinkFile.Add ('CHECK FPC_NW_CHECKFUNCTION'); { system.pp }
  302. if not (cs_link_strip in current_settings.globalswitches) then
  303. begin
  304. NLMConvLinkFile.Add ('DEBUG');
  305. Comment(V_Debug,'DEBUG');
  306. end;
  307. { Write staticlibraries }
  308. if not StaticLibFiles.Empty then
  309. begin
  310. LinkRes.Add ('GROUP(');
  311. While not StaticLibFiles.Empty do
  312. begin
  313. S:=lower (StaticLibFiles.GetFirst);
  314. if s<>'' then
  315. begin
  316. {ad: that's a hack !
  317. whith -XX we get the .a files as static libs (in addition to the
  318. imported libraries}
  319. if (pos ('.a',s) <> 0) OR (pos ('.A', s) <> 0) then
  320. begin
  321. S2 := FindObjectFile(s,'',false);
  322. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  323. Comment(V_Debug,'adding Object File (StaticLibFiles) '+S2);
  324. end else
  325. begin
  326. i:=Pos(target_info.staticlibext,S);
  327. if i>0 then
  328. Delete(S,i,255);
  329. S := S + '.imp'; S2 := '';
  330. librarysearchpath.FindFile(S,S2);
  331. {$ifdef netware}
  332. Comment(V_Debug,'IMPORT @'+s2);
  333. s2 := FExpand (S2);
  334. {$endif}
  335. NLMConvLinkFile.Add('IMPORT @'+S2);
  336. Comment(V_Debug,'IMPORT @'+s2);
  337. end;
  338. end
  339. end;
  340. LinkRes.Add (')');
  341. end;
  342. if not SharedLibFiles.Empty then
  343. begin
  344. While not SharedLibFiles.Empty do
  345. begin
  346. {becuase of upper/lower case mix, we may get duplicate
  347. names but nlmconv ignores that.
  348. Here we are setting the import-files for nlmconv. I.e. for
  349. the module clib or clib.nlm we add IMPORT @clib.imp and also
  350. the module clib.nlm (autoload)
  351. ? may it be better to set autoload's via StaticLibFiles ? }
  352. S:=lower (SharedLibFiles.GetFirst);
  353. if s<>'' then
  354. begin
  355. s2:=s;
  356. i:=Pos(target_info.sharedlibext,S);
  357. if i>0 then
  358. Delete(S,i,255);
  359. if s[1] = '!' then
  360. begin // special, with ! only the imp will be included but no module is autoloaded, needed i.e. for netware.imp
  361. S := copy(S,2,255) + '.imp';
  362. librarysearchpath.FindFile(S,S3);
  363. {$ifdef netware}
  364. Comment(V_Debug,'IMPORT @'+S3);
  365. S3 := FExpand (S3);
  366. {$endif}
  367. NLMConvLinkFile.Add('IMPORT @'+S3);
  368. Comment(V_Debug,'IMPORT @'+S3);
  369. end else
  370. begin
  371. S := S + '.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. NLMConvLinkFile.Add('MODULE '+s2);
  379. Comment(V_Debug,'MODULE '+S2);
  380. Comment(V_Debug,'IMPORT @'+S3);
  381. end;
  382. end;
  383. end;
  384. end;
  385. { write exports }
  386. hp2:=texported_item(current_module._exports.first);
  387. while assigned(hp2) do
  388. begin
  389. if not hp2.is_var then
  390. begin
  391. { Export the Symbol }
  392. Comment(V_Debug,'EXPORT '+hp2.name^);
  393. NLMConvLinkFile.Add ('EXPORT '+hp2.name^);
  394. end
  395. else
  396. { really, i think it is possible }
  397. Message1(parser_e_no_export_of_variables_for_target,'netware');
  398. hp2:=texported_item(hp2.next);
  399. end;
  400. { Write and Close response for ld, response for nlmconv is in NLMConvLinkFile(not written) }
  401. linkres.writetodisk;
  402. LinkRes.Free;
  403. { pass options from -k to nlmconv, ; is interpreted as newline }
  404. s := ParaLinkOptions;
  405. while(Length(s) > 0) and (s[1] = ' ') do
  406. delete (s,1,1);
  407. p := pos ('"',s);
  408. while p > 0 do
  409. begin
  410. delete (s,p,1);
  411. p := pos ('"',s);
  412. end;
  413. p := pos (';',s);
  414. while p > 0 do
  415. begin
  416. s2 := copy(s,1,p-1);
  417. comment (V_Debug,'adding "'+s2+'" to nlmvonv input');
  418. NLMConvLinkFile.Add(s2);
  419. delete (s,1,p);
  420. p := pos (';',s);
  421. end;
  422. if s <> '' then
  423. begin
  424. comment (V_Debug,'adding "'+s+'" to nlmvonv input');
  425. NLMConvLinkFile.Add(s);
  426. end;
  427. WriteResponseFile:=True;
  428. end;
  429. function TLinkerNetware.MakeExecutable:boolean;
  430. var
  431. binstr : String;
  432. cmdstr : TCmdStr;
  433. success : boolean;
  434. StripStr : string[2];
  435. begin
  436. if not(cs_link_nolink in current_settings.globalswitches) then
  437. Message1(exec_i_linking,current_module.exefilename^);
  438. { Create some replacements }
  439. StripStr:='';
  440. if (cs_link_strip in current_settings.globalswitches) then
  441. StripStr:='-s';
  442. { Write used files and libraries and create Headerfile for
  443. nlmconv in NLMConvLinkFile }
  444. WriteResponseFile(false);
  445. { Call linker, this will generate a new object file that will be passed
  446. to nlmconv. Otherwise we could not create nlms without debug info }
  447. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  448. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  449. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  450. Replace(cmdstr,'$STRIP',StripStr);
  451. Replace(cmdstr,'$TMPOBJ',maybequoted(outputexedir+tmpLinkFileName));
  452. Comment (v_debug,'Executing '+BinStr+' '+cmdstr);
  453. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  454. { Remove ReponseFile }
  455. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  456. DeleteFile(outputexedir+Info.ResName);
  457. { Call nlmconv }
  458. if success then
  459. begin
  460. NLMConvLinkFile.writetodisk;
  461. NLMConvLinkFile.Free;
  462. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  463. Replace(cmdstr,'$RES',maybequoted(outputexedir+'n'+Info.ResName));
  464. Comment (v_debug,'Executing '+BinStr+' '+cmdstr);
  465. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  466. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  467. begin
  468. DeleteFile(outputexedir+'n'+Info.ResName);
  469. DeleteFile(outputexedir+tmpLinkFileName);
  470. end;
  471. end;
  472. MakeExecutable:=success; { otherwise a recursive call to link method }
  473. end;
  474. {*****************************************************************************
  475. Initialize
  476. *****************************************************************************}
  477. initialization
  478. RegisterExternalLinker(system_i386_netware_info,TLinkerNetware);
  479. RegisterImport(system_i386_netware,TImportLibNetware);
  480. RegisterExport(system_i386_netware,TExportLibNetware);
  481. RegisterTarget(system_i386_netware_info);
  482. end.