t_morph.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. {$IFDEF MORPHOS}
  38. { * PathConv is implemented in the system unit! * }
  39. function PathConv(path: string): string; external name 'PATHCONV';
  40. {$ELSE}
  41. function PathConv(path: string): string;
  42. begin
  43. PathConv:=path;
  44. end;
  45. {$ENDIF}
  46. {****************************************************************************
  47. TLinkerMorphOS
  48. ****************************************************************************}
  49. Constructor TLinkerMorphOS.Create;
  50. begin
  51. Inherited Create;
  52. { allow duplicated libs (PM) }
  53. SharedLibFiles.doubles:=true;
  54. StaticLibFiles.doubles:=true;
  55. end;
  56. procedure TLinkerMorphOS.SetDefaultInfo;
  57. begin
  58. with Info do
  59. begin
  60. if (cs_link_on_target in aktglobalswitches) then begin
  61. ExeCmd[1]:='ld $OPT -o $EXE $RES';
  62. ExeCmd[2]:='strip --strip-unneeded --remove-section .comment $EXE';
  63. end else begin
  64. ExeCmd[1]:='fpcvlink -b elf32amiga $OPT $STRIP -o $EXE -F $RES';
  65. end;
  66. end;
  67. end;
  68. Function TLinkerMorphOS.WriteResponseFile(isdll:boolean) : Boolean;
  69. Var
  70. linkres : TLinkRes;
  71. i : longint;
  72. HPath : TStringListItem;
  73. s : string;
  74. linklibc : boolean;
  75. begin
  76. WriteResponseFile:=False;
  77. { Open link.res file }
  78. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  79. if (cs_link_on_target in aktglobalswitches) then begin
  80. { Write path to search libraries }
  81. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  82. while assigned(HPath) do
  83. begin
  84. s:=HPath.Str;
  85. if (cs_link_on_target in aktglobalswitches) then
  86. s:=ScriptFixFileName(s);
  87. LinkRes.Add('-L'+s);
  88. HPath:=TStringListItem(HPath.Next);
  89. end;
  90. HPath:=TStringListItem(LibrarySearchPath.First);
  91. while assigned(HPath) do
  92. begin
  93. s:=HPath.Str;
  94. if s<>'' then
  95. LinkRes.Add('SEARCH_DIR('+maybequoted(s)+')');
  96. HPath:=TStringListItem(HPath.Next);
  97. end;
  98. LinkRes.Add('INPUT (');
  99. { add objectfiles, start with prt0 always }
  100. s:=FindObjectFile('prt0','',false);
  101. LinkRes.AddFileName(s);
  102. while not ObjectFiles.Empty do
  103. begin
  104. s:=ObjectFiles.GetFirst;
  105. if s<>'' then
  106. begin
  107. LinkRes.AddFileName(maybequoted(s));
  108. end;
  109. end;
  110. LinkRes.Add(')');
  111. { Write staticlibraries }
  112. if not StaticLibFiles.Empty then
  113. begin
  114. LinkRes.Add('GROUP(');
  115. While not StaticLibFiles.Empty do
  116. begin
  117. S:=StaticLibFiles.GetFirst;
  118. LinkRes.AddFileName(maybequoted(s));
  119. end;
  120. LinkRes.Add(')');
  121. end;
  122. end else begin
  123. { add objectfiles, start with prt0 always }
  124. s:=FindObjectFile('prt0','',false);
  125. LinkRes.AddFileName(s);
  126. while not ObjectFiles.Empty do
  127. begin
  128. s:=ObjectFiles.GetFirst;
  129. if s<>'' then
  130. begin
  131. s:=FindObjectFile(s,'',false);
  132. LinkRes.AddFileName(maybequoted(s));
  133. end;
  134. end;
  135. if not StaticLibFiles.Empty then
  136. begin
  137. While not StaticLibFiles.Empty do
  138. begin
  139. S:=StaticLibFiles.GetFirst;
  140. LinkRes.AddFileName(maybequoted(s));
  141. end;
  142. end;
  143. end;
  144. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  145. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  146. linklibc:=false;
  147. While not SharedLibFiles.Empty do
  148. begin
  149. S:=SharedLibFiles.GetFirst;
  150. if s<>'c' then
  151. begin
  152. i:=Pos(target_info.sharedlibext,S);
  153. if i>0 then
  154. Delete(S,i,255);
  155. LinkRes.Add('-l'+s);
  156. end
  157. else
  158. begin
  159. LinkRes.Add('-l'+s);
  160. linklibc:=true;
  161. end;
  162. end;
  163. { be sure that libc&libgcc is the last lib }
  164. if linklibc then
  165. begin
  166. LinkRes.Add('-lc');
  167. LinkRes.Add('-lgcc');
  168. end;
  169. { Write and Close response }
  170. linkres.writetodisk;
  171. linkres.free;
  172. WriteResponseFile:=True;
  173. end;
  174. function TLinkerMorphOS.MakeExecutable:boolean;
  175. var
  176. binstr,
  177. cmdstr : string;
  178. success : boolean;
  179. StripStr: string[40];
  180. begin
  181. if not(cs_link_extern in aktglobalswitches) then
  182. Message1(exec_i_linking,current_module.exefilename^);
  183. if not (cs_link_on_target in aktglobalswitches) then begin
  184. StripStr:='';
  185. if (cs_link_strip in aktglobalswitches) then
  186. StripStr:='-s -P __abox__';
  187. end;
  188. { Write used files and libraries }
  189. WriteResponseFile(false);
  190. { Call linker }
  191. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  192. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  193. if not(cs_link_on_target in aktglobalswitches) then begin
  194. Replace(cmdstr,'$EXE',PathConv(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  195. Replace(cmdstr,'$RES',PathConv(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  196. Replace(cmdstr,'$STRIP',StripStr);
  197. end else begin
  198. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename^)));
  199. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  200. end;
  201. success:=DoExec(FindUtil(BinStr),cmdstr,true,false);
  202. { Stripping Enabled? }
  203. { For MorphOS a separate strip command is needed, to avoid stripping }
  204. { __abox__ symbol, which is required to be present in current MorphOS }
  205. { executables. }
  206. if (cs_link_on_target in aktglobalswitches) then begin
  207. if success and (cs_link_strip in aktglobalswitches) then
  208. begin
  209. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  210. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  211. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  212. end;
  213. end;
  214. { Remove ReponseFile }
  215. if (success) and not(cs_link_extern in aktglobalswitches) then
  216. RemoveFile(outputexedir+Info.ResName);
  217. MakeExecutable:=success; { otherwise a recursive call to link method }
  218. end;
  219. {*****************************************************************************
  220. Initialize
  221. *****************************************************************************}
  222. initialization
  223. RegisterExternalLinker(system_powerpc_morphos_info,TLinkerMorphOS);
  224. RegisterTarget(system_powerpc_morphos_info);
  225. end.
  226. {
  227. $Log$
  228. Revision 1.11 2005-02-03 03:54:07 karoly
  229. t_morph.pas
  230. Revision 1.10 2004/12/23 18:45:23 jonas
  231. * fixed typo
  232. Revision 1.9 2004/12/22 16:32:46 peter
  233. * maybequoted() added
  234. Revision 1.8 2004/10/25 15:38:41 peter
  235. * heap and heapsize removed
  236. * checkpointer fixes
  237. Revision 1.7 2004/06/20 08:55:32 florian
  238. * logs truncated
  239. Revision 1.6 2004/06/08 15:04:23 karoly
  240. * fixed stripping really this time
  241. Revision 1.5 2004/06/07 23:44:37 karoly
  242. + fixed stripping support
  243. Revision 1.4 2004/04/28 15:19:03 florian
  244. + syscall directive support for MorphOS added
  245. Revision 1.3 2004/04/09 01:32:46 karoly
  246. * disable stripping in mos linking scripts.
  247. Revision 1.2 2004/04/08 17:11:02 karoly
  248. * added external linker support based on 1.0 amiga support
  249. }