Press enter to see results or esc to cancel.

Loading Library

This procedure loads Turbo Pascal library (a collection of units). It adjust each unit so that it starts on a segment boundary. Actually, each compiled unit is a group of symbol tables.

Procedure LoadLibrary;
Var DummyVar: Pointer;

  Procedure LoadLibraryProc;
  Var LibraryFile: File;
      LibPtr, UnitPtr: PUnitHeader;

    Procedure CalculateUnitSegments;
    Var SymbolTable: TSymbolTable;

      Procedure AdjustUnitSeg (Size: Word);
      begin
        UnitPtr := Ptr (Seg (UnitPtr^) + (Size + $000F) shr 4, 0);
      end;

    begin
      AdjustUnitSeg (LibPtr^.JoinedBlockSize);
      For SymbolTable := st11 to stTypedConstantsReferences do
        begin
          LibPtr^.SymbolTableSegment [SymbolTable] := Seg (UnitPtr^);
          AdjustUnitSeg (LibPtr^.BlockSize [SymbolTable]);
        end;
    end;

  begin
    LastLibraryUnitSegment := 0;
    If CurrentFileName [0] = #0 then Exit;
    OpenFile (LibraryFile, CurrentFileName);
    LibPtr := ReadFile (LibraryFile);
    Close (LibraryFile);
    Repeat
      If LibPtr^.Signature <> UnitSignature then FileError (CurrentFileName, UnitFileFormatError);
      UnitPtr := LibPtr;
      CalculateUnitSegments;
      Include (LibPtr^.Flags, ufUnitFlag_8000);
      LibPtr^.PreviousLibraryUnitSegment := LastLibraryUnitSegment;
      LastLibraryUnitSegment := Seg (LibPtr^);
      LibPtr := UnitPtr;
    until LibPtr = HeapPtr;
  end;

begin
  SetErrorAddress (@LoadLibraryError);
  LoadLibraryProc;
end;