t_aros.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. {
  2. Copyright (c) 2004-2006 by Free Pascal Development Team
  3. This unit implements support import, export, link routines
  4. for the AROS targets (AROS/i386, AROS/x86_64)
  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_aros;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. rescmn, comprsrc, import, link, ogbase;
  23. type
  24. timportlibaros=class(timportlib)
  25. procedure generatelib; override;
  26. end;
  27. PLinkeraros = ^TLinkeraros;
  28. TLinkeraros = class(texternallinker)
  29. private
  30. function WriteResponseFile(isdll: boolean): boolean;
  31. function MakeAROSExe: boolean;
  32. public
  33. constructor Create; override;
  34. procedure SetDefaultInfo; override;
  35. procedure InitSysInitUnitName; override;
  36. function MakeExecutable: boolean; override;
  37. end;
  38. implementation
  39. uses
  40. SysUtils,
  41. cutils,cfileutl,cclasses,aasmbase,
  42. globtype,globals,systems,verbose,cscript,fmodule,i_aros;
  43. procedure timportlibaros.generatelib;
  44. var
  45. i: longint;
  46. ImportLibrary: TImportLibrary;
  47. begin
  48. for i:=0 to current_module.ImportLibraryList.count -1 do
  49. begin
  50. ImportLibrary := TImportlibrary(current_module.ImportLibraryList[i]);
  51. current_module.linkothersharedlibs.add(ImportLibrary.Name, link_always);
  52. end;
  53. end;
  54. {****************************************************************************
  55. TLinkeraros
  56. ****************************************************************************}
  57. constructor TLinkeraros.Create;
  58. begin
  59. Inherited Create;
  60. { allow duplicated libs (PM) }
  61. SharedLibFiles.doubles:=true;
  62. StaticLibFiles.doubles:=true;
  63. end;
  64. procedure TLinkeraros.SetDefaultInfo;
  65. begin
  66. with Info do begin
  67. { Note: collect-aros seems to be buggy, and doesn't forward options }
  68. { properly when calling the underlying GNU LD. (FIXME?) }
  69. { This means paths with spaces in them are not supported for now on AROS. }
  70. { So for example no Ram Disk: usage for anything which must be linked. (KB) }
  71. ExeCmd[1]:='collect-aros $OPT $GCSECTIONS $ENTRY -d -n -o $EXE $RES';
  72. ExeCmd[2]:='strip --strip-unneeded $EXE';
  73. end;
  74. end;
  75. Procedure TLinkeraros.InitSysInitUnitName;
  76. begin
  77. sysinitunit:='si_prc';
  78. end;
  79. function TLinkeraros.WriteResponseFile(isdll: boolean): boolean;
  80. var
  81. linkres : TLinkRes;
  82. i : longint;
  83. HPath : TCmdStrListItem;
  84. s,s1 : string;
  85. linklibc : boolean;
  86. begin
  87. WriteResponseFile:=False;
  88. { Open link.res file }
  89. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  90. { Write path to search libraries }
  91. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  92. while assigned(HPath) do
  93. begin
  94. s:=HPath.Str;
  95. if (cs_link_on_target in current_settings.globalswitches) then
  96. s:=ScriptFixFileName(s);
  97. LinkRes.Add('-L'+s);
  98. HPath:=TCmdStrListItem(HPath.Next);
  99. end;
  100. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  101. while assigned(HPath) do
  102. begin
  103. s:=HPath.Str;
  104. s1 := Unix2AmigaPath(maybequoted(s));
  105. if trim(s1)<>'' then
  106. LinkRes.Add('SEARCH_DIR('+s1+')');
  107. HPath:=TCmdStrListItem(HPath.Next);
  108. end;
  109. LinkRes.Add('INPUT (');
  110. { add objectfiles, start with prt0 always }
  111. if not (target_info.system in systems_internal_sysinit) then
  112. begin
  113. s:=FindObjectFile('prt0','',false);
  114. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  115. end;
  116. while not ObjectFiles.Empty do
  117. begin
  118. s:=ObjectFiles.GetFirst;
  119. if s<>'' then
  120. begin
  121. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  122. end;
  123. end;
  124. { Write staticlibraries }
  125. if not StaticLibFiles.Empty then
  126. begin
  127. LinkRes.Add(')');
  128. LinkRes.Add('GROUP(');
  129. while not StaticLibFiles.Empty do
  130. begin
  131. S:=StaticLibFiles.GetFirst;
  132. LinkRes.AddFileName(Unix2AmigaPath(maybequoted(s)));
  133. end;
  134. end;
  135. if (cs_link_on_target in current_settings.globalswitches) then
  136. begin
  137. LinkRes.Add(')');
  138. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  139. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  140. linklibc:=false;
  141. while not SharedLibFiles.Empty do
  142. begin
  143. S:=SharedLibFiles.GetFirst;
  144. if s<>'c' then
  145. begin
  146. i:=Pos(target_info.sharedlibext,S);
  147. if i>0 then
  148. Delete(S,i,255);
  149. LinkRes.Add('-l'+s);
  150. end
  151. else
  152. begin
  153. LinkRes.Add('-l'+s);
  154. linklibc:=true;
  155. end;
  156. end;
  157. { be sure that libc&libgcc is the last lib }
  158. if linklibc then
  159. begin
  160. LinkRes.Add('-lc');
  161. LinkRes.Add('-lgcc');
  162. end;
  163. end
  164. else
  165. begin
  166. while not SharedLibFiles.Empty do
  167. begin
  168. S:=SharedLibFiles.GetFirst;
  169. LinkRes.Add('lib'+s+target_info.staticlibext);
  170. end;
  171. LinkRes.Add(')');
  172. end;
  173. { Write and Close response }
  174. linkres.writetodisk;
  175. linkres.free;
  176. WriteResponseFile:=True;
  177. end;
  178. function TLinkeraros.MakeAROSExe: boolean;
  179. var
  180. BinStr,
  181. CmdStr : TCmdStr;
  182. EntryStr: string;
  183. GCSectionsStr: string;
  184. success: boolean;
  185. begin
  186. GCSectionsStr:='';
  187. EntryStr:='-e _start';
  188. if create_smartlink_sections then
  189. GCSectionsStr:='--gc-sections';
  190. { Call linker }
  191. SplitBinCmd(Info.ExeCmd[1],BinStr,CmdStr);
  192. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  193. Replace(cmdstr,'$EXE',maybequoted(ScriptFixFileName(current_module.exefilename)));
  194. Replace(cmdstr,'$RES',maybequoted(ScriptFixFileName(outputexedir+Info.ResName)));
  195. Replace(cmdstr,'$ENTRY',EntryStr);
  196. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  197. { Replace(cmdstr,'$EXE',Unix2AmigaPath(maybequoted(ScriptFixFileName(current_module.exefilename^))));
  198. Replace(cmdstr,'$RES',Unix2AmigaPath(maybequoted(ScriptFixFileName(outputexedir+Info.ResName))));}
  199. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  200. { Call Strip }
  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(ScriptFixFileName(current_module.exefilename)));
  205. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  206. end;
  207. MakeAROSExe:=success;
  208. end;
  209. function TLinkeraros.MakeExecutable:boolean;
  210. var
  211. success : boolean;
  212. begin
  213. success:=false;
  214. if not(cs_link_nolink in current_settings.globalswitches) then
  215. Message1(exec_i_linking,current_module.exefilename);
  216. { Write used files and libraries }
  217. WriteResponseFile(false);
  218. success:=MakeAROSExe;
  219. { Remove ReponseFile }
  220. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  221. DeleteFile(outputexedir+Info.ResName);
  222. MakeExecutable:=success; { otherwise a recursive call to link method }
  223. end;
  224. {*****************************************************************************
  225. Initialize
  226. *****************************************************************************}
  227. initialization
  228. {$ifdef i386}
  229. RegisterLinker(ld_aros,TLinkeraros);
  230. RegisterTarget(system_i386_aros_info);
  231. {$endif i386}
  232. {$ifdef x86_64}
  233. RegisterLinker(ld_aros,TLinkeraros);
  234. RegisterTarget(system_x86_64_aros_info);
  235. {$endif x86_64}
  236. {$ifdef arm}
  237. RegisterLinker(ld_aros,TLinkeraros);
  238. RegisterTarget(system_arm_aros_info);
  239. {$endif arm}
  240. RegisterRes(res_elf_info, TWinLikeResourceFile);
  241. end.