t_nwm.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. pd : tprocdef;
  169. begin
  170. hp2:=texported_item(current_module._exports.first);
  171. while assigned(hp2) do
  172. begin
  173. if (not hp2.is_var) and
  174. (hp2.sym.typ=procsym) then
  175. begin
  176. { the manglednames can already be the same when the procedure
  177. is declared with cdecl }
  178. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  179. if pd.mangledname<>hp2.name^ then
  180. begin
  181. {$ifdef i386}
  182. { place jump in al_procedures }
  183. current_asmdata.asmlists[al_procedures].concat(Tai_align.Create_op(4,$90));
  184. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  185. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(pd.mangledname)));
  186. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  187. {$endif i386}
  188. end;
  189. end
  190. else
  191. //Comment(V_Error,'Exporting of variables is not supported under netware');
  192. Message1(parser_e_no_export_of_variables_for_target,'netware');
  193. hp2:=texported_item(hp2.next);
  194. end;
  195. end;
  196. {*****************************************************************************
  197. TLINKERNETWARE
  198. *****************************************************************************}
  199. Constructor TLinkerNetware.Create;
  200. begin
  201. Inherited Create;
  202. end;
  203. procedure TLinkerNetware.SetDefaultInfo;
  204. begin
  205. with Info do
  206. begin
  207. {$ifndef netware}
  208. ExeCmd[1]:= FindUtil(utilsprefix+'ld') + ' -Ur -T $RES $STRIP -o $TMPOBJ';
  209. ExeCmd[2]:= FindUtil(utilsprefix+'nlmconv') + ' -T$RES';
  210. {$else}
  211. {for running on netware we need absolute pathes since ld has another working directory}
  212. ExeCmd[1]:= FindUtil(utilsprefix+'ld') + ' -Ur -T '+FExpand(outputexedir+Info.ResName)+' $STRIP -o '+Fexpand(outputexedir+tmpLinkFileName);
  213. ExeCmd[2]:= FindUtil(utilsprefix+'nlmconv') + ' -T'+FExpand(outputexedir+'n'+Info.ResName);
  214. {$endif}
  215. end;
  216. end;
  217. Function TLinkerNetware.WriteResponseFile(isdll:boolean) : Boolean;
  218. Var
  219. linkres : TLinkRes;
  220. i : longint;
  221. s,s2,s3 : TCmdStr;
  222. ProgNam : string [80];
  223. NlmNam : string [80];
  224. hp2 : texported_item; { for exports }
  225. p : byte;
  226. begin
  227. WriteResponseFile:=False;
  228. ProgNam := current_module.exefilename^;
  229. i:=Pos(target_info.exeext,ProgNam);
  230. if i>0 then
  231. Delete(ProgNam,i,255);
  232. NlmNam := ProgNam + target_info.exeext;
  233. { Open link.res file }
  234. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName); {for ld}
  235. NLMConvLinkFile:=TLinkRes.Create(outputexedir+'n'+Info.ResName); {for nlmconv, written in CreateExeFile}
  236. p := Pos ('"', Description);
  237. while (p > 0) do
  238. begin
  239. delete (Description,p,1);
  240. p := Pos ('"', Description);
  241. end;
  242. if Description <> '' then
  243. NLMConvLinkFile.Add('DESCRIPTION "' + Description + '"');
  244. NLMConvLinkFile.Add('VERSION '+tostr(dllmajor)+','+tostr(dllminor)+','+tostr(dllrevision));
  245. p := Pos ('"', nwscreenname);
  246. while (p > 0) do
  247. begin
  248. delete (nwscreenname,p,1);
  249. p := Pos ('"', nwscreenname);
  250. end;
  251. p := Pos ('"', nwthreadname);
  252. while (p > 0) do
  253. begin
  254. delete (nwthreadname,p,1);
  255. p := Pos ('"', nwthreadname);
  256. end;
  257. p := Pos ('"', nwcopyright);
  258. while (p > 0) do
  259. begin
  260. delete (nwcopyright,p,1);
  261. p := Pos ('"', nwcopyright);
  262. end;
  263. if nwscreenname <> '' then
  264. NLMConvLinkFile.Add('SCREENNAME "' + nwscreenname + '"');
  265. if nwthreadname <> '' then
  266. NLMConvLinkFile.Add('THREADNAME "' + nwthreadname + '"');
  267. if nwcopyright <> '' then
  268. NLMConvLinkFile.Add('COPYRIGHT "' + nwcopyright + '"');
  269. if stacksize < minStackSize then stacksize := minStackSize;
  270. str (stacksize, s);
  271. NLMConvLinkFile.Add ('STACKSIZE '+s);
  272. {$ifndef netware}
  273. NLMConvLinkFile.Add ('INPUT '+outputexedir+tmpLinkFileName);
  274. {$else}
  275. NLMConvLinkFile.Add ('INPUT '+FExpand(outputexedir+tmpLinkFileName));
  276. {$endif}
  277. { add objectfiles, start with nwpre always }
  278. LinkRes.Add ('INPUT(');
  279. s2 := FindObjectFile('nwpre','',false);
  280. Comment (V_Debug,'adding Object File '+s2);
  281. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  282. { main objectfiles, add to linker input }
  283. while not ObjectFiles.Empty do
  284. begin
  285. s:=ObjectFiles.GetFirst;
  286. if s<>'' then
  287. begin
  288. s2 := FindObjectFile (s,'',false);
  289. Comment (V_Debug,'adding Object File '+s2);
  290. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  291. end;
  292. end;
  293. LinkRes.Add (')');
  294. { output file (nlm), add to nlmconv }
  295. {$ifndef netware}
  296. NLMConvLinkFile.Add ('OUTPUT ' + NlmNam);
  297. {$else}
  298. NLMConvLinkFile.Add ('OUTPUT ' + FExpand(NlmNam));
  299. {$endif}
  300. { start and stop-procedures }
  301. NLMConvLinkFile.Add ('START _Prelude'); { defined in rtl/netware/nwpre.as }
  302. NLMConvLinkFile.Add ('EXIT _Stop'); { nwpre.as }
  303. NLMConvLinkFile.Add ('CHECK FPC_NW_CHECKFUNCTION'); { system.pp }
  304. if not (cs_link_strip in current_settings.globalswitches) then
  305. begin
  306. NLMConvLinkFile.Add ('DEBUG');
  307. Comment(V_Debug,'DEBUG');
  308. end;
  309. { Write staticlibraries }
  310. if not StaticLibFiles.Empty then
  311. begin
  312. LinkRes.Add ('GROUP(');
  313. While not StaticLibFiles.Empty do
  314. begin
  315. S:=lower (StaticLibFiles.GetFirst);
  316. if s<>'' then
  317. begin
  318. {ad: that's a hack !
  319. whith -XX we get the .a files as static libs (in addition to the
  320. imported libraries}
  321. if (pos ('.a',s) <> 0) OR (pos ('.A', s) <> 0) then
  322. begin
  323. S2 := FindObjectFile(s,'',false);
  324. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  325. Comment(V_Debug,'adding Object File (StaticLibFiles) '+S2);
  326. end else
  327. begin
  328. i:=Pos(target_info.staticlibext,S);
  329. if i>0 then
  330. Delete(S,i,255);
  331. S := S + '.imp'; S2 := '';
  332. librarysearchpath.FindFile(S,false,S2);
  333. {$ifdef netware}
  334. Comment(V_Debug,'IMPORT @'+s2);
  335. s2 := FExpand (S2);
  336. {$endif}
  337. NLMConvLinkFile.Add('IMPORT @'+S2);
  338. Comment(V_Debug,'IMPORT @'+s2);
  339. end;
  340. end
  341. end;
  342. LinkRes.Add (')');
  343. end;
  344. if not SharedLibFiles.Empty then
  345. begin
  346. While not SharedLibFiles.Empty do
  347. begin
  348. {becuase of upper/lower case mix, we may get duplicate
  349. names but nlmconv ignores that.
  350. Here we are setting the import-files for nlmconv. I.e. for
  351. the module clib or clib.nlm we add IMPORT @clib.imp and also
  352. the module clib.nlm (autoload)
  353. ? may it be better to set autoload's via StaticLibFiles ? }
  354. S:=lower (SharedLibFiles.GetFirst);
  355. if s<>'' then
  356. begin
  357. s2:=s;
  358. i:=Pos(target_info.sharedlibext,S);
  359. if i>0 then
  360. Delete(S,i,255);
  361. if s[1] = '!' then
  362. begin // special, with ! only the imp will be included but no module is autoloaded, needed i.e. for netware.imp
  363. S := copy(S,2,255) + '.imp';
  364. librarysearchpath.FindFile(S,false,S3);
  365. {$ifdef netware}
  366. Comment(V_Debug,'IMPORT @'+S3);
  367. S3 := FExpand (S3);
  368. {$endif}
  369. NLMConvLinkFile.Add('IMPORT @'+S3);
  370. Comment(V_Debug,'IMPORT @'+S3);
  371. end else
  372. begin
  373. S := S + '.imp';
  374. librarysearchpath.FindFile(S,false,S3);
  375. {$ifdef netware}
  376. Comment(V_Debug,'IMPORT @'+S3);
  377. S3 := FExpand (S3);
  378. {$endif}
  379. NLMConvLinkFile.Add('IMPORT @'+S3);
  380. NLMConvLinkFile.Add('MODULE '+s2);
  381. Comment(V_Debug,'MODULE '+S2);
  382. Comment(V_Debug,'IMPORT @'+S3);
  383. end;
  384. end;
  385. end;
  386. end;
  387. { write exports }
  388. hp2:=texported_item(current_module._exports.first);
  389. while assigned(hp2) do
  390. begin
  391. if not hp2.is_var then
  392. begin
  393. { Export the Symbol }
  394. Comment(V_Debug,'EXPORT '+hp2.name^);
  395. NLMConvLinkFile.Add ('EXPORT '+hp2.name^);
  396. end
  397. else
  398. { really, i think it is possible }
  399. Message1(parser_e_no_export_of_variables_for_target,'netware');
  400. hp2:=texported_item(hp2.next);
  401. end;
  402. { Write and Close response for ld, response for nlmconv is in NLMConvLinkFile(not written) }
  403. linkres.writetodisk;
  404. LinkRes.Free;
  405. { pass options from -k to nlmconv, ; is interpreted as newline }
  406. s := ParaLinkOptions;
  407. while(Length(s) > 0) and (s[1] = ' ') do
  408. delete (s,1,1);
  409. p := pos ('"',s);
  410. while p > 0 do
  411. begin
  412. delete (s,p,1);
  413. p := pos ('"',s);
  414. end;
  415. p := pos (';',s);
  416. while p > 0 do
  417. begin
  418. s2 := copy(s,1,p-1);
  419. comment (V_Debug,'adding "'+s2+'" to nlmvonv input');
  420. NLMConvLinkFile.Add(s2);
  421. delete (s,1,p);
  422. p := pos (';',s);
  423. end;
  424. if s <> '' then
  425. begin
  426. comment (V_Debug,'adding "'+s+'" to nlmvonv input');
  427. NLMConvLinkFile.Add(s);
  428. end;
  429. WriteResponseFile:=True;
  430. end;
  431. function TLinkerNetware.MakeExecutable:boolean;
  432. var
  433. binstr,
  434. cmdstr : TCmdStr;
  435. success : boolean;
  436. StripStr : string[2];
  437. begin
  438. if not(cs_link_nolink in current_settings.globalswitches) then
  439. Message1(exec_i_linking,current_module.exefilename^);
  440. { Create some replacements }
  441. StripStr:='';
  442. if (cs_link_strip in current_settings.globalswitches) then
  443. StripStr:='-s';
  444. { Write used files and libraries and create Headerfile for
  445. nlmconv in NLMConvLinkFile }
  446. WriteResponseFile(false);
  447. { Call linker, this will generate a new object file that will be passed
  448. to nlmconv. Otherwise we could not create nlms without debug info }
  449. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  450. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  451. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  452. Replace(cmdstr,'$STRIP',StripStr);
  453. Replace(cmdstr,'$TMPOBJ',maybequoted(outputexedir+tmpLinkFileName));
  454. Comment (v_debug,'Executing '+BinStr+' '+cmdstr);
  455. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  456. { Remove ReponseFile }
  457. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  458. DeleteFile(outputexedir+Info.ResName);
  459. { Call nlmconv }
  460. if success then
  461. begin
  462. NLMConvLinkFile.writetodisk;
  463. NLMConvLinkFile.Free;
  464. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  465. Replace(cmdstr,'$RES',maybequoted(outputexedir+'n'+Info.ResName));
  466. Comment (v_debug,'Executing '+BinStr+' '+cmdstr);
  467. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  468. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  469. begin
  470. DeleteFile(outputexedir+'n'+Info.ResName);
  471. DeleteFile(outputexedir+tmpLinkFileName);
  472. end;
  473. end;
  474. MakeExecutable:=success; { otherwise a recursive call to link method }
  475. end;
  476. {*****************************************************************************
  477. Initialize
  478. *****************************************************************************}
  479. initialization
  480. RegisterExternalLinker(system_i386_netware_info,TLinkerNetware);
  481. RegisterImport(system_i386_netware,TImportLibNetware);
  482. RegisterExport(system_i386_netware,TExportLibNetware);
  483. RegisterTarget(system_i386_netware_info);
  484. end.