main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 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);
  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));
  214. CreateSectionMarkers (&(Program.Destructors), MergeAllSections (&Program, NULL, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE));
  215. #ifdef DATA_VAR_SUPPORT
  216. // If we want a separate data variable, merge all data
  217. // sections.
  218. if (DatVarInfo->Name)
  219. {
  220. if (OptInfo->RemoveUnused && (!(Program.Frozen)))
  221. {
  222. // Mark the section containing __main as referenced.
  223. MarkMainSection (&Program);
  224. // Remove unreferenced sections now, before constant merging
  225. // and section merging make it impossible.
  226. RemoveUnusedSections (&Program);
  227. // Reset the Referenced flags so we can do another GC pass
  228. // when the imports are done.
  229. ResetReferencedFlags (&Program);
  230. DoSpecialDump (1, "(early-cut)");
  231. }
  232. if (OptInfo->MergeConstants && (!(Program.Frozen)))
  233. {
  234. // Merge constants now, as we can't do it anymore after
  235. // the data variable has been built.
  236. MergeConstants (&Program);
  237. DoSpecialDump (1, "(const-merged)");
  238. }
  239. Program.DataSection = MergeAllSections (&Program, NULL, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE);
  240. // Mark the section as "handled" so it will not be merged
  241. // with code.
  242. if (Program.DataSection)
  243. Program.DataSection->Handled = TRUE;
  244. else
  245. Warning (NULL, "No data to put into external variable.");
  246. }
  247. #endif /* DATA_VAR_SUPPORT */
  248. DoDump (2);
  249. // Create all global imports needed by this program.
  250. CreateSpecialGlobalImports (&Program);
  251. DoDump (3);
  252. // Resolve all remaining global imports. Usually, global imports
  253. // are processed directly, but if a symbol contains an inverted
  254. // condition, we have to wait until we really know that no such
  255. // global import exists.
  256. ResolveRemainingGlobalImports (&Program);
  257. // Resolve the relocs from the newly imported archive members.
  258. if (ResolveRelocs (&Program, TRUE))
  259. {
  260. // No more startup sections may be added.
  261. // So the program entry point is fixed from now on.
  262. {
  263. SECTION *FirstSection = GetFirst (Program.Sections);
  264. if (FirstSection)
  265. {
  266. Program.EntryPoint.Symbol = FirstSection->SectionSymbol;
  267. Program.EntryPoint.SymbolName = FirstSection->SectionSymbol->Name;
  268. #ifdef FARGO_SUPPORT
  269. // Fargo programs use the location in front
  270. // of the two size bytes as the entry point.
  271. if (Program.Type == PT_FARGO)
  272. Program.EntryPoint.Offset -= 2;
  273. #endif /* FARGO_SUPPORT */
  274. }
  275. }
  276. DoDump (4);
  277. // Now that all relocs have been resolved, there is no chance
  278. // that some previously unknown archive member will add new imports.
  279. CheckAllGlobalImports (&Program);
  280. if (OptInfo->OptimizeRelocs)
  281. {
  282. // Optimize relocs. This should not have any effect on the program.
  283. OptimizeRelocs (&Program);
  284. DoSpecialDump (4, "(optimized)");
  285. }
  286. if (OptInfo->RemoveUnused && (!(Program.Frozen)))
  287. {
  288. // Remove unreferenced sections.
  289. RemoveUnusedSections (&Program);
  290. DoSpecialDump (4, "(cut)");
  291. }
  292. if (!DatVarInfo->Name && OptInfo->MergeConstants && (!(Program.Frozen)))
  293. {
  294. // Merge constants.
  295. MergeConstants (&Program);
  296. DoSpecialDump (4, "(const-merged)");
  297. }
  298. if (OptInfo->ReorderSections && (!(Program.Frozen)))
  299. {
  300. // Reorder sections.
  301. ReorderSections (&Program);
  302. DoSpecialDump (4, "(reordered)");
  303. }
  304. #ifdef FLASH_OS_SUPPORT
  305. if (Program.Type == PT_FLASH_OS)
  306. {
  307. // Flash OS export: merge startup and normal sections separately.
  308. // The resulting two parts are merged later, padding the first
  309. // part to the full 24 KB of the OS startup area (base 1)
  310. // + the 8 KB corresponding to the read protected FlashROM
  311. // area.
  312. // Thus, the startup sections end up in the OS startup area
  313. // (base 1) and the non-startup areas end up in the OS main
  314. // area (base 2), the big OS code part.
  315. // Merge all startup sections.
  316. MergeAllSections (&Program, NULL, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE);
  317. // Merge all normal sections.
  318. MergeAllSections (&Program, NULL, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE);
  319. }
  320. #endif /* FLASH_OS_SUPPORT */
  321. // Merge all initialized sections.
  322. Program.MainSection = MergeAllSections (&Program, NULL, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
  323. // Merge all unhandled sections.
  324. Program.MainSection = MergeAllSections (&Program, Program.MainSection, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
  325. // Record the size of the BSS section.
  326. if (Program.BSSSection)
  327. OptInfo->BSSSize = Program.BSSSection->Size;
  328. if (Program.MainSection && Program.Library && (Program.MainSection->StartupNumber > 0))
  329. Warning (Program.MainSection->FileName, "Library only contains program startup sections.");
  330. DoDump (5);
  331. // Fix the code (if that is still possible).
  332. FixCode (&Program);
  333. DoDump (6);
  334. // Resolve the relocs pointing to built-in symbols.
  335. Program.ResolveAllBuiltins = TRUE;
  336. // Resolve requested automatic insertions.
  337. if (ResolveRelocs (&Program, TRUE))
  338. {
  339. DoDump (7);
  340. // Do one more optimization pass for relocs to automatic insertions.
  341. FixCode (&Program);
  342. // Remove relocs where possible.
  343. if (FixupRelativeRelocs (&Program))
  344. {
  345. DoDump (8);
  346. if (Program.Calcs)
  347. {
  348. // Export the program to the appropriate files.
  349. if (ExportProgram (&Program, GetOutputFile, FinalizeOutputFile))
  350. {
  351. Result = RESULT_OK;
  352. #ifndef TARGET_EMBEDDED
  353. #ifdef ENABLE_STATS
  354. #include "main_vbs.inc"
  355. #endif /* ENABLE_STATS */
  356. #endif /* !TARGET_EMBEDDED */
  357. }
  358. else
  359. Result = RESULT_EXPORT_ERROR;
  360. }
  361. else
  362. Error (NULL, "No target calculators specified.");
  363. }
  364. }
  365. }
  366. }
  367. }
  368. Cleanup: ATTRIBUTE_UNUSED
  369. // Final Cleanup.
  370. FreeProgram (&Program);
  371. return Result;
  372. }