dxeload.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Pierre Muller,
  5. member of the Free Pascal development team.
  6. Unit to Load DXE files for Go32V2
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************
  13. }
  14. Unit dxeload;
  15. interface
  16. const
  17. DXE_MAGIC = $31455844;
  18. type
  19. dxe_header = record
  20. magic,
  21. symbol_offset,
  22. element_size,
  23. nrelocs : cardinal;
  24. end;
  25. function dxe_load(filename : string) : pointer;
  26. implementation
  27. function dxe_load(filename : string) : pointer;
  28. {
  29. Copyright (C) 1995 Charles Sandmann ([email protected])
  30. translated to Free Pascal by Pierre Muller
  31. }
  32. type
  33. { to avoid range check problems }
  34. pointer_array = array[0..maxlongint] of pointer;
  35. tpa = ^pointer_array;
  36. plongint = ^longint;
  37. pcardinal = ^cardinal;
  38. ppointer = ^pointer;
  39. var
  40. dh : dxe_header;
  41. data : pchar;
  42. f : file;
  43. relocs : tpa;
  44. i : longint;
  45. addr : pcardinal;
  46. begin
  47. dxe_load:=nil;
  48. { open the file }
  49. assign(f,filename);
  50. {$I-}
  51. reset(f,1);
  52. {$I+}
  53. { quit if no file !! }
  54. if ioresult<>0 then
  55. exit;
  56. { load the header }
  57. blockread(f,dh,sizeof(dxe_header),i);
  58. if (i<>sizeof(dxe_header)) or (dh.magic<>DXE_MAGIC) then
  59. begin
  60. close(f);
  61. exit;
  62. end;
  63. { get memory for code }
  64. getmem(data,dh.element_size);
  65. if data=nil then
  66. exit;
  67. { get memory for relocations }
  68. getmem(relocs,dh.nrelocs*sizeof(pointer));
  69. if relocs=nil then
  70. begin
  71. freemem(data,dh.element_size);
  72. exit;
  73. end;
  74. { copy code }
  75. blockread(f,data^,dh.element_size);
  76. blockread(f,relocs^,dh.nrelocs*sizeof(pointer));
  77. close(f);
  78. { relocate internal references }
  79. for i:=0 to dh.nrelocs-1 do
  80. begin
  81. cardinal(addr):=cardinal(data)+cardinal(relocs^[i]);
  82. addr^:=addr^+cardinal(data);
  83. end;
  84. FreeMem(relocs,dh.nrelocs*sizeof(pointer));
  85. dxe_load:=pointer( dh.symbol_offset + cardinal(data));
  86. end;
  87. end.
  88. {
  89. $Log$
  90. Revision 1.4 2001-07-23 09:52:38 marco
  91. * Fixed an unnecessary memleak.
  92. Revision 1.3 2000/12/16 15:57:52 jonas
  93. * avoid the longint + cardinal to int64 conversion (merged Pierre's patch)
  94. Revision 1.2 2000/07/13 11:33:40 michael
  95. + removed logs
  96. }