t_morph.pas 7.3 KB

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