ins_def.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* ins_def.c: Common definitions for insertions
  2. Copyright (C) 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 "ins_def.h"
  15. #include "../manip.h"
  16. #include <stdlib.h>
  17. // Create a new program-relative reloc at a specific data location,
  18. // pointing to a specific location in the target section. Increase
  19. // NewData to point behind the reloc.
  20. RELOC *CreateProgramRelativeReloc (SECTION *Section, I1 **NewData, SECTION *TargetSection, OFFSET TargetLocation, SIZE Size)
  21. {
  22. RELOC *Reloc = calloc (1, sizeof (RELOC));
  23. if (Reloc)
  24. {
  25. Reloc->Parent = Section;
  26. Reloc->Location = *NewData - Section->Data;
  27. *NewData += ((Reloc->Size = Size));
  28. Reloc->Target.Symbol = TargetSection->SectionSymbol;
  29. Reloc->Target.SymbolName = TargetSection->SectionSymbol->Name;
  30. Reloc->Target.Offset = TargetLocation;
  31. SetRelocProgramRelative (Reloc);
  32. InsertReloc (Section, Reloc);
  33. }
  34. else
  35. Error (NULL, "Out of memory.");
  36. return Reloc;
  37. }
  38. // Allocate and initialize a LIB_CALL_USER_DATA structure.
  39. BOOLEAN InitializeLibCallUserData (LIB_CALL_USER_DATA *UserData, PROGRAM *Program)
  40. {
  41. // Allocate space for dynamic user data.
  42. if (!(UserData->Libs = calloc ((UserData->LibCount = Program->Libraries.ReferencedCount), sizeof (LIB_DATA))))
  43. {
  44. Error (NULL, "Out of memory while inserting libraries.");
  45. return FALSE;
  46. }
  47. {
  48. LIBRARY *CurLibrary;
  49. LIB_DATA *CurLib = UserData->Libs;
  50. // Query info about the libraries.
  51. for_each (CurLibrary, Program->Libraries)
  52. {
  53. if (CurLibrary->Referenced)
  54. {
  55. // Set the library pointer.
  56. CurLib->Lib = CurLibrary;
  57. // Allocate space for the imports.
  58. if (!(CurLib->Functions = calloc (CurLibrary->Highest + 1, sizeof (LIB_FUNCTION_DATA))))
  59. {
  60. Error (NULL, "Out of memory while inserting libraries.");
  61. return FALSE;
  62. }
  63. CurLib++;
  64. }
  65. }
  66. }
  67. return TRUE;
  68. }
  69. // Finalize and deallocate a LIB_CALL_USER_DATA structure.
  70. void FinalizeLibCallUserData (LIB_CALL_USER_DATA *UserData)
  71. {
  72. LIB_DATA *CurLibPtr = UserData->Libs;
  73. OFFSET CurLib;
  74. for (CurLib = 0; CurLib < UserData->LibCount; CurLib++, CurLibPtr++)
  75. {
  76. LIB_FUNCTION_DATA *Functions = CurLibPtr->Functions;
  77. if (Functions)
  78. free (Functions);
  79. }
  80. free (UserData->Libs);
  81. }