2
0

expunix.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. cgcpu,hlcgobj,hlcgcpu,
  51. ncgutil,
  52. verbose;
  53. constructor texportlibunix.create;
  54. begin
  55. inherited create;
  56. fexportedsymnames:=tcmdstrlist.create_no_double;
  57. end;
  58. destructor texportlibunix.destroy;
  59. begin
  60. fexportedsymnames.free;
  61. inherited destroy;
  62. end;
  63. procedure texportlibunix.preparelib(const s:string);
  64. begin
  65. end;
  66. procedure texportlibunix.exportprocedure(hp : texported_item);
  67. var
  68. hp2 : texported_item;
  69. begin
  70. { first test the index value }
  71. if eo_index in hp.options then
  72. begin
  73. Message1(parser_e_no_export_with_index_for_target,target_info.shortname);
  74. exit;
  75. end;
  76. { now place in correct order }
  77. hp2:=texported_item(current_module._exports.first);
  78. while assigned(hp2) and
  79. (hp.name^>hp2.name^) do
  80. hp2:=texported_item(hp2.next);
  81. { insert hp there !! }
  82. if assigned(hp2) and (hp2.name^=hp.name^) then
  83. begin
  84. { this is not allowed !! }
  85. duplicatesymbol(hp.name^);
  86. exit;
  87. end;
  88. if hp2=texported_item(current_module._exports.first) then
  89. current_module._exports.concat(hp)
  90. else if assigned(hp2) then
  91. begin
  92. hp.next:=hp2;
  93. hp.previous:=hp2.previous;
  94. if assigned(hp2.previous) then
  95. hp2.previous.next:=hp;
  96. hp2.previous:=hp;
  97. end
  98. else
  99. current_module._exports.concat(hp);
  100. end;
  101. procedure texportlibunix.exportvar(hp : texported_item);
  102. begin
  103. hp.is_var:=true;
  104. exportprocedure(hp);
  105. end;
  106. procedure texportlibunix.generatelib; // straight t_linux copy for now.
  107. var
  108. hp2 : texported_item;
  109. pd : tprocdef;
  110. anyhasalias : boolean;
  111. i : longint;
  112. {$ifdef x86}
  113. sym : tasmsymbol;
  114. r : treference;
  115. {$endif x86}
  116. begin
  117. create_hlcodegen;
  118. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  119. hp2:=texported_item(current_module._exports.first);
  120. while assigned(hp2) do
  121. begin
  122. if (not hp2.is_var) and
  123. assigned(hp2.sym) and
  124. (hp2.sym.typ=procsym) then
  125. begin
  126. { the manglednames can already be the same when the procedure
  127. is declared with cdecl }
  128. { note: for "exports" sections we only allow non overloaded procsyms,
  129. so checking all symbols only matters for packages }
  130. anyhasalias:=false;
  131. for i:=0 to tprocsym(hp2.sym).procdeflist.count-1 do
  132. begin
  133. pd:=tprocdef(tprocsym(hp2.sym).procdeflist[i]);
  134. anyhasalias:=has_alias_name(pd,hp2.name^);
  135. if anyhasalias then
  136. break;
  137. end;
  138. if not anyhasalias then
  139. begin
  140. { place jump in al_procedures }
  141. current_asmdata.asmlists[al_procedures].concat(tai_align.create(target_info.alignment.procalign));
  142. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  143. if (cs_create_pic in current_settings.moduleswitches) and
  144. { other targets need to be checked how it works }
  145. (target_info.system in [system_i386_freebsd,system_x86_64_freebsd,system_x86_64_linux,system_i386_linux,system_x86_64_solaris,system_i386_solaris,system_i386_android]) then
  146. begin
  147. {$ifdef x86}
  148. sym:=current_asmdata.RefAsmSymbol(pd.mangledname);
  149. reference_reset_symbol(r,sym,0,sizeof(pint));
  150. if cs_create_pic in current_settings.moduleswitches then
  151. r.refaddr:=addr_pic
  152. else
  153. r.refaddr:=addr_full;
  154. current_asmdata.asmlists[al_procedures].concat(taicpu.op_ref(A_JMP,S_NO,r));
  155. {$endif x86}
  156. end
  157. else
  158. cg.g_external_wrapper(current_asmdata.asmlists[al_procedures],pd,pd.mangledname);
  159. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  160. end;
  161. exportedsymnames.insert(hp2.name^);
  162. end
  163. else
  164. begin
  165. if assigned(hp2.sym) and
  166. (hp2.name^<>hp2.sym.mangledname) then
  167. Message2(parser_e_cant_export_var_different_name,hp2.sym.realname,hp2.sym.mangledname)
  168. else
  169. exportedsymnames.insert(hp2.name^);
  170. end;
  171. hp2:=texported_item(hp2.next);
  172. end;
  173. destroy_hlcodegen;
  174. end;
  175. end.