t_morph.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. $Id$
  3. Copyright (c) 2004 by Free Pascal Development Team
  4. This unit implements support import,export,link routines
  5. for the MorphOS (PowerPC) 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. ****************************************************************************
  18. }
  19. unit t_morph;
  20. {$i fpcdefs.inc}
  21. interface
  22. implementation
  23. uses
  24. link,
  25. cutils,cclasses,
  26. globtype,globals,systems,verbose,script,fmodule,i_morph;
  27. type
  28. PlinkerMorphOS=^TlinkerMorphOS;
  29. TlinkerMorphOS=class(texternallinker)
  30. private
  31. Function WriteResponseFile(isdll:boolean) : Boolean;
  32. public
  33. constructor Create; override;
  34. procedure SetDefaultInfo; override;
  35. function MakeExecutable:boolean; override;
  36. end;
  37. {****************************************************************************
  38. TLinkerMorphOS
  39. ****************************************************************************}
  40. Constructor TLinkerMorphOS.Create;
  41. begin
  42. Inherited Create;
  43. { allow duplicated libs (PM) }
  44. SharedLibFiles.doubles:=true;
  45. StaticLibFiles.doubles:=true;
  46. end;
  47. procedure TLinkerMorphOS.SetDefaultInfo;
  48. begin
  49. with Info do
  50. begin
  51. if (cs_link_on_target in aktglobalswitches) then begin
  52. ExeCmd[1]:='ld $OPT -o $EXE --script $RES';
  53. ExeCmd[2]:='strip --strip-unneeded --remove-section .comment $EXE';
  54. end else begin
  55. ExeCmd[1]:='ld $OPT -o $EXE $RES';
  56. ExeCmd[2]:='strip --strip-unneeded --remove-section .comment $EXE';
  57. end;
  58. end;
  59. end;
  60. Function TLinkerMorphOS.WriteResponseFile(isdll:boolean) : Boolean;
  61. Var
  62. linkres : TLinkRes;
  63. i : longint;
  64. HPath : TStringListItem;
  65. s : string;
  66. linklibc : boolean;
  67. begin
  68. WriteResponseFile:=False;
  69. { Open link.res file }
  70. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  71. { Write path to search libraries }
  72. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  73. while assigned(HPath) do
  74. begin
  75. s:=HPath.Str;
  76. if not (cs_link_on_target in aktglobalswitches) then
  77. s:=GetShortName(s)
  78. else
  79. s:=ScriptFixFileName(s);
  80. LinkRes.Add('-L'+s);
  81. HPath:=TStringListItem(HPath.Next);
  82. end;
  83. HPath:=TStringListItem(LibrarySearchPath.First);
  84. while assigned(HPath) do
  85. begin
  86. s:=HPath.Str;
  87. if not (cs_link_on_target in aktglobalswitches) then
  88. s:=GetShortName(s);
  89. if s<>'' then
  90. LinkRes.Add('SEARCH_DIR('+s+')');
  91. HPath:=TStringListItem(HPath.Next);
  92. end;
  93. LinkRes.Add('INPUT (');
  94. { add objectfiles, start with prt0 always }
  95. s:=FindObjectFile('prt0','',false);
  96. if not (cs_link_on_target in aktglobalswitches) then
  97. s:=GetShortName(s);
  98. LinkRes.AddFileName(s);
  99. while not ObjectFiles.Empty do
  100. begin
  101. s:=ObjectFiles.GetFirst;
  102. if not (cs_link_on_target in aktglobalswitches) then
  103. s:=GetShortName(s);
  104. if s<>'' then
  105. LinkRes.AddFileName(s);
  106. end;
  107. LinkRes.Add(')');
  108. { Write staticlibraries }
  109. if not StaticLibFiles.Empty then
  110. begin
  111. LinkRes.Add('GROUP(');
  112. While not StaticLibFiles.Empty do
  113. begin
  114. S:=StaticLibFiles.GetFirst;
  115. if not (cs_link_on_target in aktglobalswitches) then
  116. s:=GetShortName(s);
  117. LinkRes.AddFileName(s);
  118. end;
  119. LinkRes.Add(')');
  120. end;
  121. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  122. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  123. linklibc:=false;
  124. While not SharedLibFiles.Empty do
  125. begin
  126. S:=SharedLibFiles.GetFirst;
  127. if s<>'c' then
  128. begin
  129. i:=Pos(target_info.sharedlibext,S);
  130. if i>0 then
  131. Delete(S,i,255);
  132. LinkRes.Add('-l'+s);
  133. end
  134. else
  135. begin
  136. LinkRes.Add('-l'+s);
  137. linklibc:=true;
  138. end;
  139. end;
  140. { be sure that libc&libgcc is the last lib }
  141. if linklibc then
  142. begin
  143. LinkRes.Add('-lc');
  144. LinkRes.Add('-lgcc');
  145. end;
  146. { Write and Close response }
  147. linkres.writetodisk;
  148. linkres.free;
  149. WriteResponseFile:=True;
  150. end;
  151. function TLinkerMorphOS.MakeExecutable:boolean;
  152. var
  153. binstr,
  154. cmdstr : string;
  155. success : boolean;
  156. begin
  157. if not(cs_link_extern in aktglobalswitches) then
  158. Message1(exec_i_linking,current_module.exefilename^);
  159. { Write used files and libraries }
  160. WriteResponseFile(false);
  161. { Call linker }
  162. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  163. if pos(' ',current_module.exefilename^)>0 then
  164. Replace(cmdstr,'$EXE','"'+ScriptFixFileName(current_module.exefilename^)+'"')
  165. else
  166. Replace(cmdstr,'$EXE',ScriptFixFileName(current_module.exefilename^));
  167. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  168. Replace(cmdstr,'$RES',ScriptFixFileName(outputexedir+Info.ResName));
  169. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  170. { Stripping Enabled? }
  171. { Under MorphOS a separate strip command is needed, to avoid stripping }
  172. { __abox__ symbol, which is required to be present in current MorphOS }
  173. { executables. }
  174. if success and (cs_link_strip in aktglobalswitches) then
  175. begin
  176. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  177. Replace(cmdstr,'$EXE',current_module.exefilename^);
  178. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  179. end;
  180. { Remove ReponseFile }
  181. if (success) and not(cs_link_extern in aktglobalswitches) then
  182. RemoveFile(outputexedir+Info.ResName);
  183. MakeExecutable:=success; { otherwise a recursive call to link method }
  184. end;
  185. {*****************************************************************************
  186. Initialize
  187. *****************************************************************************}
  188. initialization
  189. RegisterExternalLinker(system_powerpc_morphos_info,TLinkerMorphOS);
  190. RegisterTarget(system_powerpc_morphos_info);
  191. end.
  192. {
  193. $Log$
  194. Revision 1.6 2004-06-08 15:04:23 karoly
  195. * fixed stripping really this time
  196. Revision 1.5 2004/06/07 23:44:37 karoly
  197. + fixed stripping support
  198. Revision 1.4 2004/04/28 15:19:03 florian
  199. + syscall directive support for MorphOS added
  200. Revision 1.3 2004/04/09 01:32:46 karoly
  201. * disable stripping in mos linking scripts.
  202. Revision 1.2 2004/04/08 17:11:02 karoly
  203. * added external linker support based on 1.0 amiga support
  204. Revision 1.1 2004/02/13 05:46:58 karoly
  205. * added powerpc-morphos target
  206. }