t_morph.pas 7.1 KB

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