t_nwm.pas 18 KB

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