2
0

expunix.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. {
  2. Copyright (c) 2008 by the Free Pascal Compiler team
  3. This unit implements common support for import,export,link routines
  4. for unix 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. ****************************************************************************
  17. }
  18. unit expunix;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. systems,
  24. export,
  25. symtype,symdef,symsym,
  26. aasmbase;
  27. type
  28. texportlibunix=class(texportlib)
  29. private
  30. fexportedsymnames: TCmdStrList;
  31. public
  32. constructor Create; override;
  33. destructor destroy; override;
  34. procedure preparelib(const s : string);override;
  35. procedure exportprocedure(hp : texported_item);override;
  36. procedure exportvar(hp : texported_item);override;
  37. procedure generatelib;override;
  38. property exportedsymnames: TCmdStrList read fexportedsymnames;
  39. end;
  40. implementation
  41. {****************************************************************************
  42. TExportLibUnix
  43. ****************************************************************************}
  44. uses
  45. symconst,
  46. globtype,globals,
  47. aasmdata,aasmtai,aasmcpu,
  48. fmodule,
  49. cgbase,cgutils,cpubase,cgobj,
  50. ncgutil,
  51. verbose;
  52. constructor texportlibunix.create;
  53. begin
  54. inherited create;
  55. fexportedsymnames:=tcmdstrlist.create_no_double;
  56. end;
  57. destructor texportlibunix.destroy;
  58. begin
  59. fexportedsymnames.free;
  60. inherited destroy;
  61. end;
  62. procedure texportlibunix.preparelib(const s:string);
  63. begin
  64. end;
  65. procedure texportlibunix.exportprocedure(hp : texported_item);
  66. var
  67. hp2 : texported_item;
  68. begin
  69. { first test the index value }
  70. if (hp.options and eo_index)<>0 then
  71. begin
  72. Message1(parser_e_no_export_with_index_for_target,target_info.shortname);
  73. exit;
  74. end;
  75. { now place in correct order }
  76. hp2:=texported_item(current_module._exports.first);
  77. while assigned(hp2) and
  78. (hp.name^>hp2.name^) do
  79. hp2:=texported_item(hp2.next);
  80. { insert hp there !! }
  81. if assigned(hp2) and (hp2.name^=hp.name^) then
  82. begin
  83. { this is not allowed !! }
  84. Message1(parser_e_export_name_double,hp.name^);
  85. exit;
  86. end;
  87. if hp2=texported_item(current_module._exports.first) then
  88. current_module._exports.concat(hp)
  89. else if assigned(hp2) then
  90. begin
  91. hp.next:=hp2;
  92. hp.previous:=hp2.previous;
  93. if assigned(hp2.previous) then
  94. hp2.previous.next:=hp;
  95. hp2.previous:=hp;
  96. end
  97. else
  98. current_module._exports.concat(hp);
  99. end;
  100. procedure texportlibunix.exportvar(hp : texported_item);
  101. begin
  102. hp.is_var:=true;
  103. exportprocedure(hp);
  104. end;
  105. procedure texportlibunix.generatelib; // straight t_linux copy for now.
  106. var
  107. hp2 : texported_item;
  108. pd : tprocdef;
  109. {$ifdef x86}
  110. sym : tasmsymbol;
  111. r : treference;
  112. {$endif x86}
  113. begin
  114. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  115. hp2:=texported_item(current_module._exports.first);
  116. while assigned(hp2) do
  117. begin
  118. if (not hp2.is_var) and
  119. (hp2.sym.typ=procsym) then
  120. begin
  121. { the manglednames can already be the same when the procedure
  122. is declared with cdecl }
  123. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  124. if not has_alias_name(pd,hp2.name^) then
  125. begin
  126. { place jump in al_procedures }
  127. current_asmdata.asmlists[al_procedures].concat(tai_align.create(target_info.alignment.procalign));
  128. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  129. if (cs_create_pic in current_settings.moduleswitches) and
  130. { other targets need to be checked how it works }
  131. (target_info.system in [system_i386_freebsd,system_x86_64_freebsd,system_x86_64_linux,system_i386_linux]) then
  132. begin
  133. {$ifdef x86}
  134. sym:=current_asmdata.RefAsmSymbol(pd.mangledname);
  135. reference_reset_symbol(r,sym,0,sizeof(pint));
  136. if cs_create_pic in current_settings.moduleswitches then
  137. r.refaddr:=addr_pic
  138. else
  139. r.refaddr:=addr_full;
  140. current_asmdata.asmlists[al_procedures].concat(taicpu.op_ref(A_JMP,S_NO,r));
  141. {$endif x86}
  142. end
  143. else
  144. cg.a_jmp_name(current_asmdata.asmlists[al_procedures],pd.mangledname);
  145. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  146. end;
  147. exportedsymnames.insert(hp2.name^);
  148. end
  149. else
  150. begin
  151. if (hp2.name^<>hp2.sym.mangledname) then
  152. Message2(parser_e_cant_export_var_different_name,hp2.sym.realname,hp2.sym.mangledname)
  153. else
  154. exportedsymnames.insert(hp2.name^);
  155. end;
  156. hp2:=texported_item(hp2.next);
  157. end;
  158. end;
  159. end.