main.c 12 KB

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