diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-12-27 07:39:05 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-12-27 07:39:05 +0100 |
| commit | f61c814a0a95e98529c96361c992f1a8ea24688a (patch) | |
| tree | 0c5fcb7976f5753149e0b8cc3b974a318e013f61 /dev/LibCompiler/NFC | |
| parent | c2046f25120d8c39b36cb81459f3370c8a5f1fa3 (diff) | |
META: Refactor source code.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/LibCompiler/NFC')
| -rw-r--r-- | dev/LibCompiler/NFC/AE.h | 143 | ||||
| -rw-r--r-- | dev/LibCompiler/NFC/ErrorID.h | 22 | ||||
| -rw-r--r-- | dev/LibCompiler/NFC/ErrorOr.h | 61 | ||||
| -rw-r--r-- | dev/LibCompiler/NFC/PEF.h | 144 | ||||
| -rw-r--r-- | dev/LibCompiler/NFC/Ref.h | 103 | ||||
| -rw-r--r-- | dev/LibCompiler/NFC/String.h | 90 | ||||
| -rw-r--r-- | dev/LibCompiler/NFC/XCOFF.h | 41 |
7 files changed, 604 insertions, 0 deletions
diff --git a/dev/LibCompiler/NFC/AE.h b/dev/LibCompiler/NFC/AE.h new file mode 100644 index 0000000..962a7c3 --- /dev/null +++ b/dev/LibCompiler/NFC/AE.h @@ -0,0 +1,143 @@ +/* + * ======================================================== + * + * LibCompiler + * Copyright (C) 2024 Theater Quality Inc, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <LibCompiler/Defines.h> + +#define kAEMag0 'A' +#define kAEMag1 'E' + +#define kAESymbolLen (255) +#define kAEPad (8) +#define kAEMagLen (2) +#define kAENullType (0x00) + +// Advanced Executable File Format for MetroLink. +// Reloctable by offset is the default strategy. +// You can also relocate at runtime but that's up to the operating system +// loader. + +namespace LibCompiler +{ + // @brief Advanced Executable Header + // One thing to keep in mind. + // This object format, is reloctable. + typedef struct AEHeader final + { + CharType fMagic[kAEMagLen]; + CharType fArch; + CharType fSubArch; + SizeType fCount; + CharType fSize; + SizeType fStartCode; + SizeType fCodeSize; + CharType fPad[kAEPad]; + } PACKED AEHeader, *AEHeaderPtr; + + // @brief Advanced Executable Record. + // Could be data, code or bss. + // fKind must be filled with PEF fields. + + typedef struct AERecordHeader final + { + CharType fName[kAESymbolLen]; + SizeType fKind; + SizeType fSize; + SizeType fFlags; + UIntPtr fOffset; + CharType fPad[kAEPad]; + } PACKED AERecordHeader, *AERecordHeaderPtr; + + enum + { + kKindRelocationByOffset = 0x23f, + kKindRelocationAtRuntime = 0x34f, + }; +} // namespace LibCompiler + +// provide operator<< for AE + +inline std::ofstream& operator<<(std::ofstream& fp, LibCompiler::AEHeader& container) +{ + fp.write((char*)&container, sizeof(LibCompiler::AEHeader)); + + return fp; +} + +inline std::ofstream& operator<<(std::ofstream& fp, + LibCompiler::AERecordHeader& container) +{ + fp.write((char*)&container, sizeof(LibCompiler::AERecordHeader)); + + return fp; +} + +inline std::ifstream& operator>>(std::ifstream& fp, LibCompiler::AEHeader& container) +{ + fp.read((char*)&container, sizeof(LibCompiler::AEHeader)); + return fp; +} + +inline std::ifstream& operator>>(std::ifstream& fp, + LibCompiler::AERecordHeader& container) +{ + fp.read((char*)&container, sizeof(LibCompiler::AERecordHeader)); + return fp; +} + +namespace LibCompiler::Utils +{ + /** + * @brief AE Reader protocol + * + */ + class AEReadableProtocol final + { + public: + std::ifstream FP; + + public: + explicit AEReadableProtocol() = default; + ~AEReadableProtocol() = default; + + TOOLCHAINKIT_COPY_DELETE(AEReadableProtocol); + + /** + * @brief Read AE Record headers. + * + * @param raw the containing buffer + * @param sz it's size (1 = one AERecordHeader, 2 two AERecordHeader(s)) + * @return AERecordHeaderPtr + */ + AERecordHeaderPtr Read(char* raw, std::size_t sz) + { + if (!raw) + return nullptr; + + return this->_Read<AERecordHeader>(raw, sz * sizeof(AERecordHeader)); + } + + private: + /** + * @brief Implementation of Read for raw classes. + * + * @tparam TypeClass The class to read. + * @param raw the buffer + * @param sz the size + * @return TypeClass* the returning class. + */ + template <typename TypeClass> + TypeClass* _Read(char* raw, std::size_t sz) + { + FP.read(raw, std::streamsize(sz)); + return reinterpret_cast<TypeClass*>(raw); + } + }; +} // namespace LibCompiler::Utils diff --git a/dev/LibCompiler/NFC/ErrorID.h b/dev/LibCompiler/NFC/ErrorID.h new file mode 100644 index 0000000..e41410e --- /dev/null +++ b/dev/LibCompiler/NFC/ErrorID.h @@ -0,0 +1,22 @@ +/* + * ======================================================== + * + * LibCompiler + * Copyright (C) 2024 Theater Quality Inc, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <LibCompiler/Defines.h> +#include <LibCompiler/NFC/ErrorOr.h> + +#define TOOLCHAINKIT_EXEC_ERROR -30 +#define TOOLCHAINKIT_FILE_NOT_FOUND -31 +#define TOOLCHAINKIT_DIR_NOT_FOUND -32 +#define TOOLCHAINKIT_FILE_EXISTS -33 +#define TOOLCHAINKIT_TOO_LONG -34 +#define TOOLCHAINKIT_INVALID_DATA -35 +#define TOOLCHAINKIT_UNIMPLEMENTED -36 +#define TOOLCHAINKIT_FAT_ERROR -37 diff --git a/dev/LibCompiler/NFC/ErrorOr.h b/dev/LibCompiler/NFC/ErrorOr.h new file mode 100644 index 0000000..18bac3f --- /dev/null +++ b/dev/LibCompiler/NFC/ErrorOr.h @@ -0,0 +1,61 @@ +/* + * ======================================================== + * + * LibCompiler + * Copyright (C) 2024 Theater Quality Inc, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <LibCompiler/Defines.h> +#include <LibCompiler/NFC/Ref.h> + +namespace LibCompiler +{ + using ErrorT = UInt32; + + template <typename T> + class ErrorOr final + { + public: + ErrorOr() = default; + ~ErrorOr() = default; + + public: + explicit ErrorOr(Int32 err) + : mId(err) + { + } + + explicit ErrorOr(nullPtr Null) + { + } + + explicit ErrorOr(T Class) + : mRef(Class) + { + } + + ErrorOr& operator=(const ErrorOr&) = default; + ErrorOr(const ErrorOr&) = default; + + Ref<T> Leak() + { + return mRef; + } + + operator bool() + { + return mRef; + } + + private: + Ref<T> mRef; + Int32 mId{0}; + }; + + using ErrorOrAny = ErrorOr<voidPtr>; + +} // namespace LibCompiler diff --git a/dev/LibCompiler/NFC/PEF.h b/dev/LibCompiler/NFC/PEF.h new file mode 100644 index 0000000..aec3c64 --- /dev/null +++ b/dev/LibCompiler/NFC/PEF.h @@ -0,0 +1,144 @@ +/* ------------------------------------------- + + Copyright (C) 2024 Theater Quality Inc, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include <LibCompiler/Defines.h> + +// @file PEF.hpp +// @brief Preferred Executable Format + +#define kPefMagic "Joy!" +#define kPefMagicFat "yoJ!" + +#define kPefExt ".o" +#define kPefDylibExt ".dylib" +#define kPefLibExt ".lib" +#define kPefObjectExt ".obj" +#define kPefDebugExt ".dbg" +#define kPefDriverExt ".sys" + +#define kPefZero128 ".zero128" +#define kPefCode128 ".code128" +#define kPefData128 ".data128" + +#define kPefZero64 ".zero64" +#define kPefCode64 ".code64" +#define kPefData64 ".data64" + +#define kPefMagicLen (5) + +#define kPefVersion (3) +#define kPefNameLen (255) + +#define kPefBaseOrigin (0x40000000) + +#define kPefStart "__ImageStart" + +namespace LibCompiler +{ + enum + { + kPefArchStart = 99, + kPefArchIntel86S = 100, + kPefArchAMD64, + kPefArchRISCV, + kPefArch64000, /* 64x0 RISC architecture. */ + kPefArch32000, + kPefArchPowerPC, /* 64-bit POWER architecture. */ + kPefArchARM64, + kPefArchCount = (kPefArchPowerPC - kPefArchIntel86S) + 1, + kPefArchInvalid = 0xFF, + }; + + enum + { + kPefSubArchAMD, + kPefSubArchIntel, + kPefSubArchARM, + kPefSubArchGeneric, + kPefSubArchIBM, + }; + + enum + { + kPefKindExec = 1, /* .o */ + kPefKindDylib = 2, /* .dylib */ + kPefKindObject = 4, /* .obj */ + kPefKindDebug = 5, /* .dbg */ + kPefKindDriver = 6, + kPefKindCount, + }; + + /* PEF container */ + typedef struct PEFContainer final + { + CharType Magic[kPefMagicLen]; + UInt32 Linker; + UInt32 Version; + UInt32 Kind; + UInt32 Abi; + UInt32 Cpu; + UInt32 SubCpu; /* Cpu specific information */ + UIntPtr Start; /* Origin of code */ + SizeType HdrSz; /* Size of header */ + SizeType Count; /* container header count */ + } PACKED PEFContainer; + + /* First PEFCommandHeader starts after PEFContainer */ + /* Last container is __exec_end */ + + /* PEF executable section and commands. */ + + typedef struct PEFCommandHeader final + { + CharType Name[kPefNameLen]; /* container name */ + UInt32 Cpu; /* container cpu */ + UInt32 SubCpu; /* container sub-cpu */ + UInt32 Flags; /* container flags */ + UInt16 Kind; /* container kind */ + UIntPtr Offset; /* file offset */ + SizeType Size; /* file size */ + } PACKED PEFCommandHeader; + + enum + { + kPefCode = 0xC, + kPefData = 0xD, + kPefZero = 0xE, + kPefLinkerID = 0x1, + kPefCount = 4, + kPefInvalid = 0xFF, + }; +} // namespace LibCompiler + +inline std::ofstream& operator<<(std::ofstream& fp, + LibCompiler::PEFContainer& container) +{ + fp.write((char*)&container, sizeof(LibCompiler::PEFContainer)); + return fp; +} + +inline std::ofstream& operator<<(std::ofstream& fp, + LibCompiler::PEFCommandHeader& container) +{ + fp.write((char*)&container, sizeof(LibCompiler::PEFCommandHeader)); + return fp; +} + +inline std::ifstream& operator>>(std::ifstream& fp, + LibCompiler::PEFContainer& container) +{ + fp.read((char*)&container, sizeof(LibCompiler::PEFContainer)); + return fp; +} + +inline std::ifstream& operator>>(std::ifstream& fp, + LibCompiler::PEFCommandHeader& container) +{ + fp.read((char*)&container, sizeof(LibCompiler::PEFCommandHeader)); + return fp; +} diff --git a/dev/LibCompiler/NFC/Ref.h b/dev/LibCompiler/NFC/Ref.h new file mode 100644 index 0000000..f76f676 --- /dev/null +++ b/dev/LibCompiler/NFC/Ref.h @@ -0,0 +1,103 @@ + +/* + * ======================================================== + * + * LibCompiler + * Copyright (C) 2024 Theater Quality Inc, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <LibCompiler/Defines.h> + +namespace LibCompiler +{ + // @author EL Mahrouss Amlal + // @brief Reference holder class, refers to a pointer of data in static memory. + template <typename T> + class Ref final + { + public: + explicit Ref() = default; + + ~Ref() + { + if (m_Strong) + { + (*m_Class).~T(); + } + } + + TOOLCHAINKIT_COPY_DEFAULT(Ref); + + public: + explicit Ref(T cls, const Bool& strong = false) + : m_Class(&cls), m_Strong(strong) + { + } + + Ref& operator=(T ref) + { + *m_Class = ref; + return *this; + } + + public: + T* operator->() const + { + return m_Class; + } + + T& Leak() + { + return *m_Class; + } + + T operator*() + { + return *m_Class; + } + + Bool IsStrong() const + { + return m_Strong; + } + + operator bool() + { + return *m_Class; + } + + private: + T* m_Class{nullptr}; + Bool m_Strong{false}; + }; + + // @author EL Mahrouss Amlal + // @brief Non null Reference holder class, refers to a pointer of data in static memory. + template <typename T> + class NonNullRef final + { + public: + explicit NonNullRef() = delete; + + explicit NonNullRef(T* ref) + : m_Ref(ref, true) + { + } + + Ref<T>& operator->() + { + MUST_PASS(m_Ref); + return m_Ref; + } + + NonNullRef& operator=(const NonNullRef<T>& ref) = delete; + NonNullRef(const NonNullRef<T>& ref) = default; + + private: + Ref<T> m_Ref{nullptr}; + }; +} // namespace LibCompiler diff --git a/dev/LibCompiler/NFC/String.h b/dev/LibCompiler/NFC/String.h new file mode 100644 index 0000000..958da8e --- /dev/null +++ b/dev/LibCompiler/NFC/String.h @@ -0,0 +1,90 @@ +/* + * ======================================================== + * + * LibCompiler + * Copyright (C) 2024 Theater Quality Inc, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <LibCompiler/Defines.h> +#include <LibCompiler/NFC/ErrorOr.h> + +namespace LibCompiler +{ + /** + * @brief StringView class, contains a C string and manages it. + * @note No need to manage it it's getting deleted by default. + */ + + class StringView final + { + public: + explicit StringView() = delete; + + explicit StringView(SizeType Sz) noexcept + : m_Sz(Sz) + { + m_Data = new CharType[Sz]; + assert(m_Data); + } + + ~StringView() noexcept + { + if (m_Data) + { + memset(m_Data, 0, m_Sz); + delete[] m_Data; + + m_Data = nullptr; + } + } + + TOOLCHAINKIT_COPY_DEFAULT(StringView); + + CharType* Data(); + const CharType* CData() const; + SizeType Length() const; + + bool operator==(const CharType* rhs) const; + bool operator!=(const CharType* rhs) const; + + bool operator==(const StringView& rhs) const; + bool operator!=(const StringView& rhs) const; + + StringView& operator+=(const CharType* rhs); + StringView& operator+=(const StringView& rhs); + + operator bool() + { + return m_Data && m_Data[0] != 0; + } + + bool operator!() + { + return !m_Data || m_Data[0] == 0; + } + + private: + CharType* m_Data{nullptr}; + SizeType m_Sz{0}; + SizeType m_Cur{0}; + + friend class StringBuilder; + }; + + /** + * @brief StringBuilder class + * @note These results shall call delete[] after they're used. + */ + struct StringBuilder final + { + static StringView Construct(const CharType* data); + static const char* FromInt(const char* fmt, int n); + static const char* FromBool(const char* fmt, bool n); + static const char* Format(const char* fmt, const char* from); + static bool Equals(const char* lhs, const char* rhs); + }; +} // namespace LibCompiler diff --git a/dev/LibCompiler/NFC/XCOFF.h b/dev/LibCompiler/NFC/XCOFF.h new file mode 100644 index 0000000..0594855 --- /dev/null +++ b/dev/LibCompiler/NFC/XCOFF.h @@ -0,0 +1,41 @@ +/* ------------------------------------------- + + Copyright (C) 2024 Theater Quality Inc, all rights reserved + + File: XCOFF.hpp + Purpose: XCOFF for NewOS. + + Revision History: + + 04/07/24: Added file (amlel) + +------------------------------------------- */ + +#ifndef __XCOFF__ +#define __XCOFF__ + +#include <LibCompiler/Defines.h> + +#define kXCOFF64Magic 0x01F7 + +#define kXCOFFRelFlg 0x0001 +#define kXCOFFExecutable 0x0002 +#define kXCOFFLnno 0x0004 +#define kXCOFFLSyms 0x0008 + +namespace LibCompiler +{ + /// @brief XCoff identification header. + typedef struct XCoffFileHeader + { + UInt16 fMagic; + UInt16 fTarget; + UInt16 fNumSecs; + UInt32 fTimeDat; + UIntPtr fSymPtr; + UInt32 fNumSyms; + UInt16 fOptHdr; // ?: Number of bytes in optional header + } XCoffFileHeader; +} // namespace LibCompiler + +#endif // ifndef __XCOFF__ |
