t_nwl.pas 18 KB

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