t_nwm.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. s2 := FindObjectFile('nwpre','',false);
  296. Comment (V_Debug,'adding Object File '+s2);
  297. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  298. { main objectfiles, add to linker input }
  299. while not ObjectFiles.Empty do
  300. begin
  301. s:=ObjectFiles.GetFirst;
  302. if s<>'' then
  303. begin
  304. s2 := FindObjectFile (s,'',false);
  305. Comment (V_Debug,'adding Object File '+s2);
  306. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  307. end;
  308. end;
  309. LinkRes.Add (')');
  310. { output file (nlm), add to nlmconv }
  311. {$ifndef netware}
  312. NLMConvLinkFile.Add ('OUTPUT ' + NlmNam);
  313. {$else}
  314. NLMConvLinkFile.Add ('OUTPUT ' + FExpand(NlmNam));
  315. {$endif}
  316. { start and stop-procedures }
  317. NLMConvLinkFile.Add ('START _Prelude'); { defined in rtl/netware/nwpre.as }
  318. NLMConvLinkFile.Add ('EXIT _Stop'); { nwpre.as }
  319. NLMConvLinkFile.Add ('CHECK FPC_NW_CHECKFUNCTION'); { system.pp }
  320. if not (cs_link_strip in aktglobalswitches) then
  321. begin
  322. NLMConvLinkFile.Add ('DEBUG');
  323. Comment(V_Debug,'DEBUG');
  324. end;
  325. { Write staticlibraries }
  326. if not StaticLibFiles.Empty then
  327. begin
  328. LinkRes.Add ('GROUP(');
  329. While not StaticLibFiles.Empty do
  330. begin
  331. S:=lower (StaticLibFiles.GetFirst);
  332. if s<>'' then
  333. begin
  334. {ad: that's a hack !
  335. whith -XX we get the .a files as static libs (in addition to the
  336. imported libraries}
  337. if (pos ('.a',s) <> 0) OR (pos ('.A', s) <> 0) then
  338. begin
  339. S2 := FindObjectFile(s,'',false);
  340. {$ifndef netware} LinkRes.Add (s2); {$else} LinkRes.Add (FExpand(s2)); {$endif}
  341. Comment(V_Debug,'adding Object File (StaticLibFiles) '+S2);
  342. end else
  343. begin
  344. i:=Pos(target_info.staticlibext,S);
  345. if i>0 then
  346. Delete(S,i,255);
  347. S := S + '.imp'; S2 := '';
  348. librarysearchpath.FindFile(S,S2);
  349. {$ifdef netware}
  350. Comment(V_Debug,'IMPORT @'+s2);
  351. s2 := FExpand (S2);
  352. {$endif}
  353. NLMConvLinkFile.Add('IMPORT @'+S2);
  354. Comment(V_Debug,'IMPORT @'+s2);
  355. end;
  356. end
  357. end;
  358. LinkRes.Add (')');
  359. end;
  360. if not SharedLibFiles.Empty then
  361. begin
  362. While not SharedLibFiles.Empty do
  363. begin
  364. {becuase of upper/lower case mix, we may get duplicate
  365. names but nlmconv ignores that.
  366. Here we are setting the import-files for nlmconv. I.e. for
  367. the module clib or clib.nlm we add IMPORT @clib.imp and also
  368. the module clib.nlm (autoload)
  369. ? may it be better to set autoload's via StaticLibFiles ? }
  370. S:=lower (SharedLibFiles.GetFirst);
  371. if s<>'' then
  372. begin
  373. s2:=s;
  374. i:=Pos(target_info.sharedlibext,S);
  375. if i>0 then
  376. Delete(S,i,255);
  377. S := S + '.imp';
  378. librarysearchpath.FindFile(S,S3);
  379. {$ifdef netware}
  380. Comment(V_Debug,'IMPORT @'+S3);
  381. S3 := FExpand (S3);
  382. {$endif}
  383. NLMConvLinkFile.Add('IMPORT @'+S3);
  384. NLMConvLinkFile.Add('MODULE '+s2);
  385. Comment(V_Debug,'MODULE '+S2);
  386. Comment(V_Debug,'IMPORT @'+S3);
  387. end
  388. end;
  389. end;
  390. { write exports }
  391. hp2:=texported_item(current_module._exports.first);
  392. while assigned(hp2) do
  393. begin
  394. if not hp2.is_var then
  395. begin
  396. { Export the Symbol }
  397. Comment(V_Debug,'EXPORT '+hp2.name^);
  398. NLMConvLinkFile.Add ('EXPORT '+hp2.name^);
  399. end
  400. else
  401. { really, i think it is possible }
  402. Message1(parser_e_no_export_of_variables_for_target,'netware');
  403. hp2:=texported_item(hp2.next);
  404. end;
  405. { Write and Close response for ld, response for nlmconv is in NLMConvLinkFile(not written) }
  406. linkres.writetodisk;
  407. LinkRes.Free;
  408. { pass options from -k to nlmconv, ; is interpreted as newline }
  409. s := ParaLinkOptions;
  410. while(Length(s) > 0) and (s[1] = ' ') do
  411. delete (s,1,1);
  412. p := pos ('"',s);
  413. while p > 0 do
  414. begin
  415. delete (s,p,1);
  416. p := pos ('"',s);
  417. end;
  418. p := pos (';',s);
  419. while p > 0 do
  420. begin
  421. s2 := copy(s,1,p-1);
  422. comment (V_Debug,'adding "'+s2+'" to nlmvonv input');
  423. NLMConvLinkFile.Add(s2);
  424. delete (s,1,p);
  425. p := pos (';',s);
  426. end;
  427. if s <> '' then
  428. begin
  429. comment (V_Debug,'adding "'+s+'" to nlmvonv input');
  430. NLMConvLinkFile.Add(s);
  431. end;
  432. WriteResponseFile:=True;
  433. end;
  434. function TLinkerNetware.MakeExecutable:boolean;
  435. var
  436. binstr,
  437. cmdstr : string;
  438. success : boolean;
  439. StripStr : string[2];
  440. begin
  441. if not(cs_link_extern in aktglobalswitches) then
  442. Message1(exec_i_linking,current_module.exefilename^);
  443. { Create some replacements }
  444. StripStr:='';
  445. if (cs_link_strip in aktglobalswitches) then
  446. StripStr:='-s';
  447. { Write used files and libraries and create Headerfile for
  448. nlmconv in NLMConvLinkFile }
  449. WriteResponseFile(false);
  450. { Call linker, this will generate a new object file that will be passed
  451. to nlmconv. Otherwise we could not create nlms without debug info }
  452. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  453. Replace(cmdstr,'$EXE',current_module.exefilename^);
  454. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  455. Replace(cmdstr,'$STRIP',StripStr);
  456. Replace(cmdstr,'$TMPOBJ',outputexedir+tmpLinkFileName);
  457. Comment (v_debug,'Executing '+BinStr+' '+cmdstr);
  458. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  459. { Remove ReponseFile }
  460. if (success) and not(cs_link_extern in aktglobalswitches) then
  461. RemoveFile(outputexedir+Info.ResName);
  462. { Call nlmconv }
  463. if success then
  464. begin
  465. NLMConvLinkFile.writetodisk;
  466. NLMConvLinkFile.Free;
  467. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  468. Replace(cmdstr,'$RES',outputexedir+'n'+Info.ResName);
  469. Comment (v_debug,'Executing '+BinStr+' '+cmdstr);
  470. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  471. if (success) and not(cs_link_extern in aktglobalswitches) then
  472. begin
  473. RemoveFile(outputexedir+'n'+Info.ResName);
  474. RemoveFile(outputexedir+tmpLinkFileName);
  475. end;
  476. end;
  477. MakeExecutable:=success; { otherwise a recursive call to link method }
  478. end;
  479. {*****************************************************************************
  480. Initialize
  481. *****************************************************************************}
  482. initialization
  483. RegisterExternalLinker(system_i386_netware_info,TLinkerNetware);
  484. RegisterImport(system_i386_netware,TImportLibNetware);
  485. RegisterExport(system_i386_netware,TExportLibNetware);
  486. RegisterTarget(system_i386_netware_info);
  487. end.
  488. {
  489. $Log$
  490. Revision 1.17 2004-09-24 10:48:31 armin
  491. * added GROUP for .a files to linker script
  492. Revision 1.16 2004/09/22 15:25:14 mazen
  493. * Fix error committing : previous version must be in branch USE_SYSUTILS
  494. Revision 1.14 2004/08/30 11:17:34 armin
  495. * added support for libc
  496. Revision 1.13 2004/08/01 19:29:06 armin
  497. * changes to compile fpc on netware
  498. Revision 1.12 2004/07/30 16:00:19 armin
  499. * removed -m for nlmconv, it is only valid for ld
  500. Revision 1.11 2004/06/20 08:55:32 florian
  501. * logs truncated
  502. Revision 1.10 2004/03/02 00:36:33 olle
  503. * big transformation of Tai_[const_]Symbol.Create[data]name*
  504. }