t_go32v2.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) Go32v2 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_go32v2;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. link,
  24. cutils,cclasses,
  25. globtype,globals,systems,verbose,script,fmodule,i_go32v2,ogcoff;
  26. type
  27. tlinkergo32v2=class(texternallinker)
  28. private
  29. Function WriteResponseFile(isdll:boolean) : Boolean;
  30. Function WriteScript(isdll:boolean) : Boolean;
  31. public
  32. constructor Create;override;
  33. procedure SetDefaultInfo;override;
  34. function MakeExecutable:boolean;override;
  35. end;
  36. {****************************************************************************
  37. TLinkerGo32v2
  38. ****************************************************************************}
  39. Constructor TLinkerGo32v2.Create;
  40. begin
  41. Inherited Create;
  42. { allow duplicated libs (PM) }
  43. SharedLibFiles.doubles:=true;
  44. StaticLibFiles.doubles:=true;
  45. end;
  46. procedure TLinkerGo32v2.SetDefaultInfo;
  47. begin
  48. with Info do
  49. begin
  50. ExeCmd[1]:='ld $SCRIPT $OPT $STRIP -o $EXE $RES';
  51. end;
  52. end;
  53. Function TLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  54. Var
  55. linkres : TLinkRes;
  56. i : longint;
  57. s : string;
  58. linklibc : boolean;
  59. begin
  60. WriteResponseFile:=False;
  61. { Open link.res file }
  62. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  63. { Write staticlibraries }
  64. if not StaticLibFiles.Empty then
  65. begin
  66. LinkRes.Add('-(');
  67. While not StaticLibFiles.Empty do
  68. begin
  69. S:=StaticLibFiles.GetFirst;
  70. LinkRes.AddFileName(GetShortName(s))
  71. end;
  72. LinkRes.Add('-)');
  73. end;
  74. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  75. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  76. linklibc:=false;
  77. While not SharedLibFiles.Empty do
  78. begin
  79. S:=SharedLibFiles.GetFirst;
  80. if s<>'c' then
  81. begin
  82. i:=Pos(target_info.sharedlibext,S);
  83. if i>0 then
  84. Delete(S,i,255);
  85. LinkRes.Add('-l'+s);
  86. end
  87. else
  88. begin
  89. LinkRes.Add('-l'+s);
  90. linklibc:=true;
  91. end;
  92. end;
  93. { be sure that libc&libgcc is the last lib }
  94. if linklibc then
  95. begin
  96. LinkRes.Add('-lc');
  97. LinkRes.Add('-lgcc');
  98. end;
  99. { Write and Close response }
  100. linkres.writetodisk;
  101. LinkRes.Free;
  102. WriteResponseFile:=True;
  103. end;
  104. Function TLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  105. Var
  106. scriptres : TLinkRes;
  107. HPath : TStringListItem;
  108. s : string;
  109. begin
  110. WriteScript:=False;
  111. { Open link.res file }
  112. ScriptRes:=TLinkRes.Create(outputexedir+Info.ScriptName);
  113. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  114. ScriptRes.Add('ENTRY(start)');
  115. ScriptRes.Add('SECTIONS');
  116. ScriptRes.Add('{');
  117. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  118. ScriptRes.Add(' . = ALIGN(16);');
  119. { add objectfiles, start with prt0 always }
  120. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0','',false))+'(.text)');
  121. while not ObjectFiles.Empty do
  122. begin
  123. s:=ObjectFiles.GetFirst;
  124. if s<>'' then
  125. begin
  126. ScriptRes.Add(' . = ALIGN(16);');
  127. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  128. end;
  129. end;
  130. ScriptRes.Add(' *(.text)');
  131. ScriptRes.Add(' etext = . ; _etext = .;');
  132. ScriptRes.Add(' . = ALIGN(0x200);');
  133. ScriptRes.Add(' }');
  134. ScriptRes.Add(' .data ALIGN(0x200) : {');
  135. ScriptRes.Add(' djgpp_first_ctor = . ;');
  136. ScriptRes.Add(' *(.ctor)');
  137. ScriptRes.Add(' djgpp_last_ctor = . ;');
  138. ScriptRes.Add(' djgpp_first_dtor = . ;');
  139. ScriptRes.Add(' *(.dtor)');
  140. ScriptRes.Add(' djgpp_last_dtor = . ;');
  141. ScriptRes.Add(' *(.data)');
  142. ScriptRes.Add(' *(.gcc_exc)');
  143. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  144. ScriptRes.Add(' *(.eh_fram)');
  145. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  146. ScriptRes.Add(' LONG(0)');
  147. ScriptRes.Add(' edata = . ; _edata = .;');
  148. ScriptRes.Add(' . = ALIGN(0x200);');
  149. ScriptRes.Add(' }');
  150. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  151. ScriptRes.Add(' {');
  152. ScriptRes.Add(' _object.2 = . ;');
  153. ScriptRes.Add(' . += 24 ;');
  154. ScriptRes.Add(' *(.bss)');
  155. ScriptRes.Add(' *(COMMON)');
  156. ScriptRes.Add(' end = . ; _end = .;');
  157. ScriptRes.Add(' . = ALIGN(0x200);');
  158. ScriptRes.Add(' }');
  159. ScriptRes.Add(' }');
  160. { Write path to search libraries }
  161. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  162. while assigned(HPath) do
  163. begin
  164. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  165. HPath:=TStringListItem(HPath.Next);
  166. end;
  167. HPath:=TStringListItem(LibrarySearchPath.First);
  168. while assigned(HPath) do
  169. begin
  170. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  171. HPath:=TStringListItem(HPath.Next);
  172. end;
  173. { Write and Close response }
  174. ScriptRes.WriteToDisk;
  175. ScriptRes.Free;
  176. WriteScript:=True;
  177. end;
  178. function TLinkerGo32v2.MakeExecutable:boolean;
  179. var
  180. binstr : String;
  181. cmdstr : TCmdStr;
  182. success : boolean;
  183. StripStr : string[40];
  184. begin
  185. if not(cs_link_extern in aktglobalswitches) then
  186. Message1(exec_i_linking,current_module.exefilename^);
  187. { Create some replacements }
  188. StripStr:='';
  189. if (cs_link_strip in aktglobalswitches) then
  190. StripStr:='-s';
  191. { Write used files and libraries and our own ld script }
  192. WriteScript(false);
  193. WriteResponsefile(false);
  194. { Call linker }
  195. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  196. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  197. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  198. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  199. (* Potential issues with older ld version??? *)
  200. Replace(cmdstr,'$STRIP',StripStr);
  201. Replace(cmdstr,'$SCRIPT','--script='+maybequoted(outputexedir+Info.ScriptName));
  202. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  203. { Remove ReponseFile }
  204. if (success) and not(cs_link_extern in aktglobalswitches) then
  205. begin
  206. RemoveFile(outputexedir+Info.ResName);
  207. RemoveFile(outputexedir+Info.ScriptName);
  208. end;
  209. MakeExecutable:=success; { otherwise a recursive call to link method }
  210. end;
  211. {$ifdef notnecessary}
  212. procedure tlinkergo32v2.postprocessexecutable(const n : string);
  213. type
  214. tcoffheader=packed record
  215. mach : word;
  216. nsects : word;
  217. time : longint;
  218. sympos : longint;
  219. syms : longint;
  220. opthdr : word;
  221. flag : word;
  222. end;
  223. tcoffsechdr=packed record
  224. name : array[0..7] of char;
  225. vsize : longint;
  226. rvaofs : longint;
  227. datalen : longint;
  228. datapos : longint;
  229. relocpos : longint;
  230. lineno1 : longint;
  231. nrelocs : word;
  232. lineno2 : word;
  233. flags : longint;
  234. end;
  235. psecfill=^TSecfill;
  236. TSecfill=record
  237. fillpos,
  238. fillsize : longint;
  239. next : psecfill;
  240. end;
  241. var
  242. f : file;
  243. coffheader : tcoffheader;
  244. firstsecpos,
  245. maxfillsize,
  246. l : longint;
  247. coffsec : tcoffsechdr;
  248. secroot,hsecroot : psecfill;
  249. zerobuf : pointer;
  250. begin
  251. { when -s is used quit, because there is no .exe }
  252. if cs_link_extern in aktglobalswitches then
  253. exit;
  254. { open file }
  255. assign(f,n);
  256. {$I-}
  257. reset(f,1);
  258. if ioresult<>0 then
  259. Message1(execinfo_f_cant_open_executable,n);
  260. { read headers }
  261. seek(f,2048);
  262. blockread(f,coffheader,sizeof(tcoffheader));
  263. { read section info }
  264. maxfillsize:=0;
  265. firstsecpos:=0;
  266. secroot:=nil;
  267. for l:=1to coffheader.nSects do
  268. begin
  269. blockread(f,coffsec,sizeof(tcoffsechdr));
  270. if coffsec.datapos>0 then
  271. begin
  272. if secroot=nil then
  273. firstsecpos:=coffsec.datapos;
  274. new(hsecroot);
  275. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  276. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  277. hsecroot^.next:=secroot;
  278. secroot:=hsecroot;
  279. if secroot^.fillsize>maxfillsize then
  280. maxfillsize:=secroot^.fillsize;
  281. end;
  282. end;
  283. if firstsecpos>0 then
  284. begin
  285. l:=firstsecpos-filepos(f);
  286. if l>maxfillsize then
  287. maxfillsize:=l;
  288. end
  289. else
  290. l:=0;
  291. { get zero buffer }
  292. getmem(zerobuf,maxfillsize);
  293. fillchar(zerobuf^,maxfillsize,0);
  294. { zero from sectioninfo until first section }
  295. blockwrite(f,zerobuf^,l);
  296. { zero section alignments }
  297. while assigned(secroot) do
  298. begin
  299. seek(f,secroot^.fillpos);
  300. blockwrite(f,zerobuf^,secroot^.fillsize);
  301. hsecroot:=secroot;
  302. secroot:=secroot^.next;
  303. dispose(hsecroot);
  304. end;
  305. freemem(zerobuf,maxfillsize);
  306. close(f);
  307. {$I+}
  308. i:=ioresult;
  309. postprocessexecutable:=true;
  310. end;
  311. {$endif}
  312. {*****************************************************************************
  313. Initialize
  314. *****************************************************************************}
  315. initialization
  316. RegisterExternalLinker(system_i386_go32v2_info,TLinkerGo32v2);
  317. RegisterInternalLinker(system_i386_go32v2_info,TDJCoffLinker);
  318. RegisterTarget(system_i386_go32v2_info);
  319. end.