| 1 | #pragma once |
|---|---|
| 2 | |
| 3 | #include <nn/util/util_BinTypes.h> |
| 4 | |
| 5 | namespace nn::util { |
| 6 | |
| 7 | struct RelocationTable; |
| 8 | struct BinaryBlockHeader; |
| 9 | |
| 10 | enum ByteOrderMark { ByteOrderMark_Normal = 65279, ByteOrderMark_Reverse = 65534 }; |
| 11 | |
| 12 | struct BinaryFileHeader { |
| 13 | BinFileSignature signature; |
| 14 | BinVersion version; |
| 15 | uint16_t _byteOrderMark; |
| 16 | uint8_t _alignmentShift; |
| 17 | uint8_t _targetAddressSize; |
| 18 | uint32_t _offsetToFileName; |
| 19 | uint16_t _flag; |
| 20 | uint16_t _offsetToFirstBlock; |
| 21 | uint32_t _offsetToRelTable; |
| 22 | uint32_t _fileSize; |
| 23 | |
| 24 | bool IsValid(int64_t, int, int, int) const; |
| 25 | bool IsSignatureValid(int64_t) const; |
| 26 | bool IsVersionValid(int, int, int) const; |
| 27 | bool IsEndianValid() const; |
| 28 | bool IsEndianReverse() const; |
| 29 | bool IsAlignmentValid() const; |
| 30 | bool IsRelocated() const; |
| 31 | size_t GetFileSize() const; |
| 32 | size_t GetAlignment() const; |
| 33 | string_view GetFileName() const; |
| 34 | RelocationTable* GetRelocationTable(); |
| 35 | BinaryBlockHeader* GetFirstBlock(); |
| 36 | const BinaryBlockHeader* GetFirstBlock() const; |
| 37 | BinaryBlockHeader* FindFirstBlock(int); |
| 38 | const BinaryBlockHeader* FindFirstBlock(int) const; |
| 39 | void SetByteOrderMark(ByteOrderMark); |
| 40 | void SetAddressSize(); |
| 41 | void SetAlignment(size_t); |
| 42 | void SetFileSize(size_t); |
| 43 | void SetFileName(const string_view&); |
| 44 | void SetFirstBlock(BinaryBlockHeader*); |
| 45 | void SetRelocated(bool); |
| 46 | void SetRelocationTable(RelocationTable*); |
| 47 | }; |
| 48 | |
| 49 | struct BinaryBlockHeader { |
| 50 | BinaryBlockHeader* GetNextBlock(); |
| 51 | const BinaryBlockHeader* GetNextBlock() const; |
| 52 | BinaryBlockHeader* FindNextBlock(int signature); |
| 53 | const BinaryBlockHeader* FindNextBlock(int signature) const; |
| 54 | size_t GetBlockSize() const; |
| 55 | void SetNextBlock(BinaryBlockHeader* block); |
| 56 | void SetBlockSize(size_t size); |
| 57 | |
| 58 | BinBlockSignature signature; |
| 59 | uint32_t _offsetToNextBlock; |
| 60 | uint32_t _blockSize; |
| 61 | uint32_t _reserved; |
| 62 | }; |
| 63 | |
| 64 | struct RelocationTable { |
| 65 | typedef uint64_t AddrType; |
| 66 | |
| 67 | struct Section { |
| 68 | union { |
| 69 | AddrType _padding; |
| 70 | void* _ptr; |
| 71 | }; |
| 72 | uint32_t _position; |
| 73 | uint32_t _size; |
| 74 | int32_t _entryIndex; |
| 75 | int32_t _entryCount; |
| 76 | |
| 77 | void SetPtr(void*); |
| 78 | void* GetPtr() const; |
| 79 | void* GetPtrInFile(void*) const; |
| 80 | void* GetBasePtr(void*) const; |
| 81 | size_t GetSize() const; |
| 82 | }; |
| 83 | |
| 84 | static const int PackedSignature; |
| 85 | BinBlockSignature _signature; |
| 86 | uint32_t _position; |
| 87 | int32_t _sectionCount; |
| 88 | Section _sections[1]; |
| 89 | |
| 90 | static AddrType CalculateSize(int, int); |
| 91 | void Relocate(); |
| 92 | void Unrelocate(); |
| 93 | Section* GetSection(int); |
| 94 | const Section* GetSection(int) const; |
| 95 | void SetSignature(); |
| 96 | }; |
| 97 | |
| 98 | } // namespace nn::util |