t_msdos.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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_msdos;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutl,cclasses,
  25. globtype,globals,systems,verbose,script,
  26. fmodule,i_msdos,
  27. link{,ogcoff},aasmbase;
  28. type
  29. { TInternalLinkerGo32v2=class(TInternallinker)
  30. constructor create;override;
  31. procedure DefaultLinkScript;override;
  32. end;}
  33. TExternalLinkerGo32v2=class(texternallinker)
  34. private
  35. Function WriteResponseFile(isdll:boolean) : Boolean;
  36. Function WriteScript(isdll:boolean) : Boolean;
  37. public
  38. constructor Create;override;
  39. procedure SetDefaultInfo;override;
  40. function MakeExecutable:boolean;override;
  41. end;
  42. {****************************************************************************
  43. TCoffLinker
  44. ****************************************************************************}
  45. (* constructor TInternalLinkerGo32v2.Create;
  46. begin
  47. inherited Create;
  48. CExeoutput:=TDJCoffexeoutput;
  49. CObjInput:=TDJCoffObjInput;
  50. end;
  51. procedure TInternalLinkerGo32v2.DefaultLinkScript;
  52. var
  53. s: TCmdStr;
  54. linklibc: Boolean;
  55. i: longint;
  56. procedure AddLib(const name: TCmdStr);
  57. var
  58. s2: TCmdStr;
  59. begin
  60. if FindLibraryFile(name,target_info.staticClibprefix,target_info.staticClibext,s2) then
  61. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s2))
  62. else
  63. Comment(V_Error,'Import library not found for '+name);
  64. end;
  65. begin
  66. with LinkScript do
  67. begin
  68. Concat('READOBJECT '+GetShortName(FindObjectFile('prt0','',false)));
  69. while not ObjectFiles.Empty do
  70. begin
  71. s:=ObjectFiles.GetFirst;
  72. if s<>'' then
  73. Concat('READOBJECT '+MaybeQuoted(s));
  74. end;
  75. while not StaticLibFiles.Empty do
  76. begin
  77. s:=StaticLibFiles.GetFirst;
  78. if s<>'' then
  79. Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  80. end;
  81. linklibc:=False;
  82. while not SharedLibFiles.Empty do
  83. begin
  84. S:=SharedLibFiles.GetFirst;
  85. if S<>'c' then
  86. begin
  87. i:=Pos(target_info.sharedlibext,S);
  88. if i>0 then
  89. Delete(S,i,255);
  90. AddLib(s);
  91. end
  92. else
  93. linklibc:=true;
  94. end;
  95. { be sure that to add libc and libgcc at the end }
  96. if linklibc then
  97. begin
  98. AddLib('c');
  99. AddLib('gcc');
  100. end;
  101. Concat('ENTRYNAME start');
  102. Concat('HEADER');
  103. Concat('EXESECTION .text');
  104. Concat(' OBJSECTION .text*');
  105. Concat(' SYMBOL etext');
  106. Concat(' PROVIDE _etext');
  107. Concat('ENDEXESECTION');
  108. Concat('EXESECTION .data');
  109. Concat(' SYMBOL djgpp_first_ctor');
  110. Concat(' OBJSECTION .ctors.*');
  111. Concat(' OBJSECTION .ctor');
  112. Concat(' OBJSECTION .ctors');
  113. Concat(' SYMBOL djgpp_last_ctor');
  114. Concat(' SYMBOL djgpp_first_dtor');
  115. Concat(' OBJSECTION .dtors.*');
  116. Concat(' OBJSECTION .dtor');
  117. Concat(' OBJSECTION .dtors');
  118. Concat(' SYMBOL djgpp_last_dtor');
  119. Concat(' SYMBOL __environ');
  120. Concat(' PROVIDE _environ');
  121. Concat(' LONG 0');
  122. Concat(' OBJSECTION .data*');
  123. Concat(' OBJSECTION .fpc*');
  124. Concat(' OBJSECTION .gcc_exc*');
  125. Concat(' SYMBOL ___EH_FRAME_BEGIN__');
  126. Concat(' OBJSECTION .eh_fram*');
  127. Concat(' SYMBOL ___EH_FRAME_END__');
  128. Concat(' LONG 0');
  129. Concat(' SYMBOL edata');
  130. Concat(' SYMBOL _edata');
  131. Concat('ENDEXESECTION');
  132. Concat('EXESECTION .bss');
  133. //ScriptRes.Add(' _object.2 = . ;');
  134. //ScriptRes.Add(' . += 32 ;');
  135. Concat(' OBJSECTION .bss*');
  136. Concat(' SYMBOL end');
  137. Concat(' SYMBOL _end');
  138. Concat('ENDEXESECTION');
  139. { Stabs debugging sections }
  140. Concat('EXESECTION .stab');
  141. Concat(' OBJSECTION .stab');
  142. Concat('ENDEXESECTION');
  143. Concat('EXESECTION .stabstr');
  144. Concat(' OBJSECTION .stabstr');
  145. Concat('ENDEXESECTION');
  146. { DWARF 2 }
  147. ScriptAddGenericSections('.debug_aranges,.debug_pubnames,.debug_info,.debug_abbrev,'+
  148. '.debug_line,.debug_frame,.debug_str,.debug_loc,.debug_macinfo');
  149. Concat('STABS');
  150. Concat('SYMBOLS');
  151. end;
  152. end;
  153. *)
  154. {****************************************************************************
  155. TExternalLinkerGo32v2
  156. ****************************************************************************}
  157. Constructor TExternalLinkerGo32v2.Create;
  158. begin
  159. Inherited Create;
  160. { allow duplicated libs (PM) }
  161. SharedLibFiles.doubles:=true;
  162. StaticLibFiles.doubles:=true;
  163. end;
  164. procedure TExternalLinkerGo32v2.SetDefaultInfo;
  165. begin
  166. with Info do
  167. begin
  168. ExeCmd[1]:='ld $RES';
  169. end;
  170. end;
  171. Function TExternalLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  172. Var
  173. linkres : TLinkRes;
  174. i : longint;
  175. s : string;
  176. linklibc : boolean;
  177. begin
  178. WriteResponseFile:=False;
  179. { Open link.res file }
  180. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  181. { Add all options to link.res instead of passing them via command line:
  182. DOS command line is limited to 126 characters! }
  183. LinkRes.Add('--script='+maybequoted(outputexedir+Info.ScriptName));
  184. if (cs_link_map in current_settings.globalswitches) then
  185. LinkRes.Add('-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map')));
  186. if create_smartlink_sections then
  187. LinkRes.Add('--gc-sections');
  188. if info.ExtraOptions<>'' then
  189. LinkRes.Add(Info.ExtraOptions);
  190. (* Potential issues with older ld version??? *)
  191. if (cs_link_strip in current_settings.globalswitches) then
  192. LinkRes.Add('-s');
  193. LinkRes.Add('-o '+maybequoted(current_module.exefilename));
  194. { Write staticlibraries }
  195. if not StaticLibFiles.Empty then
  196. begin
  197. LinkRes.Add('-(');
  198. While not StaticLibFiles.Empty do
  199. begin
  200. S:=StaticLibFiles.GetFirst;
  201. LinkRes.AddFileName(GetShortName(s))
  202. end;
  203. LinkRes.Add('-)');
  204. end;
  205. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  206. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  207. linklibc:=false;
  208. While not SharedLibFiles.Empty do
  209. begin
  210. S:=SharedLibFiles.GetFirst;
  211. if s<>'c' then
  212. begin
  213. i:=Pos(target_info.sharedlibext,S);
  214. if i>0 then
  215. Delete(S,i,255);
  216. LinkRes.Add('-l'+s);
  217. end
  218. else
  219. linklibc:=true;
  220. end;
  221. { be sure that libc&libgcc is the last lib }
  222. if linklibc then
  223. begin
  224. LinkRes.Add('-lc');
  225. LinkRes.Add('-lgcc');
  226. end;
  227. { Write and Close response }
  228. linkres.writetodisk;
  229. LinkRes.Free;
  230. WriteResponseFile:=True;
  231. end;
  232. Function TExternalLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  233. Var
  234. scriptres : TLinkRes;
  235. HPath : TCmdStrListItem;
  236. s : string;
  237. begin
  238. WriteScript:=False;
  239. { Open link.res file }
  240. ScriptRes:=TLinkRes.Create(outputexedir+Info.ScriptName,true);
  241. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  242. ScriptRes.Add('ENTRY(start)');
  243. ScriptRes.Add('SECTIONS');
  244. ScriptRes.Add('{');
  245. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  246. ScriptRes.Add(' . = ALIGN(16);');
  247. { add objectfiles, start with prt0 always }
  248. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0','',false))+'(.text)');
  249. while not ObjectFiles.Empty do
  250. begin
  251. s:=ObjectFiles.GetFirst;
  252. if s<>'' then
  253. begin
  254. ScriptRes.Add(' . = ALIGN(16);');
  255. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  256. end;
  257. end;
  258. ScriptRes.Add(' *(.text)');
  259. ScriptRes.Add(' *(.text.*)');
  260. ScriptRes.Add(' etext = . ;');
  261. ScriptRes.Add(' PROVIDE(_etext = .);');
  262. ScriptRes.Add(' . = ALIGN(0x200);');
  263. ScriptRes.Add(' }');
  264. ScriptRes.Add(' .data ALIGN(0x200) : {');
  265. ScriptRes.Add(' djgpp_first_ctor = . ;');
  266. ScriptRes.Add(' *(SORT(.ctors.*))');
  267. ScriptRes.Add(' *(.ctor)');
  268. ScriptRes.Add(' *(.ctors)');
  269. ScriptRes.Add(' djgpp_last_ctor = . ;');
  270. ScriptRes.Add(' djgpp_first_dtor = . ;');
  271. ScriptRes.Add(' *(SORT(.dtors.*))');
  272. ScriptRes.Add(' *(.dtor)');
  273. ScriptRes.Add(' *(.dtors)');
  274. ScriptRes.Add(' djgpp_last_dtor = . ;');
  275. ScriptRes.Add(' __environ = . ;');
  276. ScriptRes.Add(' PROVIDE(_environ = .);');
  277. ScriptRes.Add(' LONG(0)');
  278. ScriptRes.Add(' *(.data)');
  279. ScriptRes.Add(' *(.data.*)');
  280. ScriptRes.Add(' *(.fpc*)');
  281. ScriptRes.Add(' *(.gcc_exc)');
  282. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  283. ScriptRes.Add(' *(.eh_fram*)');
  284. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  285. ScriptRes.Add(' LONG(0)');
  286. ScriptRes.Add(' edata = . ; _edata = .;');
  287. ScriptRes.Add(' . = ALIGN(0x200);');
  288. ScriptRes.Add(' }');
  289. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  290. ScriptRes.Add(' {');
  291. ScriptRes.Add(' _object.2 = . ;');
  292. ScriptRes.Add(' . += 32 ;');
  293. ScriptRes.Add(' *(.bss)');
  294. ScriptRes.Add(' *(.bss.*)');
  295. ScriptRes.Add(' *(COMMON)');
  296. ScriptRes.Add(' end = . ; _end = .;');
  297. ScriptRes.Add(' . = ALIGN(0x200);');
  298. ScriptRes.Add(' }');
  299. ScriptRes.Add(' /* Stabs debugging sections. */');
  300. ScriptRes.Add(' .stab 0 : { *(.stab) }');
  301. ScriptRes.Add(' .stabstr 0 : { *(.stabstr) }');
  302. ScriptRes.Add(' /* DWARF 2 */');
  303. ScriptRes.Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  304. ScriptRes.Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  305. ScriptRes.Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  306. ScriptRes.Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  307. ScriptRes.Add(' .debug_line 0 : { *(.debug_line) }');
  308. ScriptRes.Add(' .debug_frame 0 : { *(.debug_frame) }');
  309. ScriptRes.Add(' .debug_str 0 : { *(.debug_str) }');
  310. ScriptRes.Add(' .debug_loc 0 : { *(.debug_loc) }');
  311. ScriptRes.Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  312. ScriptRes.Add(' }');
  313. { Write path to search libraries }
  314. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  315. while assigned(HPath) do
  316. begin
  317. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  318. HPath:=TCmdStrListItem(HPath.Next);
  319. end;
  320. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  321. while assigned(HPath) do
  322. begin
  323. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  324. HPath:=TCmdStrListItem(HPath.Next);
  325. end;
  326. { Write and Close response }
  327. ScriptRes.WriteToDisk;
  328. ScriptRes.Free;
  329. WriteScript:=True;
  330. end;
  331. function TExternalLinkerGo32v2.MakeExecutable:boolean;
  332. var
  333. binstr,
  334. cmdstr : TCmdStr;
  335. success : boolean;
  336. begin
  337. if not(cs_link_nolink in current_settings.globalswitches) then
  338. Message1(exec_i_linking,current_module.exefilename);
  339. { Write used files and libraries and our own ld script }
  340. WriteScript(false);
  341. WriteResponsefile(false);
  342. { Call linker }
  343. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  344. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  345. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  346. { Remove ReponseFile }
  347. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  348. begin
  349. DeleteFile(outputexedir+Info.ResName);
  350. DeleteFile(outputexedir+Info.ScriptName);
  351. end;
  352. MakeExecutable:=success; { otherwise a recursive call to link method }
  353. end;
  354. {$ifdef notnecessary}
  355. procedure TExternalLinkerGo32v2.postprocessexecutable(const n : string);
  356. type
  357. tcoffheader=packed record
  358. mach : word;
  359. nsects : word;
  360. time : longint;
  361. sympos : longint;
  362. syms : longint;
  363. opthdr : word;
  364. flag : word;
  365. end;
  366. tcoffsechdr=packed record
  367. name : array[0..7] of char;
  368. vsize : longint;
  369. rvaofs : longint;
  370. datalen : longint;
  371. datapos : longint;
  372. relocpos : longint;
  373. lineno1 : longint;
  374. nrelocs : word;
  375. lineno2 : word;
  376. flags : longint;
  377. end;
  378. psecfill=^TSecfill;
  379. TSecfill=record
  380. fillpos,
  381. fillsize : longint;
  382. next : psecfill;
  383. end;
  384. var
  385. f : file;
  386. coffheader : tcoffheader;
  387. firstsecpos,
  388. maxfillsize,
  389. l : longint;
  390. coffsec : tcoffsechdr;
  391. secroot,hsecroot : psecfill;
  392. zerobuf : pointer;
  393. begin
  394. { when -s is used quit, because there is no .exe }
  395. if cs_link_nolink in current_settings.globalswitches then
  396. exit;
  397. { open file }
  398. assign(f,n);
  399. {$push}{$I-}
  400. reset(f,1);
  401. if ioresult<>0 then
  402. Message1(execinfo_f_cant_open_executable,n);
  403. { read headers }
  404. seek(f,2048);
  405. blockread(f,coffheader,sizeof(tcoffheader));
  406. { read section info }
  407. maxfillsize:=0;
  408. firstsecpos:=0;
  409. secroot:=nil;
  410. for l:=1to coffheader.nSects do
  411. begin
  412. blockread(f,coffsec,sizeof(tcoffsechdr));
  413. if coffsec.datapos>0 then
  414. begin
  415. if secroot=nil then
  416. firstsecpos:=coffsec.datapos;
  417. new(hsecroot);
  418. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  419. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  420. hsecroot^.next:=secroot;
  421. secroot:=hsecroot;
  422. if secroot^.fillsize>maxfillsize then
  423. maxfillsize:=secroot^.fillsize;
  424. end;
  425. end;
  426. if firstsecpos>0 then
  427. begin
  428. l:=firstsecpos-filepos(f);
  429. if l>maxfillsize then
  430. maxfillsize:=l;
  431. end
  432. else
  433. l:=0;
  434. { get zero buffer }
  435. getmem(zerobuf,maxfillsize);
  436. fillchar(zerobuf^,maxfillsize,0);
  437. { zero from sectioninfo until first section }
  438. blockwrite(f,zerobuf^,l);
  439. { zero section alignments }
  440. while assigned(secroot) do
  441. begin
  442. seek(f,secroot^.fillpos);
  443. blockwrite(f,zerobuf^,secroot^.fillsize);
  444. hsecroot:=secroot;
  445. secroot:=secroot^.next;
  446. dispose(hsecroot);
  447. end;
  448. freemem(zerobuf,maxfillsize);
  449. close(f);
  450. {$pop}
  451. i:=ioresult;
  452. postprocessexecutable:=true;
  453. end;
  454. {$endif}
  455. {*****************************************************************************
  456. Initialize
  457. *****************************************************************************}
  458. initialization
  459. RegisterExternalLinker(system_i8086_msdos_info,TExternalLinkerGo32v2);
  460. RegisterTarget(system_i8086_msdos_info);
  461. end.