t_morph.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 (cs_link_on_target in aktglobalswitches) then
  77. s:=ScriptFixFileName(s);
  78. LinkRes.Add('-L'+s);
  79. HPath:=TStringListItem(HPath.Next);
  80. end;
  81. HPath:=TStringListItem(LibrarySearchPath.First);
  82. while assigned(HPath) do
  83. begin
  84. s:=HPath.Str;
  85. if s<>'' then
  86. LinkRes.Add('SEARCH_DIR('+maybequoted(s)+')');
  87. HPath:=TStringListItem(HPath.Next);
  88. end;
  89. LinkRes.Add('INPUT (');
  90. { add objectfiles, start with prt0 always }
  91. s:=FindObjectFile('prt0','',false);
  92. LinkRes.AddFileName(s);
  93. while not ObjectFiles.Empty do
  94. begin
  95. s:=ObjectFiles.GetFirst;
  96. if s<>'' then
  97. LinkRes.AddFileName(maybequoted(s));
  98. end;
  99. LinkRes.Add(')');
  100. { Write staticlibraries }
  101. if not StaticLibFiles.Empty then
  102. begin
  103. LinkRes.Add('GROUP(');
  104. While not StaticLibFiles.Empty do
  105. begin
  106. S:=StaticLibFiles.GetFirst;
  107. LinkRes.AddFileName(maybequoted(s));
  108. end;
  109. LinkRes.Add(')');
  110. end;
  111. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  112. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  113. linklibc:=false;
  114. While not SharedLibFiles.Empty do
  115. begin
  116. S:=SharedLibFiles.GetFirst;
  117. if s<>'c' then
  118. begin
  119. i:=Pos(target_info.sharedlibext,S);
  120. if i>0 then
  121. Delete(S,i,255);
  122. LinkRes.Add('-l'+s);
  123. end
  124. else
  125. begin
  126. LinkRes.Add('-l'+s);
  127. linklibc:=true;
  128. end;
  129. end;
  130. { be sure that libc&libgcc is the last lib }
  131. if linklibc then
  132. begin
  133. LinkRes.Add('-lc');
  134. LinkRes.Add('-lgcc');
  135. end;
  136. { Write and Close response }
  137. linkres.writetodisk;
  138. linkres.free;
  139. WriteResponseFile:=True;
  140. end;
  141. function TLinkerMorphOS.MakeExecutable:boolean;
  142. var
  143. binstr,
  144. cmdstr : string;
  145. success : boolean;
  146. begin
  147. if not(cs_link_extern in aktglobalswitches) then
  148. Message1(exec_i_linking,current_module.exefilename^);
  149. { Write used files and libraries }
  150. WriteResponseFile(false);
  151. { Call linker }
  152. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  153. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename^));
  154. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  155. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  156. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  157. { Stripping Enabled? }
  158. { Under MorphOS a separate strip command is needed, to avoid stripping }
  159. { __abox__ symbol, which is required to be present in current MorphOS }
  160. { executables. }
  161. if success and (cs_link_strip in aktglobalswitches) then
  162. begin
  163. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  164. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  165. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  166. end;
  167. { Remove ReponseFile }
  168. if (success) and not(cs_link_extern in aktglobalswitches) then
  169. RemoveFile(outputexedir+Info.ResName);
  170. MakeExecutable:=success; { otherwise a recursive call to link method }
  171. end;
  172. {*****************************************************************************
  173. Initialize
  174. *****************************************************************************}
  175. initialization
  176. RegisterExternalLinker(system_powerpc_morphos_info,TLinkerMorphOS);
  177. RegisterTarget(system_powerpc_morphos_info);
  178. end.
  179. {
  180. $Log$
  181. Revision 1.9 2004-12-22 16:32:46 peter
  182. * maybequoted() added
  183. Revision 1.8 2004/10/25 15:38:41 peter
  184. * heap and heapsize removed
  185. * checkpointer fixes
  186. Revision 1.7 2004/06/20 08:55:32 florian
  187. * logs truncated
  188. Revision 1.6 2004/06/08 15:04:23 karoly
  189. * fixed stripping really this time
  190. Revision 1.5 2004/06/07 23:44:37 karoly
  191. + fixed stripping support
  192. Revision 1.4 2004/04/28 15:19:03 florian
  193. + syscall directive support for MorphOS added
  194. Revision 1.3 2004/04/09 01:32:46 karoly
  195. * disable stripping in mos linking scripts.
  196. Revision 1.2 2004/04/08 17:11:02 karoly
  197. * added external linker support based on 1.0 amiga support
  198. }