exp_tios.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* exp_tios.c: Routines to export to a TIOS file
  2. Copyright (C) 2002-2003 Sebastian Reichelt
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "exp_tios.h"
  15. #ifdef TIOS_SUPPORT
  16. #include "../manip.h"
  17. #include "../special.h"
  18. // Get the file size needed to export the program into a TIOS file.
  19. // Returns 0 on failure.
  20. // Call this function once to receive necessary error messages.
  21. SIZE GetTIOSFileSize (const PROGRAM *Program)
  22. {
  23. // Get a pointer to the main section.
  24. SECTION *MainSection = Program->MainSection;
  25. if (!MainSection)
  26. {
  27. Error (NULL, "No main section.");
  28. return 0;
  29. }
  30. {
  31. // Start with the size of the section.
  32. // Add 2 for the two null bytes at the beginning of the reloc table.
  33. SIZE Size = MainSection->Size + 2;
  34. if (!(IsEmpty (MainSection->Relocs)))
  35. {
  36. RELOC *Reloc;
  37. // Add the size needed for the relocs.
  38. for_each (Reloc, MainSection->Relocs)
  39. {
  40. // If this can be resolved to a calculator-dependent value, ignore the
  41. // reloc for now.
  42. if (IsPlainCalcBuiltin (Reloc))
  43. continue;
  44. // We can only emit 4-byte absolute relocs.
  45. if (Reloc->Relative || (Reloc->Size != 4))
  46. {
  47. Error (GetFileName (MainSection, Reloc->Location), "Cannot emit %ld byte %s reloc to `%s'.", (long) Reloc->Size, Reloc->Relative ? "relative" : "absolute", Reloc->Target.SymbolName);
  48. Size = 0;
  49. }
  50. if (Size)
  51. // A reloc takes 4 bytes: 2 bytes for the location and 2 bytes for the target offset.
  52. Size += 4;
  53. }
  54. }
  55. return Size;
  56. }
  57. }
  58. // Export the internal data structures into a TIOS file.
  59. BOOLEAN ExportTIOSFile (const PROGRAM *Program, EXP_FILE *File, SIZE FileSize, ProgramCalcs DestCalc)
  60. {
  61. // A simple macro to make the code more readable.
  62. #define FailWithError(Err...) ({ Error (Err); return FALSE; })
  63. const char *SectionFileName = NULL;
  64. OFFSET DataStart = 0;
  65. COUNT EmittedRelocCount = 0;
  66. // Get a pointer to the main section.
  67. SECTION *MainSection = Program->MainSection;
  68. if (!MainSection)
  69. FailWithError (NULL, "No main section.");
  70. SectionFileName = MainSection->FileName;
  71. // In nostub mode, execution simply starts at the beginning of
  72. // the first section. But ideally, startup code should always be
  73. // marked as such. When merging sections, the destination startup
  74. // number is kept, so in the end, the complete code is marked as
  75. // a startup section (which makes sense; it is startable).
  76. if (!(MainSection->StartupNumber || (Program->Type == PT_NOSTUB)))
  77. FailWithError (SectionFileName, "No startup section(s) defined.");
  78. if (!(MainSection->Data))
  79. FailWithError (SectionFileName, "No section contents.");
  80. // Write out the section contents first.
  81. DataStart = ExportTell (File);
  82. ExportWrite (File, MainSection->Data, MainSection->Size, 1);
  83. // Write out two zero bytes as a separator.
  84. ExportWriteTI2 (File, 0);
  85. if (!(IsEmpty (MainSection->Relocs)))
  86. {
  87. RELOC *Reloc;
  88. // Write out the relocation table.
  89. for (Reloc = GetLast (MainSection->Relocs); Reloc; Reloc = GetPrev (Reloc))
  90. {
  91. // Get the current file name for error messages.
  92. const char *CurFileName = GetFileName (MainSection, Reloc->Location);
  93. // If this can be resolved to a calculator-dependent value, write the
  94. // value into the section data.
  95. if (EmitCalcBuiltinValue (Reloc, DestCalc, File, FileSize, DataStart))
  96. continue;
  97. // We can only emit relocs with a target symbol in the same section.
  98. if (!(Reloc->Target.Symbol))
  99. FailWithError (CurFileName, "Unresolved reference to `%s'.", Reloc->Target.SymbolName);
  100. if (Reloc->Target.Symbol->Parent != MainSection)
  101. FailWithError (CurFileName, "Cannot emit reloc to `%s' in different section.", Reloc->Target.SymbolName);
  102. // We can only emit 4-byte absolute relocs.
  103. if (Reloc->Relative || (Reloc->Size != 4))
  104. FailWithError (CurFileName, "Cannot emit %ld byte %s reloc to `%s'.", (long) Reloc->Size, Reloc->Relative ? "relative" : "absolute", Reloc->Target.SymbolName);
  105. if (((I2) Reloc->Location) != Reloc->Location)
  106. FailWithError (CurFileName, "Cannot emit reloc outside of limited program range to `%s'.", Reloc->Target.SymbolName);
  107. {
  108. OFFSET TargetLocation = GetLocationOffset (MainSection, &(Reloc->Target)) + Reloc->FixedOffset;
  109. if (((I2) TargetLocation) != TargetLocation)
  110. FailWithError (CurFileName, "Cannot emit reloc to `%s' (Offset %ld; Location 0x%lX) outside of limited program range.", Reloc->Target.SymbolName, (long) (Reloc->Target.Offset + Reloc->FixedOffset), (long) (TargetLocation));
  111. // Everything seems to be correct.
  112. ExportWriteTI2 (File, TargetLocation);
  113. ExportWriteTI2 (File, Reloc->Location);
  114. // Increase the statistics.
  115. EmittedRelocCount++;
  116. }
  117. }
  118. }
  119. if ((!(IsEmpty (MainSection->ROMCalls))) && (!(MainSection->ROMCalls.Handled)))
  120. FailWithError (SectionFileName, "ROM calls are not supported in this mode.");
  121. if ((!(IsEmpty (MainSection->RAMCalls))) && (!(MainSection->RAMCalls.Handled)))
  122. FailWithError (SectionFileName, "RAM calls are not supported in this mode.");
  123. if ((!(IsEmpty (MainSection->LibCalls))) && (!(MainSection->LibCalls.Handled)))
  124. FailWithError (SectionFileName, "Library calls are not supported in this mode.");
  125. if (Program->OptimizeInfo->NativeRelocCount < EmittedRelocCount)
  126. Program->OptimizeInfo->NativeRelocCount = EmittedRelocCount;
  127. return TRUE;
  128. #undef FailWithError
  129. }
  130. #endif /* TIOS_SUPPORT */