t_morph.pas 7.5 KB

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