t_morph.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. UseVLink: Boolean;
  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. { TODO: always use vlink on MorphOS for now, but allow ld for cross compiling,
  47. we need a command line switch to switch between vlink and ld later. (KB) }
  48. {$IFDEF MORPHOS}
  49. UseVLink:=true;
  50. {$ELSE}
  51. UseVLink:=(cs_link_on_target in current_settings.globalswitches);
  52. {$ENDIF}
  53. end;
  54. procedure TLinkerMorphOS.SetDefaultInfo;
  55. begin
  56. with Info do
  57. begin
  58. if not UseVLink then
  59. begin
  60. ExeCmd[1]:='ld $OPT -o $EXE $RES';
  61. ExeCmd[2]:='strip --strip-unneeded --remove-section .comment $EXE';
  62. end
  63. else
  64. begin
  65. ExeCmd[1]:='fpcvlink -b elf32amiga $OPT $STRIP -o $EXE -T $RES';
  66. end;
  67. end;
  68. end;
  69. Function TLinkerMorphOS.WriteResponseFile(isdll:boolean) : Boolean;
  70. Var
  71. linkres : TLinkRes;
  72. i : longint;
  73. HPath : TCmdStrListItem;
  74. s : string;
  75. linklibc : boolean;
  76. begin
  77. WriteResponseFile:=False;
  78. { Open link.res file }
  79. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  80. { Write path to search libraries }
  81. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  82. while assigned(HPath) do
  83. begin
  84. s:=HPath.Str;
  85. if not (cs_link_on_target in current_settings.globalswitches) then
  86. s:=ScriptFixFileName(s);
  87. LinkRes.Add('-L'+s);
  88. HPath:=TCmdStrListItem(HPath.Next);
  89. end;
  90. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  91. while assigned(HPath) do
  92. begin
  93. s:=HPath.Str;
  94. if s<>'' then
  95. LinkRes.Add('SEARCH_DIR("'+Unix2AmigaPath(s)+'")');
  96. HPath:=TCmdStrListItem(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. { vlink doesn't use SEARCH_DIR for object files }
  108. if UseVLink then
  109. s:=FindObjectFile(s,'',false);
  110. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  111. end;
  112. end;
  113. { Write staticlibraries }
  114. if not StaticLibFiles.Empty then
  115. begin
  116. { vlink doesn't need, and doesn't support GROUP }
  117. if not UseVLink then
  118. begin
  119. LinkRes.Add(')');
  120. LinkRes.Add('GROUP(');
  121. end;
  122. while not StaticLibFiles.Empty do
  123. begin
  124. S:=StaticLibFiles.GetFirst;
  125. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  126. end;
  127. end;
  128. if not UseVLink then
  129. begin
  130. LinkRes.Add(')');
  131. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  132. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  133. linklibc:=false;
  134. while not SharedLibFiles.Empty do
  135. begin
  136. S:=SharedLibFiles.GetFirst;
  137. if s<>'c' then
  138. begin
  139. i:=Pos(target_info.sharedlibext,S);
  140. if i>0 then
  141. Delete(S,i,255);
  142. LinkRes.Add('-l'+s);
  143. end
  144. else
  145. begin
  146. LinkRes.Add('-l'+s);
  147. linklibc:=true;
  148. end;
  149. end;
  150. { be sure that libc&libgcc is the last lib }
  151. if linklibc then
  152. begin
  153. LinkRes.Add('-lc');
  154. LinkRes.Add('-lgcc');
  155. end;
  156. end
  157. else
  158. begin
  159. while not SharedLibFiles.Empty do
  160. begin
  161. S:=SharedLibFiles.GetFirst;
  162. LinkRes.Add('lib'+s+target_info.staticlibext);
  163. end;
  164. LinkRes.Add(')');
  165. end;
  166. { Write and Close response }
  167. linkres.writetodisk;
  168. linkres.free;
  169. WriteResponseFile:=True;
  170. end;
  171. function TLinkerMorphOS.MakeExecutable:boolean;
  172. var
  173. binstr,
  174. cmdstr : TCmdStr;
  175. success : boolean;
  176. StripStr: string[40];
  177. begin
  178. StripStr:='';
  179. if not(cs_link_nolink in current_settings.globalswitches) then
  180. Message1(exec_i_linking,current_module.exefilename);
  181. if UseVLink then
  182. begin
  183. if (cs_link_strip in current_settings.globalswitches) then
  184. StripStr:='-s -P __abox__';
  185. end;
  186. { Write used files and libraries }
  187. WriteResponseFile(false);
  188. { Call linker }
  189. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  190. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  191. if UseVLink then
  192. begin
  193. Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename))));
  194. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  195. Replace(cmdstr,'$STRIP',StripStr);
  196. end
  197. else
  198. begin
  199. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename)));
  200. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  201. end;
  202. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  203. { Stripping Enabled? }
  204. { For MorphOS ld a separate strip command is needed, to avoid stripping }
  205. { __abox__ symbol, which is required to be present in current MorphOS }
  206. { executables. }
  207. if not UseVLink then
  208. begin
  209. if success and (cs_link_strip in current_settings.globalswitches) then
  210. begin
  211. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  212. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  213. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  214. end;
  215. end;
  216. { Remove ReponseFile }
  217. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  218. DeleteFile(outputexedir+Info.ResName);
  219. MakeExecutable:=success; { otherwise a recursive call to link method }
  220. end;
  221. {*****************************************************************************
  222. Initialize
  223. *****************************************************************************}
  224. initialization
  225. RegisterLinker(ld_morphos,TLinkerMorphOS);
  226. RegisterTarget(system_powerpc_morphos_info);
  227. end.