From c13517634fb6065309eda729db210ea2af3b1e32 Mon Sep 17 00:00:00 2001 From: florian Date: Wed, 8 Jun 2022 22:59:09 +0200 Subject: [PATCH] * patch by Rika: Replace some instances of manual work with endianness with cryptic oneliners, resolves #39719 --- compiler/ogomf.pas | 203 +++++++++-------------------- compiler/omfbase.pas | 142 ++++++-------------- compiler/owomflib.pas | 6 +- compiler/symtype.pas | 19 +-- compiler/utils/ppuutils/ppudump.pp | 6 +- 5 files changed, 118 insertions(+), 258 deletions(-) diff --git a/compiler/ogomf.pas b/compiler/ogomf.pas index 4084f82cdc..1bf5119b85 100644 --- a/compiler/ogomf.pas +++ b/compiler/ogomf.pas @@ -1375,18 +1375,14 @@ implementation NextOfs:=RawRecord.WriteIndexedRef(0,SegIndex); if Is32BitLEDATA then begin - RawRecord.RawData[NextOfs]:=Byte(ChunkStart); - RawRecord.RawData[NextOfs+1]:=Byte(ChunkStart shr 8); - RawRecord.RawData[NextOfs+2]:=Byte(ChunkStart shr 16); - RawRecord.RawData[NextOfs+3]:=Byte(ChunkStart shr 24); + unaligned(PUint32(@RawRecord.RawData[NextOfs{..NextOfs+3}])^):=NtoLE(uint32(ChunkStart)); Inc(NextOfs,4); end else begin if ChunkStart>$ffff then internalerror(2018052201); - RawRecord.RawData[NextOfs]:=Byte(ChunkStart); - RawRecord.RawData[NextOfs+1]:=Byte(ChunkStart shr 8); + unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^):=NtoLE(uint16(ChunkStart)); Inc(NextOfs,2); end; sec.data.read(RawRecord.RawData[NextOfs], ChunkLen); @@ -2102,18 +2098,14 @@ implementation begin if (NextOfs+3)>=RawRec.RecordLength then internalerror(2015040512); - EnumeratedDataOffset := RawRec.RawData[NextOfs]+ - (RawRec.RawData[NextOfs+1] shl 8)+ - (RawRec.RawData[NextOfs+2] shl 16)+ - (RawRec.RawData[NextOfs+3] shl 24); + EnumeratedDataOffset := LEtoN(unaligned(PUint32(@RawRec.RawData[NextOfs{..NextOfs+3}])^)); Inc(NextOfs,4); end else begin if (NextOfs+1)>=RawRec.RecordLength then internalerror(2015040513); - EnumeratedDataOffset := RawRec.RawData[NextOfs]+ - (RawRec.RawData[NextOfs+1] shl 8); + EnumeratedDataOffset := LEtoN(unaligned(PUint16(@RawRec.RawData[NextOfs{..NextOfs+1}])^)); Inc(NextOfs,2); end; BlockLength:=RawRec.RecordLength-NextOfs-1; @@ -2754,41 +2746,26 @@ implementation HeaderBytes[$00]:=$4D; { 'M' } HeaderBytes[$01]:=$5A; { 'Z' } - HeaderBytes[$02]:=Byte(BytesInLastBlock); - HeaderBytes[$03]:=Byte(BytesInLastBlock shr 8); - HeaderBytes[$04]:=Byte(BlocksInFile); - HeaderBytes[$05]:=Byte(BlocksInFile shr 8); - HeaderBytes[$06]:=Byte(NumRelocs); - HeaderBytes[$07]:=Byte(NumRelocs shr 8); - HeaderBytes[$08]:=Byte(HeaderParagraphs); - HeaderBytes[$09]:=Byte(HeaderParagraphs shr 8); - HeaderBytes[$0A]:=Byte(MinExtraParagraphs); - HeaderBytes[$0B]:=Byte(MinExtraParagraphs shr 8); - HeaderBytes[$0C]:=Byte(MaxExtraParagraphs); - HeaderBytes[$0D]:=Byte(MaxExtraParagraphs shr 8); - HeaderBytes[$0E]:=Byte(InitialSS); - HeaderBytes[$0F]:=Byte(InitialSS shr 8); - HeaderBytes[$10]:=Byte(InitialSP); - HeaderBytes[$11]:=Byte(InitialSP shr 8); - HeaderBytes[$12]:=Byte(Checksum); - HeaderBytes[$13]:=Byte(Checksum shr 8); - HeaderBytes[$14]:=Byte(InitialIP); - HeaderBytes[$15]:=Byte(InitialIP shr 8); - HeaderBytes[$16]:=Byte(InitialCS); - HeaderBytes[$17]:=Byte(InitialCS shr 8); - HeaderBytes[$18]:=Byte(RelocTableOffset); - HeaderBytes[$19]:=Byte(RelocTableOffset shr 8); - HeaderBytes[$1A]:=Byte(OverlayNumber); - HeaderBytes[$1B]:=Byte(OverlayNumber shr 8); + unaligned(PUint16(@HeaderBytes[$02{..$03}])^):=NtoLE(uint16(BytesInLastBlock)); + unaligned(PUint16(@HeaderBytes[$04{..$05}])^):=NtoLE(uint16(BlocksInFile)); + unaligned(PUint16(@HeaderBytes[$06{..$07}])^):=NtoLE(uint16(NumRelocs)); + unaligned(PUint16(@HeaderBytes[$08{..$09}])^):=NtoLE(uint16(HeaderParagraphs)); + unaligned(PUint16(@HeaderBytes[$0A{..$0B}])^):=NtoLE(uint16(MinExtraParagraphs)); + unaligned(PUint16(@HeaderBytes[$0C{..$0D}])^):=NtoLE(uint16(MaxExtraParagraphs)); + unaligned(PUint16(@HeaderBytes[$0E{..$0F}])^):=NtoLE(uint16(InitialSS)); + unaligned(PUint16(@HeaderBytes[$10{..$11}])^):=NtoLE(uint16(InitialSP)); + unaligned(PUint16(@HeaderBytes[$12{..$13}])^):=NtoLE(uint16(Checksum)); + unaligned(PUint16(@HeaderBytes[$14{..$15}])^):=NtoLE(uint16(InitialIP)); + unaligned(PUint16(@HeaderBytes[$16{..$17}])^):=NtoLE(uint16(InitialCS)); + unaligned(PUint16(@HeaderBytes[$18{..$19}])^):=NtoLE(uint16(RelocTableOffset)); + unaligned(PUint16(@HeaderBytes[$1A{..$1B}])^):=NtoLE(uint16(OverlayNumber)); aWriter.write(HeaderBytes[0],$1C); aWriter.write(ExtraHeaderData[0],Length(ExtraHeaderData)); for i:=0 to NumRelocs-1 do with Relocations[i] do begin - RelocBytes[0]:=Byte(offset); - RelocBytes[1]:=Byte(offset shr 8); - RelocBytes[2]:=Byte(segment); - RelocBytes[3]:=Byte(segment shr 8); + unaligned(PUint16(@RelocBytes[0{..1}])^):=NtoLE(uint16(offset)); + unaligned(PUint16(@RelocBytes[2{..3}])^):=NtoLE(uint16(segment)); aWriter.write(RelocBytes[0],4); end; { pad with zeros until the end of header (paragraph aligned) } @@ -3826,66 +3803,35 @@ cleanup: HeaderBytes[$01]:=$45; { 'E' } HeaderBytes[$02]:=Byte(LinkerVersion); HeaderBytes[$03]:=Byte(LinkerRevision); - HeaderBytes[$04]:=Byte(EntryTableOffset); - HeaderBytes[$05]:=Byte(EntryTableOffset shr 8); - HeaderBytes[$06]:=Byte(EntryTableLength); - HeaderBytes[$07]:=Byte(EntryTableLength shr 8); - HeaderBytes[$08]:=Byte(Reserved); - HeaderBytes[$09]:=Byte(Reserved shr 8); - HeaderBytes[$0A]:=Byte(Reserved shr 16); - HeaderBytes[$0B]:=Byte(Reserved shr 24); - HeaderBytes[$0C]:=Byte(Word(Flags)); - HeaderBytes[$0D]:=Byte(Word(Flags) shr 8); - HeaderBytes[$0E]:=Byte(AutoDataSegmentNumber); - HeaderBytes[$0F]:=Byte(AutoDataSegmentNumber shr 8); - HeaderBytes[$10]:=Byte(InitialLocalHeapSize); - HeaderBytes[$11]:=Byte(InitialLocalHeapSize shr 8); - HeaderBytes[$12]:=Byte(InitialStackSize); - HeaderBytes[$13]:=Byte(InitialStackSize shr 8); - HeaderBytes[$14]:=Byte(InitialIP); - HeaderBytes[$15]:=Byte(InitialIP shr 8); - HeaderBytes[$16]:=Byte(InitialCS); - HeaderBytes[$17]:=Byte(InitialCS shr 8); - HeaderBytes[$18]:=Byte(InitialSP); - HeaderBytes[$19]:=Byte(InitialSP shr 8); - HeaderBytes[$1A]:=Byte(InitialSS); - HeaderBytes[$1B]:=Byte(InitialSS shr 8); - HeaderBytes[$1C]:=Byte(SegmentTableEntriesCount); - HeaderBytes[$1D]:=Byte(SegmentTableEntriesCount shr 8); - HeaderBytes[$1E]:=Byte(ModuleReferenceTableEntriesCount); - HeaderBytes[$1F]:=Byte(ModuleReferenceTableEntriesCount shr 8); - HeaderBytes[$20]:=Byte(NonresidentNameTableLength); - HeaderBytes[$21]:=Byte(NonresidentNameTableLength shr 8); - HeaderBytes[$22]:=Byte(SegmentTableStart); - HeaderBytes[$23]:=Byte(SegmentTableStart shr 8); - HeaderBytes[$24]:=Byte(ResourceTableStart); - HeaderBytes[$25]:=Byte(ResourceTableStart shr 8); - HeaderBytes[$26]:=Byte(ResidentNameTableStart); - HeaderBytes[$27]:=Byte(ResidentNameTableStart shr 8); - HeaderBytes[$28]:=Byte(ModuleReferenceTableStart); - HeaderBytes[$29]:=Byte(ModuleReferenceTableStart shr 8); - HeaderBytes[$2A]:=Byte(ImportedNameTableStart); - HeaderBytes[$2B]:=Byte(ImportedNameTableStart shr 8); - HeaderBytes[$2C]:=Byte(NonresidentNameTableStart); - HeaderBytes[$2D]:=Byte(NonresidentNameTableStart shr 8); - HeaderBytes[$2E]:=Byte(NonresidentNameTableStart shr 16); - HeaderBytes[$2F]:=Byte(NonresidentNameTableStart shr 24); - HeaderBytes[$30]:=Byte(MovableEntryPointsCount); - HeaderBytes[$31]:=Byte(MovableEntryPointsCount shr 8); - HeaderBytes[$32]:=Byte(LogicalSectorAlignmentShiftCount); - HeaderBytes[$33]:=Byte(LogicalSectorAlignmentShiftCount shr 8); - HeaderBytes[$34]:=Byte(ResourceSegmentsCount); - HeaderBytes[$35]:=Byte(ResourceSegmentsCount shr 8); + unaligned(PUint16(@HeaderBytes[$04{..$05}])^):=NtoLE(uint16(EntryTableOffset)); + unaligned(PUint16(@HeaderBytes[$06{..$07}])^):=NtoLE(uint16(EntryTableLength)); + unaligned(PUint32(@HeaderBytes[$08{..$0B}])^):=NtoLE(uint32(Reserved)); + unaligned(PUint16(@HeaderBytes[$0C{..$0D}])^):=NtoLE(uint16(Flags)); + unaligned(PUint16(@HeaderBytes[$0E{..$0F}])^):=NtoLE(uint16(AutoDataSegmentNumber)); + unaligned(PUint16(@HeaderBytes[$10{..$11}])^):=NtoLE(uint16(InitialLocalHeapSize)); + unaligned(PUint16(@HeaderBytes[$12{..$13}])^):=NtoLE(uint16(InitialStackSize)); + unaligned(PUint16(@HeaderBytes[$14{..$15}])^):=NtoLE(uint16(InitialIP)); + unaligned(PUint16(@HeaderBytes[$16{..$17}])^):=NtoLE(uint16(InitialCS)); + unaligned(PUint16(@HeaderBytes[$18{..$19}])^):=NtoLE(uint16(InitialSP)); + unaligned(PUint16(@HeaderBytes[$1A{..$1B}])^):=NtoLE(uint16(InitialSS)); + unaligned(PUint16(@HeaderBytes[$1C{..$1D}])^):=NtoLE(uint16(SegmentTableEntriesCount)); + unaligned(PUint16(@HeaderBytes[$1E{..$1F}])^):=NtoLE(uint16(ModuleReferenceTableEntriesCount)); + unaligned(PUint16(@HeaderBytes[$20{..$21}])^):=NtoLE(uint16(NonresidentNameTableLength)); + unaligned(PUint16(@HeaderBytes[$22{..$23}])^):=NtoLE(uint16(SegmentTableStart)); + unaligned(PUint16(@HeaderBytes[$24{..$25}])^):=NtoLE(uint16(ResourceTableStart)); + unaligned(PUint16(@HeaderBytes[$26{..$27}])^):=NtoLE(uint16(ResidentNameTableStart)); + unaligned(PUint16(@HeaderBytes[$28{..$29}])^):=NtoLE(uint16(ModuleReferenceTableStart)); + unaligned(PUint16(@HeaderBytes[$2A{..$2B}])^):=NtoLE(uint16(ImportedNameTableStart)); + unaligned(PUint32(@HeaderBytes[$2C{..$2F}])^):=NtoLE(uint32(NonresidentNameTableStart)); + unaligned(PUint16(@HeaderBytes[$30{..$31}])^):=NtoLE(uint16(MovableEntryPointsCount)); + unaligned(PUint16(@HeaderBytes[$32{..$33}])^):=NtoLE(uint16(LogicalSectorAlignmentShiftCount)); + unaligned(PUint16(@HeaderBytes[$34{..$35}])^):=NtoLE(uint16(ResourceSegmentsCount)); HeaderBytes[$36]:=Byte(Ord(TargetOS)); HeaderBytes[$37]:=Byte(AdditionalFlags); - HeaderBytes[$38]:=Byte(GangLoadAreaStart); - HeaderBytes[$39]:=Byte(GangLoadAreaStart shr 8); - HeaderBytes[$3A]:=Byte(GangLoadAreaLength); - HeaderBytes[$3B]:=Byte(GangLoadAreaLength shr 8); - HeaderBytes[$3C]:=Byte(Reserved2); - HeaderBytes[$3D]:=Byte(Reserved2 shr 8); - HeaderBytes[$3E]:=Byte(ExpectedWindowsVersion); - HeaderBytes[$3F]:=Byte(ExpectedWindowsVersion shr 8); + unaligned(PUint16(@HeaderBytes[$38{..$39}])^):=NtoLE(uint16(GangLoadAreaStart)); + unaligned(PUint16(@HeaderBytes[$3A{..$3B}])^):=NtoLE(uint16(GangLoadAreaLength)); + unaligned(PUint16(@HeaderBytes[$3C{..$3D}])^):=NtoLE(uint16(Reserved2)); + unaligned(PUint16(@HeaderBytes[$3E{..$3F}])^):=NtoLE(uint16(ExpectedWindowsVersion)); aWriter.write(HeaderBytes[0],$40); end; @@ -3910,8 +3856,7 @@ cleanup: var AlignShiftBytes: array [0..1] of Byte; begin - AlignShiftBytes[0]:=Byte(ResourceDataAlignmentShiftCount); - AlignShiftBytes[1]:=Byte(ResourceDataAlignmentShiftCount shr 8); + unaligned(PUint16(@AlignShiftBytes[0{..1}])^):=NtoLE(uint16(ResourceDataAlignmentShiftCount)); aWriter.write(AlignShiftBytes[0],2); end; @@ -3975,8 +3920,7 @@ cleanup: internalerror(2019080801); aWriter.write(slen,1); aWriter.write(rn.Name[1],slen); - OrdNrBuf[0]:=Byte(rn.OrdinalNr); - OrdNrBuf[1]:=Byte(rn.OrdinalNr shr 8); + unaligned(PUint16(@OrdNrBuf[0{..1}])^):=NtoLE(uint16(rn.OrdinalNr)); aWriter.write(OrdNrBuf[0],2); end; { end of table mark } @@ -4011,8 +3955,7 @@ cleanup: ImpTblEntry:=TNewExeImportedNameTableEntry(imptbl.Find(TNewExeModuleReferenceTableEntry(Items[i]).Name)); if not Assigned(ImpTblEntry) then internalerror(2019080903); - buf[2*i]:=Byte(ImpTblEntry.TableOffset); - buf[2*i+1]:=Byte(ImpTblEntry.TableOffset shr 8); + unaligned(PUint16(@buf[2*i{..2*i+1}])^):=NtoLE(uint16(ImpTblEntry.TableOffset)); end; aWriter.write(buf[0],Length(buf)); end; @@ -4202,8 +4145,7 @@ cleanup: buf[1]:=$CD; { INT 3Fh instruction } buf[2]:=$3F; buf[3]:=Byte(cp.Segment); - buf[4]:=Byte(cp.Offset); - buf[5]:=Byte(cp.Offset shr 8); + unaligned(PUint16(@buf[4{..5}])^):=NtoLE(uint16(cp.Offset)); aWriter.write(buf[0],6); end; end @@ -4216,8 +4158,7 @@ cleanup: begin cp:=Items[i]; buf[0]:=cp.FlagsByte; - buf[1]:=Byte(cp.Offset); - buf[2]:=Byte(cp.Offset shr 8); + unaligned(PUint16(@buf[1{..2}])^):=NtoLE(uint16(cp.Offset)); aWriter.write(buf[0],3); end; end; @@ -4246,8 +4187,7 @@ cleanup: begin dest[0]:=Ord(AddressType); dest[1]:=Ord(RelocationType) or (Ord(IsAdditive) shl 2); - dest[2]:=Byte(Offset); - dest[3]:=Byte(Offset shr 8); + unaligned(PUint16(@dest[2{..3}])^):=NtoLE(uint16(Offset)); case RelocationType of nertInternalRef: begin @@ -4256,38 +4196,30 @@ cleanup: begin dest[4]:=Byte(InternalRefFixedSegmentNumber); dest[5]:=0; - dest[6]:=Byte(InternalRefFixedSegmentOffset); - dest[7]:=Byte(InternalRefFixedSegmentOffset shr 8); + unaligned(PUint16(@dest[6{..7}])^):=NtoLE(uint16(InternalRefFixedSegmentOffset)); end; neirstMovable: begin dest[4]:=$FF; dest[5]:=0; - dest[6]:=Byte(InternalRefMovableSegmentEntryTableIndex); - dest[7]:=Byte(InternalRefMovableSegmentEntryTableIndex shr 8); + unaligned(PUint16(@dest[6{..7}])^):=NtoLE(uint16(InternalRefMovableSegmentEntryTableIndex)); end; end; end; nertImportName: begin - dest[4]:=Byte(ImportModuleIndex); - dest[5]:=Byte(ImportModuleIndex shr 8); - dest[6]:=Byte(ImportNameIndex); - dest[7]:=Byte(ImportNameIndex shr 8); + unaligned(PUint16(@dest[4{..5}])^):=NtoLE(uint16(ImportModuleIndex)); + unaligned(PUint16(@dest[6{..7}])^):=NtoLE(uint16(ImportNameIndex)); end; nertImportOrdinal: begin - dest[4]:=Byte(ImportModuleIndex); - dest[5]:=Byte(ImportModuleIndex shr 8); - dest[6]:=Byte(ImportOrdinal); - dest[7]:=Byte(ImportOrdinal shr 8); + unaligned(PUint16(@dest[4{..5}])^):=NtoLE(uint16(ImportModuleIndex)); + unaligned(PUint16(@dest[6{..7}])^):=NtoLE(uint16(ImportOrdinal)); end; nertOsFixup: begin - dest[4]:=Byte(Ord(OsFixupType)); - dest[5]:=Byte(Ord(OsFixupType) shr 8); - dest[6]:=0; - dest[7]:=0; + unaligned(PUint16(@dest[4{..5}])^):=NtoLE(uint16(Ord(OsFixupType))); + unaligned(PUint16(@dest[6{..7}])^):=0; end; end; end; @@ -4339,8 +4271,7 @@ cleanup: i: Integer; begin SetLength(buf,Size); - buf[0]:=Byte(Count); - buf[1]:=Byte(Count shr 8); + unaligned(PUint16(@buf[0])^):=NtoLE(uint16(Count)); p:=@(buf[2]); for i:=0 to Count-1 do begin @@ -4389,14 +4320,10 @@ cleanup: var SegmentHeaderBytes: array [0..7] of Byte; begin - SegmentHeaderBytes[0]:=Byte(DataPosSectors); - SegmentHeaderBytes[1]:=Byte(DataPosSectors shr 8); - SegmentHeaderBytes[2]:=Byte(SizeInFile); - SegmentHeaderBytes[3]:=Byte(SizeInFile shr 8); - SegmentHeaderBytes[4]:=Byte(Word(NewExeSegmentFlags)); - SegmentHeaderBytes[5]:=Byte(Word(NewExeSegmentFlags) shr 8); - SegmentHeaderBytes[6]:=Byte(MinAllocSize); - SegmentHeaderBytes[7]:=Byte(MinAllocSize shr 8); + unaligned(PUint16(@SegmentHeaderBytes[0{..1}])^):=NtoLE(uint16(DataPosSectors)); + unaligned(PUint16(@SegmentHeaderBytes[2{..3}])^):=NtoLE(uint16(SizeInFile)); + unaligned(PUint16(@SegmentHeaderBytes[4{..5}])^):=NtoLE(uint16(NewExeSegmentFlags)); + unaligned(PUint16(@SegmentHeaderBytes[6{..7}])^):=NtoLE(uint16(MinAllocSize)); aWriter.write(SegmentHeaderBytes[0],8); end; diff --git a/compiler/omfbase.pas b/compiler/omfbase.pas index d160cb4ba0..f800aadf07 100644 --- a/compiler/omfbase.pas +++ b/compiler/omfbase.pas @@ -1389,13 +1389,12 @@ implementation function TOmfRawRecord.GetRecordLength: Word; begin - Result:=RawData[-2] or (RawData[-1] shl 8); + Result:=LEtoN(unaligned(PUint16(@RawData[-2{..-1}])^)); end; procedure TOmfRawRecord.SetRecordLength(AValue: Word); begin - RawData[-2]:=Byte(AValue); - RawData[-1]:=Byte(AValue shr 8); + unaligned(PUint16(@RawData[-2{..-1}])^):=NtoLE(uint16(AValue)); end; function TOmfRawRecord.ReadStringAt(Offset: Integer; out s: string): Integer; @@ -1628,8 +1627,7 @@ implementation OrdinalIdx:=ModuleNameLenIdx+1+ModuleNameLen; if (OrdinalIdx+1)>Length(ComentRecord.CommentString) then internalerror(2019061625); - Ordinal:=Ord(ComentRecord.CommentString[OrdinalIdx]) or - (Word(Ord(ComentRecord.CommentString[OrdinalIdx+1])) shl 8); + Ordinal:=LEtoN(unaligned(PUint16(@ComentRecord.CommentString[OrdinalIdx{..OrdinalIdx+1}])^)); end else begin @@ -1698,8 +1696,7 @@ implementation ExportOrdinalIdx:=InternalNameLenIdx+1+InternalNameLen; if (ExportOrdinalIdx+1)>Length(ComentRecord.CommentString) then internalerror(2019061606); - ExportOrdinal:=Ord(ComentRecord.CommentString[ExportOrdinalIdx]) or - (Word(Ord(ComentRecord.CommentString[ExportOrdinalIdx+1])) shl 8); + ExportOrdinal:=LEtoN(unaligned(PUint16(@ComentRecord.CommentString[ExportOrdinalIdx{..ExportOrdinalIdx+1}])^)); end else ExportOrdinal:=0; @@ -1821,7 +1818,7 @@ implementation inc(MinLen,3); if RawRecord.RecordLength=RawRecord.RecordLength then internalerror(2015041401); - BaseFrame:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8); + BaseFrame:=LEtoN(unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^)); Inc(NextOfs,2); end else @@ -2004,15 +1993,14 @@ implementation begin if (NextOfs+3)>=RawRecord.RecordLength then internalerror(2015041405); - PublicOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8)+ - (RawRecord.RawData[NextOfs+2] shl 16)+(RawRecord.RawData[NextOfs+3] shl 24); + PublicOffset:=LEtoN(unaligned(PUint32(@RawRecord.RawData[NextOfs{..NextOfs+3}])^)); Inc(NextOfs,4); end else begin if (NextOfs+1)>=RawRecord.RecordLength then internalerror(2015041407); - PublicOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8); + PublicOffset:=LEtoN(unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^)); Inc(NextOfs,2); end; NextOfs:=RawRecord.ReadIndexedRef(NextOfs,TypeIndex); @@ -2049,8 +2037,7 @@ implementation NextOfs:=RawRecord.WriteIndexedRef(NextOfs,BaseSegmentIndex); if BaseSegmentIndex=0 then begin - RawRecord.RawData[NextOfs]:=Byte(BaseFrame); - RawRecord.RawData[NextOfs+1]:=Byte(BaseFrame shr 8); + unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^):=NtoLE(uint16(BaseFrame)); Inc(NextOfs,2); end; @@ -2071,18 +2058,14 @@ implementation NextOfs:=RawRecord.WriteStringAt(NextOfs,PubName.Name); if Is32Bit then begin - RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset); - RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8); - RawRecord.RawData[NextOfs+2]:=Byte(PubName.PublicOffset shr 16); - RawRecord.RawData[NextOfs+3]:=Byte(PubName.PublicOffset shr 24); + unaligned(PUint32(@RawRecord.RawData[NextOfs{..NextOfs+3}])^):=NtoLE(uint32(PubName.PublicOffset)); Inc(NextOfs,4); end else begin if PubName.PublicOffset>$ffff then internalerror(2015041403); - RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset); - RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8); + unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^):=NtoLE(uint16(PubName.PublicOffset)); Inc(NextOfs,2); end; NextOfs:=RawRecord.WriteIndexedRef(NextOfs,PubName.TypeIndex); @@ -2232,18 +2215,14 @@ implementation begin if (NextOfs+3)>=RawRecord.RecordLength then internalerror(2015040504); - TargetDisplacement := RawRecord.RawData[NextOfs]+ - (RawRecord.RawData[NextOfs+1] shl 8)+ - (RawRecord.RawData[NextOfs+2] shl 16)+ - (RawRecord.RawData[NextOfs+3] shl 24); + TargetDisplacement := LEtoN(unaligned(PUint32(@RawRecord.RawData[NextOfs{..NextOfs+3}])^)); Inc(NextOfs,4); end else begin if (NextOfs+1)>=RawRecord.RecordLength then internalerror(2015040508); - TargetDisplacement := RawRecord.RawData[NextOfs]+ - (RawRecord.RawData[NextOfs+1] shl 8); + TargetDisplacement := LEtoN(unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^)); Inc(NextOfs,2); end; end; @@ -2253,21 +2232,20 @@ implementation { physical start address } if (NextOfs+1)>=RawRecord.RecordLength then internalerror(2015040320); - PhysFrameNumber:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8); + PhysFrameNumber:=LEtoN(unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^)); Inc(NextOfs,2); if Is32Bit then begin if (NextOfs+3)>=RawRecord.RecordLength then internalerror(2015040321); - PhysOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8)+ - (RawRecord.RawData[NextOfs+2] shl 16)+(RawRecord.RawData[NextOfs+3] shl 24); + PhysOffset:=LEtoN(unaligned(PUint32(@RawRecord.RawData[NextOfs{..NextOfs+3}])^)); Inc(NextOfs,4); end else begin if (NextOfs+1)>=RawRecord.RecordLength then internalerror(2015040322); - PhysOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8); + PhysOffset:=LEtoN(unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^)); Inc(NextOfs,2); end; end; @@ -2307,18 +2285,14 @@ implementation begin if Is32Bit then begin - RawRecord.RawData[NextOfs]:=Byte(TargetDisplacement); - RawRecord.RawData[NextOfs+1]:=Byte(TargetDisplacement shr 8); - RawRecord.RawData[NextOfs+2]:=Byte(TargetDisplacement shr 16); - RawRecord.RawData[NextOfs+3]:=Byte(TargetDisplacement shr 24); + unaligned(PUint32(@RawRecord.RawData[NextOfs{..NextOfs+3}])^):=NtoLE(uint32(TargetDisplacement)); Inc(NextOfs,4); end else begin if TargetDisplacement>$ffff then internalerror(2015040502); - RawRecord.RawData[NextOfs]:=Byte(TargetDisplacement); - RawRecord.RawData[NextOfs+1]:=Byte(TargetDisplacement shr 8); + unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^):=NtoLE(uint16(TargetDisplacement)); Inc(NextOfs,2); end; end; @@ -2326,23 +2300,18 @@ implementation else begin { physical start address } - RawRecord.RawData[NextOfs]:=Byte(PhysFrameNumber); - RawRecord.RawData[NextOfs+1]:=Byte(PhysFrameNumber shr 8); + unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^):=NtoLE(uint16(PhysFrameNumber)); Inc(NextOfs,2); if Is32Bit then begin - RawRecord.RawData[NextOfs]:=Byte(PhysOffset); - RawRecord.RawData[NextOfs+1]:=Byte(PhysOffset shr 8); - RawRecord.RawData[NextOfs+2]:=Byte(PhysOffset shr 16); - RawRecord.RawData[NextOfs+3]:=Byte(PhysOffset shr 24); + unaligned(PUint32(@RawRecord.RawData[NextOfs{..NextOfs+3}])^):=NtoLE(uint32(PhysOffset)); Inc(NextOfs,4); end else begin if PhysOffset>$ffff then internalerror(2015040506); - RawRecord.RawData[NextOfs]:=Byte(PhysOffset); - RawRecord.RawData[NextOfs+1]:=Byte(PhysOffset shr 8); + unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^):=NtoLE(uint16(PhysOffset)); Inc(NextOfs,2); end; end; @@ -2455,16 +2424,11 @@ implementation RecordSize:=4; while (NextOfs+RecordSize)High(Word) then internalerror(2018050901); - RawRecord.RawData[NextOfs]:=byte(Offset); - RawRecord.RawData[NextOfs+1]:=byte(Offset shr 8); + unaligned(PUint16(@RawRecord.RawData[NextOfs{..NextOfs+1}])^):=NtoLE(uint16(Offset)); Inc(NextOfs,2); end; end; @@ -2680,7 +2639,7 @@ implementation if (Offset+2)>=RawRecord.RecordLength then internalerror(2015040509); { unlike other fields in the OMF format, this one is big endian } - Locat:=(RawRecord.RawData[Offset] shl 8) or RawRecord.RawData[Offset+1]; + Locat:=BEtoN(unaligned(PUint16(@RawRecord.RawData[Offset{..Offset+1}])^)); FixData:=RawRecord.RawData[Offset+2]; Inc(Offset,3); if (Locat and $8000)=0 then @@ -2719,18 +2678,14 @@ implementation begin if (Offset+3)>=RawRecord.RecordLength then internalerror(2015040510); - TargetDisplacement := RawRecord.RawData[Offset]+ - (RawRecord.RawData[Offset+1] shl 8)+ - (RawRecord.RawData[Offset+2] shl 16)+ - (RawRecord.RawData[Offset+3] shl 24); + TargetDisplacement := LEtoN(unaligned(PUint32(@RawRecord.RawData[Offset{..Offset+3}])^)); Inc(Offset,4); end else begin if (Offset+1)>=RawRecord.RecordLength then internalerror(2015040511); - TargetDisplacement := RawRecord.RawData[Offset]+ - (RawRecord.RawData[Offset+1] shl 8); + TargetDisplacement := LEtoN(unaligned(PUint16(@RawRecord.RawData[Offset{..Offset+1}])^)); Inc(Offset,2); end; end; @@ -2746,8 +2701,7 @@ implementation internalerror(2015040505); Locat:=$8000+(Ord(Mode) shl 14)+(Ord(LocationType) shl 10)+DataRecordOffset; { unlike other fields in the OMF format, this one is big endian } - RawRecord.RawData[Offset]:=Byte(Locat shr 8); - RawRecord.RawData[Offset+1]:=Byte(Locat); + unaligned(PUint16(@RawRecord.RawData[Offset{..Offset+1}])^):=NtoBE(uint16(Locat)); Inc(Offset, 2); FixData:=(Ord(FrameDeterminedByThread) shl 7)+(Ord(TargetDeterminedByThread) shl 3); if FrameDeterminedByThread then @@ -2772,18 +2726,14 @@ implementation begin if Is32Bit then begin - RawRecord.RawData[Offset]:=Byte(TargetDisplacement); - RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8); - RawRecord.RawData[Offset+2]:=Byte(TargetDisplacement shr 16); - RawRecord.RawData[Offset+3]:=Byte(TargetDisplacement shr 24); + unaligned(PUint32(@RawRecord.RawData[Offset{..Offset+3}])^):=NtoLE(uint32(TargetDisplacement)); Inc(Offset,4); end else begin if TargetDisplacement>$ffff then internalerror(2015040507); - RawRecord.RawData[Offset]:=Byte(TargetDisplacement); - RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8); + unaligned(PUint16(@RawRecord.RawData[Offset{..Offset+1}])^):=NtoLE(uint16(TargetDisplacement)); Inc(Offset,2); end; end; @@ -2840,12 +2790,8 @@ implementation internalerror(2015040314); { this will also range check PageSize and will ensure that RecordLength>=13 } PageSize:=RawRecord.RecordLength+3; - DictionaryOffset:=RawRecord.RawData[0]+ - (RawRecord.RawData[1] shl 8)+ - (RawRecord.RawData[2] shl 16)+ - (RawRecord.RawData[3] shl 24); - DictionarySizeInBlocks:=RawRecord.RawData[4]+ - (RawRecord.RawData[5] shl 8); + DictionaryOffset:=LEtoN(unaligned(PUint32(@RawRecord.RawData[0{..3}])^)); + DictionarySizeInBlocks:=LEtoN(unaligned(PUint16(@RawRecord.RawData[4{..5}])^)); Flags:=RawRecord.RawData[6]; end; @@ -2855,12 +2801,8 @@ implementation FillChar(RawRecord.RawData,SizeOf(RawRecord.RawData),0); RawRecord.RecordType:=RT_LIBHEAD; RawRecord.RecordLength:=PageSize-3; - RawRecord.RawData[0]:=Byte(DictionaryOffset); - RawRecord.RawData[1]:=Byte(DictionaryOffset shr 8); - RawRecord.RawData[2]:=Byte(DictionaryOffset shr 16); - RawRecord.RawData[3]:=Byte(DictionaryOffset shr 24); - RawRecord.RawData[4]:=Byte(DictionarySizeInBlocks); - RawRecord.RawData[5]:=Byte(DictionarySizeInBlocks shr 8); + unaligned(PUint32(@RawRecord.RawData[0{..3}])^):=NtoLE(uint32(DictionaryOffset)); + unaligned(PUint16(@RawRecord.RawData[4{..5}])^):=NtoLE(uint16(DictionarySizeInBlocks)); RawRecord.RawData[6]:=Flags; { the LIBHEAD record contains no checksum byte, so no need to call RawRecord.CalculateChecksumByte } diff --git a/compiler/owomflib.pas b/compiler/owomflib.pas index f6b587db7c..54931dc5f6 100644 --- a/compiler/owomflib.pas +++ b/compiler/owomflib.pas @@ -433,8 +433,7 @@ implementation store_at:=2*pb^[h.bucket_x]; pb^[store_at]:=length_of_string; Move(N[1],pb^[store_at+1],length_of_string); - pb^[store_at+1+length_of_string]:=Byte(PageNum); - pb^[store_at+1+length_of_string+1]:=Byte(PageNum shr 8); + unaligned(PUint16(@pb^[store_at+1+length_of_string{..store_at+1+length_of_string+1}])^):=NtoLE(uint16(PageNum)); Inc(pb^[freespace],space_required div 2); if pb^[freespace]=0 then pb^[freespace]:=255; @@ -525,8 +524,7 @@ implementation end; SetLength(name,length_of_string); Move(block^[ofs+1],name[1],length_of_string); - PageNum:=block^[ofs+1+length_of_string]+ - block^[ofs+1+length_of_string+1] shl 8; + PageNum:=LEtoN(unaligned(PUint16(@block^[ofs+1+length_of_string{..ofs+1+length_of_string+1}])^)); TOmfLibDictionaryEntry.create(LibSymbols,name,PageNum); end; end; diff --git a/compiler/symtype.pas b/compiler/symtype.pas index 3c10ba8fb5..4a9e59c3ea 100644 --- a/compiler/symtype.pas +++ b/compiler/symtype.pas @@ -933,26 +933,19 @@ implementation { register that the unit is needed for resolving } data[len]:=ord(deref_unit); idx:=current_module.derefidx_unit(st.moduleid); - data[len+1]:=idx shr 8 and $ff; - data[len+2]:=idx and $ff; + unaligned(PUint16(@data[len+1{..len+2}])^):=NtoBE(uint16(idx)); inc(len,3); end; if s is tsym then begin data[len]:=ord(deref_symid); - data[len+1]:=tsym(s).symid shr 24 and $ff; - data[len+2]:=tsym(s).symid shr 16 and $ff; - data[len+3]:=tsym(s).symid shr 8 and $ff; - data[len+4]:=tsym(s).symid and $ff; + unaligned(PInt32(@data[len+1{..len+4}])^):=NtoBE(int32(tsym(s).symid)); inc(len,5); end else begin data[len]:=ord(deref_defid); - data[len+1]:=tdef(s).defid shr 24 and $ff; - data[len+2]:=tdef(s).defid shr 16 and $ff; - data[len+3]:=tdef(s).defid shr 8 and $ff; - data[len+4]:=tdef(s).defid and $ff; + unaligned(PInt32(@data[len+1{..len+4}])^):=NtoBE(int32(tdef(s).defid)); inc(len,5); end; end @@ -1004,19 +997,19 @@ implementation case typ of deref_unit : begin - idx:=(data[i] shl 8) or data[i+1]; + idx:=BEtoN(unaligned(PUint16(@data[i{..i+1}])^)); inc(i,2); pm:=current_module.resolve_unit(idx); end; deref_defid : begin - idx:=longint((data[i] shl 24) or (data[i+1] shl 16) or (data[i+2] shl 8) or data[i+3]); + idx:=BEtoN(unaligned(PInt32(@data[i{..i+3}])^)); inc(i,4); result:=tdef(pm.deflist[idx]); end; deref_symid : begin - idx:=longint((data[i] shl 24) or (data[i+1] shl 16) or (data[i+2] shl 8) or data[i+3]); + idx:=BEtoN(unaligned(PInt32(@data[i{..i+3}])^)); inc(i,4); result:=tsym(pm.symlist[idx]); end; diff --git a/compiler/utils/ppuutils/ppudump.pp b/compiler/utils/ppuutils/ppudump.pp index 944aed85c6..ecc2e28f14 100644 --- a/compiler/utils/ppuutils/ppudump.pp +++ b/compiler/utils/ppuutils/ppudump.pp @@ -1416,7 +1416,7 @@ begin write('Nil'); deref_symid : begin - idx:=pdata[i] shl 24 or pdata[i+1] shl 16 or pdata[i+2] shl 8 or pdata[i+3]; + idx:=BEtoN(unaligned(PInt32(@pdata[i{..i+3}])^)); inc(i,4); write(['SymId ',idx]); if Ref <> nil then @@ -1424,7 +1424,7 @@ begin end; deref_defid : begin - idx:=pdata[i] shl 24 or pdata[i+1] shl 16 or pdata[i+2] shl 8 or pdata[i+3]; + idx:=BEtoN(unaligned(PInt32(@pdata[i{..i+3}])^)); inc(i,4); write(['DefId ',idx]); if Ref <> nil then @@ -1432,7 +1432,7 @@ begin end; deref_unit : begin - idx:=pdata[i] shl 8 or pdata[i+1]; + idx:=BEtoN(unaligned(PUint16(@pdata[i{..i+1}])^)); inc(i,2); write(['Unit ',idx]); if Ref <> nil then