ins_def.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. TargetSection->Referenced = TRUE;
  34. }
  35. else
  36. Error (NULL, "Out of memory.");
  37. return Reloc;
  38. }
  39. // Allocate and initialize a LIB_CALL_USER_DATA structure.
  40. BOOLEAN InitializeLibCallUserData (LIB_CALL_USER_DATA *UserData, PROGRAM *Program)
  41. {
  42. // Allocate space for dynamic user data.
  43. if (!(UserData->Libs = calloc ((UserData->LibCount = Program->Libraries.ReferencedCount), sizeof (LIB_DATA))))
  44. {
  45. Error (NULL, "Out of memory while inserting libraries.");
  46. return FALSE;
  47. }
  48. {
  49. LIBRARY *CurLibrary;
  50. LIB_DATA *CurLib = UserData->Libs;
  51. // Query info about the libraries.
  52. for_each (CurLibrary, Program->Libraries)
  53. {
  54. if (CurLibrary->Referenced)
  55. {
  56. // Set the library pointer.
  57. CurLib->Lib = CurLibrary;
  58. // Allocate space for the imports.
  59. if (!(CurLib->Functions = calloc (CurLibrary->Highest + 1, sizeof (LIB_FUNCTION_DATA))))
  60. {
  61. Error (NULL, "Out of memory while inserting libraries.");
  62. return FALSE;
  63. }
  64. CurLib++;
  65. }
  66. }
  67. }
  68. return TRUE;
  69. }
  70. // Finalize and deallocate a LIB_CALL_USER_DATA structure.
  71. void FinalizeLibCallUserData (LIB_CALL_USER_DATA *UserData)
  72. {
  73. LIB_DATA *CurLibPtr = UserData->Libs;
  74. OFFSET CurLib;
  75. for (CurLib = 0; CurLib < UserData->LibCount; CurLib++, CurLibPtr++)
  76. {
  77. LIB_FUNCTION_DATA *Functions = CurLibPtr->Functions;
  78. if (Functions)
  79. free (Functions);
  80. }
  81. free (UserData->Libs);
  82. }