other.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* other.c: Routines to handle automatic insertion of section contents
  2. Copyright (C) 2003 Sebastian Reichelt, Kevin Kofler
  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 "other.h"
  15. #include "../integers.h"
  16. #include "../manip.h"
  17. #include "../special.h"
  18. #include <stdlib.h>
  19. #include <string.h>
  20. // Insert the data required for the _nostub comment format.
  21. BOOLEAN InsertNostubComments (SECTION *Section)
  22. {
  23. PROGRAM *Program = Section->Parent;
  24. // Export number.
  25. COUNT ExportCount = 0;
  26. // Highest export ID.
  27. COUNT HighestExport = -1;
  28. // Loop variables.
  29. SECTION *CurSection;
  30. SYMBOL *CurSymbol;
  31. // Get the number and the highest ID of _nostub comments.
  32. for_each (CurSection, Program->Sections)
  33. {
  34. for_each (CurSymbol, CurSection->Symbols)
  35. {
  36. if (CurSymbol->Exported && (!(strncmp (CurSymbol->Name, SYMPF_NOSTUB_DATA, sizeof (SYMPF_NOSTUB_DATA) - 1))))
  37. {
  38. OFFSET ExportNumber = GetExportNumber (CurSymbol->Name + (sizeof (SYMPF_NOSTUB_DATA_START) - 1));
  39. if (ExportNumber >= 0)
  40. {
  41. ExportCount++;
  42. if (ExportNumber > HighestExport)
  43. HighestExport = ExportNumber;
  44. }
  45. }
  46. }
  47. }
  48. // Do not output anything if no comments are specified.
  49. if (ExportCount)
  50. {
  51. // Allocate space for the comments.
  52. I1 *NewData = AllocateSpaceInSection (Section, 4 * ExportCount);
  53. // Apply the format documented in _nostub_comment_header.s.
  54. if (NewData)
  55. {
  56. COUNT CurrentExport;
  57. // We need this outer loop because the exports must be written out
  58. // in order (i.e. 0, 1, 6, 32768; not 1, 32768, 6, 0).
  59. for (CurrentExport = 0; CurrentExport <= HighestExport; CurrentExport++)
  60. {
  61. // For each section...
  62. for_each (CurSection, Program->Sections)
  63. {
  64. // For each symbol...
  65. for_each (CurSymbol, CurSection->Symbols)
  66. {
  67. // Check whether it belongs to the _nostub comment export we are looking for.
  68. if (CurSymbol->Exported && (!(strncmp (CurSymbol->Name, SYMPF_NOSTUB_DATA, sizeof (SYMPF_NOSTUB_DATA) - 1))))
  69. {
  70. OFFSET ExportNumber = GetExportNumber (CurSymbol->Name + (sizeof (SYMPF_NOSTUB_DATA_START) - 1));
  71. if (ExportNumber == CurrentExport)
  72. {
  73. // Insert the number of the export.
  74. WriteTI2 (*((TI2 *) NewData), ExportNumber);
  75. NewData += 2;
  76. {
  77. // Create a new reloc at the current location,
  78. // pointing to the exported symbol.
  79. RELOC *Reloc = calloc (1, sizeof (RELOC));
  80. if (Reloc)
  81. {
  82. Reloc->Parent = Section;
  83. Reloc->Location = NewData - Section->Data;
  84. NewData += ((Reloc->Size = 2));
  85. Reloc->Target.Symbol = CurSymbol;
  86. Reloc->Target.SymbolName = CurSymbol->Name;
  87. SetRelocProgramRelative (Reloc);
  88. InsertReloc (Section, Reloc);
  89. CurSection->Referenced = TRUE;
  90. }
  91. else
  92. {
  93. Error (NULL, "Out of memory while inserting comments.");
  94. return FALSE;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. else
  104. return FALSE;
  105. }
  106. return TRUE;
  107. }
  108. #ifdef DATA_VAR_SUPPORT
  109. // Insert the name of the data variable specified for the program.
  110. BOOLEAN InsertDataVarName (SECTION *Section)
  111. {
  112. PROGRAM *Program = Section->Parent;
  113. // Do not output anything if no data variable is specified.
  114. if (Program->DataVarInfo->Name)
  115. {
  116. // Allocate space for the string.
  117. I1 *NewData = AllocateSpaceInSection (Section, strlen (Program->DataVarInfo->Name) + 1);
  118. // Copy the string into the allocated space.
  119. if (NewData)
  120. strcpy (NewData, Program->DataVarInfo->Name);
  121. else
  122. return FALSE;
  123. }
  124. return TRUE;
  125. }
  126. #endif /* DATA_VAR_SUPPORT */