main.c 15 KB

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