main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* main.c: Main entry point for ld-tigcc, handling the command line input
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2004-2005 Kevin Kofler
  4. Copyright (C) 2004 Billy Charvet
  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, or (at your option)
  8. 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 Foundation,
  15. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #include "generic.h"
  17. #include "intrface.h"
  18. #include "data.h"
  19. #include "manip.h"
  20. #include "constmrg.h"
  21. #include "gcunused.h"
  22. #include "reorder.h"
  23. #include "formats/ar.h"
  24. #include "import/import.h"
  25. #include "import/imp_ar.h"
  26. #include "export/export.h"
  27. #include "special.h"
  28. #ifdef ENABLE_DUMP
  29. #include "dump.h"
  30. #endif /* ENABLE_DUMP */
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #define RESULT_OK 0
  36. #define RESULT_GENERAL_ERROR 1
  37. #define RESULT_EXPORT_ERROR 2
  38. #define RESULT_STRANGE_ERROR 3
  39. // When compiling a DLL, the caller needs to be able to identify the version
  40. // of the function prototypes.
  41. #ifdef TARGET_DLL
  42. EXP_GET_INTERFACE_VERSION ()
  43. {
  44. return CURRENT_INTERFACE_VERSION;
  45. }
  46. #endif /* TARGET_DLL */
  47. // Main entry point.
  48. #ifdef TARGET_EMBEDDED
  49. ERROR_FUNCTION ErrorFunction;
  50. void Error_Internal (const char *FileName, const char *Text)
  51. {
  52. ErrorFunction (FileName, Text, MT_ERROR);
  53. }
  54. void Warning_Internal (const char *FileName, const char *Text)
  55. {
  56. ErrorFunction (FileName, Text, MT_WARNING);
  57. }
  58. EXP_LINK_FILES ()
  59. {
  60. #define OptInfo OptimizeInfo
  61. #ifdef DATA_VAR_SUPPORT
  62. #define DatVarInfo DataVarInfo
  63. #endif /* DATA_VAR_SUPPORT */
  64. const char **CurFile;
  65. BOOLEAN IsArchive;
  66. #else /* !TARGET_EMBEDDED */
  67. int main (int ArgCount, const char **Args)
  68. {
  69. OPTIMIZE_INFO _OptimizeInfo;
  70. #define OptInfo (&_OptimizeInfo)
  71. #ifdef DATA_VAR_SUPPORT
  72. DATA_VAR_INFO _DataVarInfo;
  73. #define DatVarInfo (&_DataVarInfo)
  74. #endif /* DATA_VAR_SUPPORT */
  75. int CurArg;
  76. BOOLEAN OmitBSSInitialization = FALSE;
  77. #ifdef ENABLE_STATS
  78. BOOLEAN DisplayStats = FALSE;
  79. #endif /* ENABLE_STATS */
  80. #ifdef DATA_VAR_SUPPORT
  81. char DataVarString[MAX_NAME_LEN+1+MAX_NAME_LEN+1];
  82. #endif /* DATA_VAR_SUPPORT */
  83. #endif /* !TARGET_EMBEDDED */
  84. #ifdef ENABLE_DUMP
  85. #define DUMP_COUNT 9
  86. BOOLEAN Dump [DUMP_COUNT] = {[0 ... (DUMP_COUNT - 1)] = FALSE};
  87. #define DoDump(DumpNumber) (_DoDump (DumpNumber, ""))
  88. #define DoSpecialDump(DumpNumber,SpecialText) (_DoDump (DumpNumber, " " SpecialText))
  89. #define _DoDump(DumpNumber,SpecialText) \
  90. ({ if (((DumpNumber) >= 0) && ((DumpNumber) < DUMP_COUNT) && (Dump [(DumpNumber)])) \
  91. { \
  92. printf ("*** DUMP " #DumpNumber SpecialText " ***\n"); \
  93. DumpProgram (stdout, NULL, &Program); \
  94. printf ("\n"); \
  95. } })
  96. #else /* !ENABLE_DUMP */
  97. #define DoDump(DumpNumber) ((void) 0)
  98. #define DoSpecialDump(DumpNumber,SpecialText) ((void) 0)
  99. #endif /* !ENABLE_DUMP */
  100. int Result = RESULT_GENERAL_ERROR;
  101. PROGRAM Program;
  102. // Check the sizes of basic integer types.
  103. if (sizeof (I1) != 1 || sizeof (I2) != 2 || sizeof (I4) != 4 || sizeof (SI1) != 1 || sizeof (SI2) != 2 || sizeof (SI4) != 4 || sizeof (OFFSET) < sizeof (SI4))
  104. {
  105. Error (NULL, "Generic type size error!");
  106. return RESULT_STRANGE_ERROR;
  107. }
  108. // Initialize.
  109. memset (&Program, 0, sizeof (Program));
  110. Program.EntryPoint.SymbolName = "__entry_point";
  111. #ifdef TARGET_EMBEDDED
  112. ErrorFunction = ErrorMessage;
  113. if (NativeMode)
  114. Program.Type = PT_NATIVE;
  115. #else /* !TARGET_EMBEDDED */
  116. memset (&_OptimizeInfo, 0, sizeof (_OptimizeInfo));
  117. #ifdef DATA_VAR_SUPPORT
  118. memset (&_DataVarInfo, 0, sizeof (_DataVarInfo));
  119. #endif /* DATA_VAR_SUPPORT */
  120. memset (ProgramName, 0, MAX_NAME_LEN + 1);
  121. memset (ProgramFolder, 0, MAX_NAME_LEN + 1);
  122. strcpy (ProgramFolder, "main");
  123. #endif /* !TARGET_EMBEDDED */
  124. Program.OptimizeInfo = OptInfo;
  125. #ifdef DATA_VAR_SUPPORT
  126. Program.DataVarInfo = DatVarInfo;
  127. #endif /* DATA_VAR_SUPPORT */
  128. #ifdef TARGET_EMBEDDED
  129. if (Fargo)
  130. {
  131. #ifdef FARGO_SUPPORT
  132. Program.Type = PT_FARGO;
  133. Program.Calcs |= CALC_TI92;
  134. Warning (NULL, "Fargo support in TIGCC is experimental.");
  135. #else /* !FARGO_SUPPORT */
  136. Error (NULL, "Fargo support is not compiled in.");
  137. goto Cleanup;
  138. #endif /* !FARGO_SUPPORT */
  139. }
  140. if (FlashOS)
  141. {
  142. #ifdef FLASH_OS_SUPPORT
  143. Program.Type = PT_FLASH_OS;
  144. Warning (NULL, "Flash OS support in TIGCC is experimental.");
  145. #else /* !FLASH_OS_SUPPORT */
  146. Error (NULL, "Flash OS support is not compiled in.");
  147. goto Cleanup;
  148. #endif /* !FLASH_OS_SUPPORT */
  149. }
  150. CurFile = ObjectFiles;
  151. IsArchive = FALSE;
  152. while (CurFile && (*CurFile))
  153. {
  154. FILE *File = fopen (*CurFile, "rb");
  155. if (File)
  156. {
  157. SIZE Size;
  158. fseek (File, 0, SEEK_END);
  159. Size = ftell (File);
  160. rewind (File);
  161. {
  162. I1 *Data = malloc (Size);
  163. if (Data)
  164. {
  165. if (fread (Data, Size, 1, File) == 1)
  166. {
  167. if (IsArchive)
  168. AddArchiveFile (&Program, Data, Size, *CurFile);
  169. else
  170. ImportObjectFile (&Program, Data, Size, *CurFile);
  171. }
  172. else
  173. Error (*CurFile, "Unable to read file.");
  174. if (!IsArchive)
  175. free (Data);
  176. }
  177. else
  178. Error (*CurFile, "Not enough memory to load file.");
  179. }
  180. fclose (File);
  181. }
  182. else
  183. Error (*CurFile, "Unable to open file.");
  184. if ((!IsArchive) && (!(*(CurFile + 1))))
  185. {
  186. CurFile = ArchiveFiles;
  187. IsArchive = TRUE;
  188. }
  189. else
  190. CurFile++;
  191. }
  192. #else /* !TARGET_EMBEDDED */
  193. #include "main_opt.inc"
  194. #endif /* !TARGET_EMBEDDED */
  195. if (IsEmpty (Program.Sections))
  196. Error (NULL, "Cannot create empty program.");
  197. else
  198. {
  199. DoDump (0);
  200. // Connect all relocs to the appropriate symbols, or convert them into
  201. // ROM/RAM calls. Also import objects from archives.
  202. // Report all unresolved references.
  203. if (ResolveRelocs (&Program, TRUE))
  204. {
  205. DoDump (1);
  206. // Merge all zero-data and uninitialized sections.
  207. Program.BSSSection = MergeAllSections (&Program, NULL, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, DI_NONE);
  208. // As a dirty trick, allow the caller to skip the BSS
  209. // initialization entirely.
  210. if (Program.BSSSection && OmitBSSInitialization)
  211. Program.BSSSection->Initialized = FALSE;
  212. // Extract, merge, and mark constructor and destructor sections.
  213. CreateSectionMarkers (&(Program.Constructors), MergeAllSections (&Program, NULL, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, DI_NONE));
  214. CreateSectionMarkers (&(Program.Destructors), MergeAllSections (&Program, NULL, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, DI_NONE));
  215. #ifdef DEBUGGING_INFO_SUPPORT
  216. // If we want debugging information, merge all debugging information
  217. // sections of each type.
  218. {
  219. DebuggingInfoTypes i;
  220. for (i = 1; i < DI_LAST; i++)
  221. {
  222. Program.DebuggingInfoSection[i] = MergeAllSections (&Program, NULL, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, i + 1);
  223. if (Program.DebuggingInfoSection[i])
  224. {
  225. Program.DebuggingInfoSection[i]->Handled = TRUE;
  226. Program.HaveDebuggingInfo = TRUE;
  227. }
  228. }
  229. }
  230. #endif /* DEBUGGING_INFO_SUPPORT */
  231. #ifdef DATA_VAR_SUPPORT
  232. // If we want a separate data variable, merge all data
  233. // sections.
  234. if (DatVarInfo->Name)
  235. {
  236. if (OptInfo->RemoveUnused && (!(Program.Frozen)))
  237. {
  238. // Mark the section containing __main as referenced.
  239. MarkMainSection (&Program);
  240. // Remove unreferenced sections now, before constant merging
  241. // and section merging make it impossible.
  242. RemoveUnusedSections (&Program);
  243. // Reset the Referenced flags so we can do another GC pass
  244. // when the imports are done.
  245. ResetReferencedFlags (&Program);
  246. #ifdef DEBUGGING_INFO_SUPPORT
  247. if (Program.HaveDebuggingInfo)
  248. {
  249. // Merge all unused sections into a .deleted section.
  250. Program.DebuggingInfoSection[0] = MergeAllSections (&Program, NULL, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 1);
  251. if (Program.DebuggingInfoSection[0])
  252. Program.DebuggingInfoSection[0]->Handled = TRUE;
  253. }
  254. #endif /* DEBUGGING_INFO_SUPPORT */
  255. DoSpecialDump (1, "(early-cut)");
  256. }
  257. if (OptInfo->MergeConstants && (!(Program.Frozen)))
  258. {
  259. // Merge constants now, as we can't do it anymore after
  260. // the data variable has been built.
  261. MergeConstants (&Program);
  262. DoSpecialDump (1, "(const-merged)");
  263. }
  264. Program.DataSection = MergeAllSections (&Program, NULL, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, DI_NONE);
  265. // Mark the section as "handled" so it will not be merged
  266. // with code.
  267. if (Program.DataSection)
  268. Program.DataSection->Handled = TRUE;
  269. else
  270. Warning (NULL, "No data to put into external variable.");
  271. }
  272. #endif /* DATA_VAR_SUPPORT */
  273. DoDump (2);
  274. // Create all global imports needed by this program.
  275. CreateSpecialGlobalImports (&Program);
  276. DoDump (3);
  277. // Resolve all remaining global imports. Usually, global imports
  278. // are processed directly, but if a symbol contains an inverted
  279. // condition, we have to wait until we really know that no such
  280. // global import exists.
  281. ResolveRemainingGlobalImports (&Program);
  282. // Resolve the relocs from the newly imported archive members.
  283. if (ResolveRelocs (&Program, TRUE))
  284. {
  285. // No more startup sections may be added.
  286. // So the program entry point is fixed from now on.
  287. {
  288. SECTION *FirstSection = GetFirst (Program.Sections);
  289. if (FirstSection)
  290. {
  291. Program.EntryPoint.Symbol = FirstSection->SectionSymbol;
  292. Program.EntryPoint.SymbolName = FirstSection->SectionSymbol->Name;
  293. #ifdef FARGO_SUPPORT
  294. // Fargo programs use the location in front
  295. // of the two size bytes as the entry point.
  296. if (Program.Type == PT_FARGO)
  297. Program.EntryPoint.Offset -= 2;
  298. #endif /* FARGO_SUPPORT */
  299. }
  300. }
  301. DoDump (4);
  302. // Now that all relocs have been resolved, there is no chance
  303. // that some previously unknown archive member will add new imports.
  304. CheckAllGlobalImports (&Program);
  305. if (OptInfo->OptimizeRelocs)
  306. {
  307. // Optimize relocs. This should not have any effect on the program.
  308. OptimizeRelocs (&Program);
  309. DoSpecialDump (4, "(optimized)");
  310. }
  311. if (OptInfo->RemoveUnused && (!(Program.Frozen)))
  312. {
  313. // Remove unreferenced sections.
  314. RemoveUnusedSections (&Program);
  315. #ifdef DEBUGGING_INFO_SUPPORT
  316. if (Program.HaveDebuggingInfo)
  317. {
  318. // Merge all unused sections into a .deleted section.
  319. // Remove the section from early-cutting if we have one.
  320. if (Program.DebuggingInfoSection[0])
  321. Program.DebuggingInfoSection[0]->Handled = FALSE;
  322. Program.DebuggingInfoSection[0] = MergeAllSections (&Program, Program.DebuggingInfoSection[0], TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 1);
  323. if (Program.DebuggingInfoSection[0])
  324. Program.DebuggingInfoSection[0]->Handled = TRUE;
  325. }
  326. #endif /* DEBUGGING_INFO_SUPPORT */
  327. DoSpecialDump (4, "(cut)");
  328. }
  329. if (!DatVarInfo->Name && OptInfo->MergeConstants && (!(Program.Frozen)))
  330. {
  331. // Merge constants.
  332. MergeConstants (&Program);
  333. DoSpecialDump (4, "(const-merged)");
  334. }
  335. if (OptInfo->ReorderSections && (!(Program.Frozen)))
  336. {
  337. // Reorder sections.
  338. ReorderSections (&Program);
  339. DoSpecialDump (4, "(reordered)");
  340. }
  341. #ifdef FLASH_OS_SUPPORT
  342. if (Program.Type == PT_FLASH_OS)
  343. {
  344. // Flash OS export: merge startup and normal sections separately.
  345. // The resulting two parts are merged later, padding the first
  346. // part to the full 24 KB of the OS startup area (base 1)
  347. // + the 8 KB corresponding to the read protected FlashROM
  348. // area.
  349. // Thus, the startup sections end up in the OS startup area
  350. // (base 1) and the non-startup areas end up in the OS main
  351. // area (base 2), the big OS code part.
  352. // Merge all startup sections.
  353. MergeAllSections (&Program, NULL, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, DI_NONE);
  354. // Merge all normal sections.
  355. MergeAllSections (&Program, NULL, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, DI_NONE);
  356. }
  357. #endif /* FLASH_OS_SUPPORT */
  358. // Merge all initialized sections.
  359. Program.MainSection = MergeAllSections (&Program, NULL, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, DI_NONE);
  360. // Merge all unhandled sections.
  361. Program.MainSection = MergeAllSections (&Program, Program.MainSection, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, DI_NONE);
  362. // Record the size of the BSS section.
  363. if (Program.BSSSection)
  364. OptInfo->BSSSize = Program.BSSSection->Size;
  365. if (Program.MainSection && Program.Library && (Program.MainSection->StartupNumber > 0))
  366. Warning (Program.MainSection->FileName, "Library only contains program startup sections.");
  367. DoDump (5);
  368. // Fix the code (if that is still possible).
  369. FixCode (&Program);
  370. DoDump (6);
  371. // Resolve the relocs pointing to built-in symbols.
  372. Program.ResolveAllBuiltins = TRUE;
  373. // Resolve requested automatic insertions.
  374. if (ResolveRelocs (&Program, TRUE))
  375. {
  376. DoDump (7);
  377. // Do one more optimization pass for relocs to automatic insertions.
  378. FixCode (&Program);
  379. // Remove relocs where possible.
  380. if (FixupRelativeRelocs (&Program))
  381. {
  382. DoDump (8);
  383. if (Program.Calcs)
  384. {
  385. // Export the program to the appropriate files.
  386. if (ExportProgram (&Program, GetOutputFile, FinalizeOutputFile))
  387. {
  388. Result = RESULT_OK;
  389. #ifndef TARGET_EMBEDDED
  390. #ifdef ENABLE_STATS
  391. #include "main_vbs.inc"
  392. #endif /* ENABLE_STATS */
  393. #endif /* !TARGET_EMBEDDED */
  394. }
  395. else
  396. Result = RESULT_EXPORT_ERROR;
  397. }
  398. else
  399. Error (NULL, "No target calculators specified.");
  400. }
  401. }
  402. }
  403. }
  404. }
  405. Cleanup: ATTRIBUTE_UNUSED
  406. // Final Cleanup.
  407. FreeProgram (&Program);
  408. return Result;
  409. }