t_morph.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 $GCSECTIONS -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. if UseVLink and (source_info.dirsep <> '/') then
  80. LinkRes.fForceUseForwardSlash:=true;
  81. { Write path to search libraries }
  82. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  83. while assigned(HPath) do
  84. begin
  85. s:=HPath.Str;
  86. if not (cs_link_on_target in current_settings.globalswitches) then
  87. s:=ScriptFixFileName(s);
  88. LinkRes.Add('-L'+s);
  89. HPath:=TCmdStrListItem(HPath.Next);
  90. end;
  91. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  92. while assigned(HPath) do
  93. begin
  94. s:=HPath.Str;
  95. if s<>'' then
  96. LinkRes.Add('SEARCH_DIR("'+Unix2AmigaPath(s)+'")');
  97. HPath:=TCmdStrListItem(HPath.Next);
  98. end;
  99. LinkRes.Add('INPUT (');
  100. { add objectfiles, start with prt0 always }
  101. if not (target_info.system in systems_internal_sysinit) then
  102. begin
  103. s:=FindObjectFile('prt0','',false);
  104. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  105. end;
  106. while not ObjectFiles.Empty do
  107. begin
  108. s:=ObjectFiles.GetFirst;
  109. if s<>'' then
  110. begin
  111. { vlink doesn't use SEARCH_DIR for object files }
  112. if UseVLink then
  113. s:=FindObjectFile(s,'',false);
  114. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  115. end;
  116. end;
  117. { Write staticlibraries }
  118. if not StaticLibFiles.Empty then
  119. begin
  120. { vlink doesn't need, and doesn't support GROUP }
  121. if not UseVLink then
  122. begin
  123. LinkRes.Add(')');
  124. LinkRes.Add('GROUP(');
  125. end;
  126. while not StaticLibFiles.Empty do
  127. begin
  128. S:=StaticLibFiles.GetFirst;
  129. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  130. end;
  131. end;
  132. if not UseVLink then
  133. begin
  134. LinkRes.Add(')');
  135. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  136. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  137. linklibc:=false;
  138. while not SharedLibFiles.Empty do
  139. begin
  140. S:=SharedLibFiles.GetFirst;
  141. if s<>'c' then
  142. begin
  143. i:=Pos(target_info.sharedlibext,S);
  144. if i>0 then
  145. Delete(S,i,255);
  146. LinkRes.Add('-l'+s);
  147. end
  148. else
  149. begin
  150. LinkRes.Add('-l'+s);
  151. linklibc:=true;
  152. end;
  153. end;
  154. { be sure that libc&libgcc is the last lib }
  155. if linklibc then
  156. begin
  157. LinkRes.Add('-lc');
  158. LinkRes.Add('-lgcc');
  159. end;
  160. end
  161. else
  162. begin
  163. while not SharedLibFiles.Empty do
  164. begin
  165. S:=SharedLibFiles.GetFirst;
  166. LinkRes.Add('lib'+s+target_info.staticlibext);
  167. end;
  168. LinkRes.Add(')');
  169. end;
  170. { Write and Close response }
  171. linkres.writetodisk;
  172. linkres.free;
  173. WriteResponseFile:=True;
  174. end;
  175. function TLinkerMorphOS.MakeExecutable:boolean;
  176. var
  177. binstr,
  178. cmdstr : TCmdStr;
  179. success : boolean;
  180. GCSectionsStr: string;
  181. StripStr: string[40];
  182. begin
  183. StripStr:='';
  184. GCSectionsStr:='';
  185. if not(cs_link_nolink in current_settings.globalswitches) then
  186. Message1(exec_i_linking,current_module.exefilename);
  187. if UseVLink then
  188. begin
  189. if (cs_link_strip in current_settings.globalswitches) then
  190. StripStr:='-s -P __abox__';
  191. if create_smartlink_sections then
  192. GCSectionsStr:='-gc-all -sc -sd';
  193. end
  194. else
  195. begin
  196. if create_smartlink_sections then
  197. GCSectionsStr:='--gc-sections -e _start';
  198. end;
  199. { Write used files and libraries }
  200. WriteResponseFile(false);
  201. { Call linker }
  202. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  203. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  204. if UseVLink then
  205. begin
  206. Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename))));
  207. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));
  208. Replace(cmdstr,'$STRIP',StripStr);
  209. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  210. end
  211. else
  212. begin
  213. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename)));
  214. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  215. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  216. end;
  217. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  218. { Stripping Enabled? }
  219. { For MorphOS ld a separate strip command is needed, to avoid stripping }
  220. { __abox__ symbol, which is required to be present in current MorphOS }
  221. { executables. }
  222. if not UseVLink then
  223. begin
  224. if success and (cs_link_strip in current_settings.globalswitches) then
  225. begin
  226. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  227. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  228. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  229. end;
  230. end;
  231. { Remove ReponseFile }
  232. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  233. DeleteFile(outputexedir+Info.ResName);
  234. MakeExecutable:=success; { otherwise a recursive call to link method }
  235. end;
  236. {*****************************************************************************
  237. Initialize
  238. *****************************************************************************}
  239. initialization
  240. RegisterLinker(ld_morphos,TLinkerMorphOS);
  241. RegisterTarget(system_powerpc_morphos_info);
  242. RegisterRes(res_elf_info, TWinLikeResourceFile);
  243. end.