main.c 14 KB

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