t_nwm.pas 17 KB

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