t_aix.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. This unit implements support import,export,link routines
  4. for the AIX 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 t_aix;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. aasmdata,
  23. symsym,symdef,ppu,
  24. import,export,expunix,link;
  25. type
  26. timportlibaix=class(timportlib)
  27. procedure generatelib;override;
  28. end;
  29. texportlibaix=class(texportlibunix)
  30. procedure setfininame(list: TAsmList; const s: string); override;
  31. end;
  32. TLinkerAIX=class(texternallinker)
  33. private
  34. prtobj : string[80];
  35. Function WriteResponseFile(isdll:boolean) : Boolean;
  36. public
  37. constructor Create;override;
  38. procedure SetDefaultInfo;override;
  39. function MakeExecutable:boolean;override;
  40. function MakeSharedLibrary:boolean;override;
  41. end;
  42. implementation
  43. uses
  44. SysUtils,
  45. cutils,cfileutl,cclasses,
  46. verbose,systems,globtype,globals,
  47. symconst,script,
  48. fmodule,
  49. aasmbase,aasmtai,aasmcpu,cpubase,
  50. cgbase,cgobj,cgutils,ogbase,ncgutil,
  51. comprsrc,
  52. rescmn, i_aix
  53. ;
  54. {*****************************************************************************
  55. timportlibaix
  56. *****************************************************************************}
  57. procedure timportlibaix.generatelib;
  58. var
  59. i : longint;
  60. ImportLibrary : TImportLibrary;
  61. begin
  62. for i:=0 to current_module.ImportLibraryList.Count-1 do
  63. begin
  64. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  65. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  66. end;
  67. end;
  68. {*****************************************************************************
  69. texportlibaix
  70. *****************************************************************************}
  71. procedure texportlibaix.setfininame(list: TAsmList; const s: string);
  72. begin
  73. inherited setfininame(list,s);
  74. end;
  75. {*****************************************************************************
  76. TLinkerAIX
  77. *****************************************************************************}
  78. Constructor TLinkerAIX.Create;
  79. begin
  80. Inherited Create;
  81. if not Dontlinkstdlibpath then
  82. if not(cs_profile in current_settings.moduleswitches) then
  83. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib;/usr/X11R6/lib;/opt/freeware/lib',true)
  84. else
  85. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/profiled;/usr/X11R6/lib;/opt/freeware/lib',true)
  86. end;
  87. procedure TLinkerAIX.SetDefaultInfo;
  88. const
  89. {$ifdef powerpc}platform_select='-b32';{$endif}
  90. {$ifdef POWERPC64} platform_select='-b64';{$endif}
  91. begin
  92. with Info do
  93. begin
  94. ExeCmd[1]:='ld '+platform_select+' $OPT $STRIP -L. -o $EXE $CATRES';
  95. DllCmd[1]:='ld '+platform_select+' $OPT $INITFINI $STRIP -L. -o $EXE $CATRES';
  96. end;
  97. {$if defined(powerpc)}
  98. if not(cs_profile in current_settings.moduleswitches) then
  99. prtobj:=sysrootpath+'/lib/crt0.o'
  100. else
  101. prtobj:=sysrootpath+'/lib/gcrt0.o';
  102. {$elseif defined(powerpc64)}
  103. if not(cs_profile in current_settings.moduleswitches) then
  104. prtobj:=sysrootpath+'/lib/crt0_64.o'
  105. else
  106. prtobj:=sysrootpath+'/lib/gcrt0_64.o';
  107. {$else}
  108. {$error unsupported AIX architecture}
  109. {$endif}
  110. end;
  111. Function TLinkerAIX.WriteResponseFile(isdll:boolean) : Boolean;
  112. Var
  113. linkres : TLinkRes;
  114. i : longint;
  115. HPath : TCmdStrListItem;
  116. s,s1 : TCmdStr;
  117. begin
  118. result:=False;
  119. { Open link.res file }
  120. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  121. with linkres do
  122. begin
  123. { Write path to search libraries }
  124. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  125. while assigned(HPath) do
  126. begin
  127. Add('-L'+HPath.Str);
  128. HPath:=TCmdStrListItem(HPath.Next);
  129. end;
  130. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  131. while assigned(HPath) do
  132. begin
  133. Add('-L'+HPath.Str);
  134. HPath:=TCmdStrListItem(HPath.Next);
  135. end;
  136. { add objectfiles, start with prt0 always }
  137. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  138. { main objectfiles }
  139. while not ObjectFiles.Empty do
  140. begin
  141. s:=ObjectFiles.GetFirst;
  142. if s<>'' then
  143. AddFileName(maybequoted(s));
  144. end;
  145. { Write staticlibraries }
  146. if not StaticLibFiles.Empty then
  147. begin
  148. While not StaticLibFiles.Empty do
  149. begin
  150. S:=StaticLibFiles.GetFirst;
  151. AddFileName(maybequoted(s))
  152. end;
  153. end;
  154. { Write sharedlibraries like -l<lib> }
  155. While not SharedLibFiles.Empty do
  156. begin
  157. S:=SharedLibFiles.GetFirst;
  158. i:=Pos(target_info.sharedlibext,S);
  159. if i>0 then
  160. Delete(S,i,255);
  161. Add('-l'+s);
  162. end;
  163. { when we have -static for the linker the we also need libgcc }
  164. if (cs_link_staticflag in current_settings.globalswitches) then
  165. begin
  166. Add('-lgcc');
  167. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  168. Add('-lgcc_eh');
  169. end;
  170. { Write and Close response }
  171. writetodisk;
  172. Free;
  173. end;
  174. WriteResponseFile:=True;
  175. end;
  176. function TLinkerAIX.MakeExecutable:boolean;
  177. var
  178. linkscript: TAsmScript;
  179. binstr,
  180. cmdstr : TCmdStr;
  181. success : boolean;
  182. StripStr : string[40];
  183. begin
  184. if not(cs_link_nolink in current_settings.globalswitches) then
  185. Message1(exec_i_linking,current_module.exefilename^);
  186. { Create some replacements }
  187. StripStr:='';
  188. if (cs_link_strip in current_settings.globalswitches) and
  189. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  190. StripStr:='-s';
  191. if (cs_link_map in current_settings.globalswitches) then
  192. StripStr:='-bmap:'+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  193. { Write used files and libraries }
  194. WriteResponseFile(false);
  195. { Call linker }
  196. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  197. binstr:=FindUtil(utilsprefix+BinStr);
  198. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  199. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  200. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  201. Replace(cmdstr,'$STRIP',StripStr);
  202. { create dynamic symbol table? }
  203. if HasExports then
  204. cmdstr:=cmdstr+' -E';
  205. { custom sysroot? (for cross-compiling -> assume gnu ld) }
  206. if sysrootpath<>'' then
  207. cmdstr:=cmdstr+' --sysroot='+sysrootpath;
  208. if not(cs_link_nolink in current_settings.globalswitches) and
  209. not(tf_no_backquote_support in source_info.flags) then
  210. begin
  211. { we have to use a script to use the IFS hack }
  212. linkscript:=TAsmScriptUnix.create(outputexedir+'ppaslink');
  213. linkscript.AddLinkCommand(binstr,CmdStr,'');
  214. // if (extdbgbinstr<>'') then
  215. // linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
  216. linkscript.WriteToDisk;
  217. BinStr:=linkscript.fn;
  218. if not path_absolute(BinStr) then
  219. BinStr:='./'+BinStr;
  220. CmdStr:='';
  221. end;
  222. success:=DoExec(BinStr,cmdstr,true,true);
  223. { Remove ReponseFile }
  224. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  225. begin
  226. DeleteFile(outputexedir+Info.ResName);
  227. DeleteFile(outputexedir+'ppaslink.sh');
  228. end;
  229. MakeExecutable:=success; { otherwise a recursive call to link method }
  230. end;
  231. Function TLinkerAIX.MakeSharedLibrary:boolean;
  232. var
  233. exportedsyms: text;
  234. InitFiniStr : string[80];
  235. StripStr,
  236. binstr,
  237. cmdstr : TCmdStr;
  238. success : boolean;
  239. begin
  240. MakeSharedLibrary:=false;
  241. if not(cs_link_nolink in current_settings.globalswitches) then
  242. Message1(exec_i_linking,current_module.sharedlibfilename^);
  243. { Write used files and libraries }
  244. WriteResponseFile(true);
  245. { Create some replacements }
  246. InitFiniStr:='-binitfini:'+exportlib.initname+':'+exportlib.fininame;
  247. if cs_link_strip in current_settings.globalswitches then
  248. StripStr:='-s'
  249. else
  250. StripStr:='';
  251. { Call linker }
  252. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  253. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  254. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  255. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  256. Replace(cmdstr,'$INITFINI',InitFiniStr);
  257. Replace(cmdstr,'$STRIP',StripStr);
  258. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  259. { exported symbols }
  260. if not texportlibunix(exportlib).exportedsymnames.empty then
  261. begin
  262. assign(exportedsyms,outputexedir+'linksyms.fpc');
  263. rewrite(exportedsyms);
  264. repeat
  265. writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
  266. until texportlibunix(exportlib).exportedsymnames.empty;
  267. close(exportedsyms);
  268. cmdstr:=cmdstr+' -bE:'+maybequoted(outputexedir)+'linksyms.fpc';
  269. end;
  270. { Remove ReponseFile }
  271. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  272. begin
  273. DeleteFile(outputexedir+Info.ResName);
  274. DeleteFile(outputexedir+'linksyms.fpc');
  275. end;
  276. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  277. end;
  278. {*****************************************************************************
  279. Initialize
  280. *****************************************************************************}
  281. initialization
  282. {$ifdef powerpc}
  283. RegisterExternalLinker(system_powerpc_aix_info,TLinkerAIX);
  284. RegisterImport(system_powerpc_aix,timportlibaix);
  285. RegisterExport(system_powerpc_aix,texportlibaix);
  286. RegisterTarget(system_powerpc_aix_info);
  287. {$endif powerpc}
  288. {$ifdef powerpc64}
  289. RegisterExternalLinker(system_powerpc64_aix_info,TLinkerAIX);
  290. RegisterImport(system_powerpc64_aix,timportlibaix);
  291. RegisterExport(system_powerpc64_aix,texportlibaix);
  292. RegisterTarget(system_powerpc64_aix_info);
  293. {$endif powerpc64}
  294. RegisterRes(res_elf_info,TWinLikeResourceFile);
  295. end.