other.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* other.c: Routines to handle automatic insertion of section contents
  2. Copyright (C) 2003 Sebastian Reichelt
  3. Copyright (C) 2003-2007 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "other.h"
  16. #include "../integers.h"
  17. #include "../manip.h"
  18. #include "../special.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. // Insert the data required for the _nostub comment format.
  22. BOOLEAN InsertNostubComments (SECTION *Section)
  23. {
  24. PROGRAM *Program = Section->Parent;
  25. // Export number.
  26. COUNT ExportCount = 0;
  27. // Highest export ID.
  28. COUNT HighestExport = -1;
  29. // Loop variables.
  30. SECTION *CurSection;
  31. SYMBOL *CurSymbol;
  32. // Get the number and the highest ID of _nostub comments.
  33. for_each (CurSection, Program->Sections)
  34. {
  35. for_each (CurSymbol, CurSection->Symbols)
  36. {
  37. if (CurSymbol->Exported && (!(strncmp (CurSymbol->Name, SYMPF_NOSTUB_DATA, sizeof (SYMPF_NOSTUB_DATA) - 1))))
  38. {
  39. OFFSET ExportNumber = GetExportNumber (CurSymbol->Name + (sizeof (SYMPF_NOSTUB_DATA_START) - 1));
  40. if (ExportNumber >= 0)
  41. {
  42. ExportCount++;
  43. if (ExportNumber > HighestExport)
  44. HighestExport = ExportNumber;
  45. }
  46. }
  47. }
  48. }
  49. // Do not output anything if no comments are specified.
  50. if (ExportCount)
  51. {
  52. // Allocate space for the comments.
  53. I1 *NewData = AllocateSpaceInSection (Section, 4 * ExportCount);
  54. // Apply the format documented in _nostub_comment_header.s.
  55. if (NewData)
  56. {
  57. COUNT CurrentExport;
  58. // We need this outer loop because the exports must be written out
  59. // in order (i.e. 0, 1, 6, 32768; not 1, 32768, 6, 0).
  60. for (CurrentExport = 0; CurrentExport <= HighestExport; CurrentExport++)
  61. {
  62. // For each section...
  63. for_each (CurSection, Program->Sections)
  64. {
  65. // For each symbol...
  66. for_each (CurSymbol, CurSection->Symbols)
  67. {
  68. // Check whether it belongs to the _nostub comment export we are looking for.
  69. if (CurSymbol->Exported && (!(strncmp (CurSymbol->Name, SYMPF_NOSTUB_DATA, sizeof (SYMPF_NOSTUB_DATA) - 1))))
  70. {
  71. OFFSET ExportNumber = GetExportNumber (CurSymbol->Name + (sizeof (SYMPF_NOSTUB_DATA_START) - 1));
  72. if (ExportNumber == CurrentExport)
  73. {
  74. // Insert the number of the export.
  75. WriteTI2 (*((TI2 *) NewData), ExportNumber);
  76. NewData += 2;
  77. {
  78. // Create a new reloc at the current location,
  79. // pointing to the exported symbol.
  80. RELOC *Reloc = calloc (1, sizeof (RELOC));
  81. if (Reloc)
  82. {
  83. Reloc->Parent = Section;
  84. Reloc->Location = NewData - Section->Data;
  85. NewData += ((Reloc->Size = 2));
  86. Reloc->Target.Symbol = CurSymbol;
  87. Reloc->Target.SymbolName = CurSymbol->Name;
  88. SetRelocProgramRelative (Reloc);
  89. InsertReloc (Section, Reloc);
  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 ((char *)NewData, Program->DataVarInfo->Name);
  121. else
  122. return FALSE;
  123. }
  124. return TRUE;
  125. }
  126. #endif /* DATA_VAR_SUPPORT */