From 7878653e8dbd65d94ea8ea8bae6e0afd3c3d0af3 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 27 May 2025 23:02:19 +0200 Subject: fix: fix SEGFAULT on CxxDrv (AMD64), caused by a stack corruption. refactor: Refactor LibCompiler's codebase, deperecate older C compilers, and fully focusing on C++ now. Signed-off-by: Amlal El Mahrouss --- dev/LibCompiler/src/BasicString.cc | 207 +++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 dev/LibCompiler/src/BasicString.cc (limited to 'dev/LibCompiler/src/BasicString.cc') diff --git a/dev/LibCompiler/src/BasicString.cc b/dev/LibCompiler/src/BasicString.cc new file mode 100644 index 0000000..78bfcaa --- /dev/null +++ b/dev/LibCompiler/src/BasicString.cc @@ -0,0 +1,207 @@ +/* + * ======================================================== + * + * LibCompiler + * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved. + * + * ======================================================== + */ + +/** + * @file BasicString.cxx + * @author Amlal (amlal@el-mahrouss-logic.com) + * @brief C++ string manipulation API. + * @version 0.2 + * @date 2024-01-23 + * + * @copyright Copyright (c) Amlal El Mahrouss + * + */ + +#include + +namespace LibCompiler { +CharType* BasicString::Data() { + return m_Data; +} + +const CharType* BasicString::CData() const { + return m_Data; +} + +SizeType BasicString::Length() const { + return strlen(m_Data); +} + +bool BasicString::operator==(const BasicString& rhs) const { + if (rhs.Length() != Length()) return false; + + for (SizeType index = 0; index < Length(); ++index) { + if (rhs.m_Data[index] != m_Data[index]) return false; + } + + return true; +} + +bool BasicString::operator==(const CharType* rhs) const { + if (string_length(rhs) != Length()) return false; + + for (SizeType index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != m_Data[index]) return false; + } + + return true; +} + +bool BasicString::operator!=(const BasicString& rhs) const { + if (rhs.Length() != Length()) return false; + + for (SizeType index = 0; index < rhs.Length(); ++index) { + if (rhs.m_Data[index] == m_Data[index]) return false; + } + + return true; +} + +bool BasicString::operator!=(const CharType* rhs) const { + if (string_length(rhs) != Length()) return false; + + for (SizeType index = 0; index < string_length(rhs); ++index) { + if (rhs[index] == m_Data[index]) return false; + } + + return true; +} + +BasicString StringBuilder::Construct(const CharType* data) { + if (!data || *data == 0) return BasicString(0); + + BasicString view(strlen(data)); + view += data; + + return view; +} + +const char* StringBuilder::FromInt(const char* fmt, int i) { + if (!fmt) return ("-1"); + + auto ret_len = 8 + string_length(fmt); + char* ret = new char[ret_len]; + + if (!ret) return ("-1"); + + memset(ret, 0, ret_len); + + CharType result[sizeof(int64_t)]; + + if (!to_str(result, sizeof(int64_t), i)) { + delete[] ret; + return ("-1"); + } + + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(result); + + for (SizeType idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; + + for (auto y_idx = 0; y_idx < res_len; ++y_idx) { + ret[y_idx] = result[result_cnt]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ +} + +const char* StringBuilder::FromBool(const char* fmt, bool i) { + if (!fmt) return ("?"); + + const char* boolean_expr = i ? "true" : "false"; + char* ret = new char[i ? 4 : 5 + string_length(fmt)]; + + if (!ret) return ("?"); + + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(boolean_expr); + + for (SizeType idx = 0; idx < fmt_len; ++idx) { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; + + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = boolean_expr[y_idx]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; +} + +bool StringBuilder::Equals(const char* lhs, const char* rhs) { + if (string_length(rhs) != string_length(lhs)) return false; + + for (SizeType index = 0; index < string_length(rhs); ++index) { + if (rhs[index] != lhs[index]) return false; + } + + return true; +} + +const char* StringBuilder::Format(const char* fmt, const char* fmtRight) { + if (!fmt || !fmtRight) return ("?"); + + char* ret = new char[string_length(fmtRight) + string_length(fmtRight)]; + if (!ret) return ("?"); + + for (SizeType idx = 0; idx < string_length(fmt); ++idx) { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; + + for (SizeType y_idx = 0; y_idx < string_length(fmtRight); ++y_idx) { + ret[result_cnt] = fmtRight[y_idx]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; +} + +BasicString& BasicString::operator+=(const CharType* rhs) { + if (strlen(rhs) > this->m_Sz) { + throw std::runtime_error("out_of_bounds: BasicString"); + } + + memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); + this->m_Cur += strlen(rhs); + + return *this; +} + +BasicString& BasicString::operator+=(const BasicString& rhs) { + if (rhs.m_Cur > this->m_Sz) { + throw std::runtime_error("out_of_bounds: BasicString"); + } + + memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData())); + this->m_Cur += strlen(rhs.CData()); + + return *this; +} +} // namespace LibCompiler -- cgit v1.2.3 From d37f1a7381825d414e4b71c487eea509325f24c3 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 28 May 2025 18:12:17 +0200 Subject: refactor: Refactor toolchain source code. Signed-off-by: Amlal El Mahrouss --- dev/LibCompiler/AE.h | 14 +++++++------- dev/LibCompiler/BasicString.h | 16 ++++++++-------- dev/LibCompiler/CodeGen.h | 8 ++++---- dev/LibCompiler/Defines.h | 4 ++-- dev/LibCompiler/PEF.h | 4 ++-- dev/LibCompiler/Util/DylibHelpers.h | 2 +- dev/LibCompiler/src/Backend/AssemblerARM64.cc | 2 +- dev/LibCompiler/src/Backend/AssemblerPowerPC.cc | 2 +- dev/LibCompiler/src/BasicString.cc | 14 +++++++------- dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc | 6 +++--- tools/cxxdrv.cc | 2 +- tools/dbg.cc | 4 ++-- tools/kdbg.cc | 4 ++-- 13 files changed, 41 insertions(+), 41 deletions(-) (limited to 'dev/LibCompiler/src/BasicString.cc') diff --git a/dev/LibCompiler/AE.h b/dev/LibCompiler/AE.h index fcc37a5..8c05c3a 100644 --- a/dev/LibCompiler/AE.h +++ b/dev/LibCompiler/AE.h @@ -28,14 +28,14 @@ namespace LibCompiler { // One thing to keep in mind. // This object format, is reloctable. typedef struct AEHeader final { - CharType fMagic[kAEMagLen]; - CharType fArch; - CharType fSubArch; + Char fMagic[kAEMagLen]; + Char fArch; + Char fSubArch; SizeType fCount; - CharType fSize; + Char fSize; SizeType fStartCode; SizeType fCodeSize; - CharType fPad[kAEPad]; + Char fPad[kAEPad]; } PACKED AEHeader, *AEHeaderPtr; // @brief Advanced Executable Record. @@ -43,12 +43,12 @@ typedef struct AEHeader final { // fKind must be filled with PEF fields. typedef struct AERecordHeader final { - CharType fName[kAESymbolLen]; + Char fName[kAESymbolLen]; SizeType fKind; SizeType fSize; SizeType fFlags; UIntPtr fOffset; - CharType fPad[kAEPad]; + Char fPad[kAEPad]; } PACKED AERecordHeader, *AERecordHeaderPtr; enum { diff --git a/dev/LibCompiler/BasicString.h b/dev/LibCompiler/BasicString.h index 53b21d3..5af9da9 100644 --- a/dev/LibCompiler/BasicString.h +++ b/dev/LibCompiler/BasicString.h @@ -26,7 +26,7 @@ class BasicString final { explicit BasicString() = delete; explicit BasicString(SizeType Sz) noexcept : m_Sz(Sz) { - m_Data = new CharType[Sz]; + m_Data = new Char[Sz]; assert(m_Data); } @@ -41,17 +41,17 @@ class BasicString final { LIBCOMPILER_COPY_DEFAULT(BasicString); - CharType* Data(); - const CharType* CData() const; + Char* Data(); + const Char* CData() const; SizeType Length() const; - bool operator==(const CharType* rhs) const; - bool operator!=(const CharType* rhs) const; + bool operator==(const Char* rhs) const; + bool operator!=(const Char* rhs) const; bool operator==(const BasicString& rhs) const; bool operator!=(const BasicString& rhs) const; - BasicString& operator+=(const CharType* rhs); + BasicString& operator+=(const Char* rhs); BasicString& operator+=(const BasicString& rhs); operator bool() { return m_Data && m_Data[0] != 0; } @@ -59,7 +59,7 @@ class BasicString final { bool operator!() { return !m_Data || m_Data[0] == 0; } private: - CharType* m_Data{nullptr}; + Char* m_Data{nullptr}; SizeType m_Sz{0}; SizeType m_Cur{0}; @@ -71,7 +71,7 @@ class BasicString final { * @note These results shall call be delete[] after they're used. */ struct StringBuilder final { - static BasicString Construct(const CharType* data); + static BasicString Construct(const Char* 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); diff --git a/dev/LibCompiler/CodeGen.h b/dev/LibCompiler/CodeGen.h index a189b93..b932cdf 100644 --- a/dev/LibCompiler/CodeGen.h +++ b/dev/LibCompiler/CodeGen.h @@ -74,7 +74,7 @@ union NumberCast64 final { ~NumberCast64() { raw = 0; } - CharType number[8]; + Char number[8]; UInt64 raw; }; @@ -84,7 +84,7 @@ union NumberCast32 final { ~NumberCast32() { raw = 0; } - CharType number[4]; + Char number[4]; UInt32 raw; }; @@ -94,7 +94,7 @@ union NumberCast16 final { ~NumberCast16() { raw = 0; } - CharType number[2]; + Char number[2]; UInt16 raw; }; @@ -104,7 +104,7 @@ union NumberCast8 final { ~NumberCast8() { raw = 0; } - CharType number; + Char number; UInt8 raw; }; diff --git a/dev/LibCompiler/Defines.h b/dev/LibCompiler/Defines.h index 24d9a96..1e1d4d9 100644 --- a/dev/LibCompiler/Defines.h +++ b/dev/LibCompiler/Defines.h @@ -48,7 +48,7 @@ #define Int8 int8_t #define UInt8 uint8_t -#define CharType char +#define Char char #define Boolean bool #include @@ -118,7 +118,7 @@ inline STLString current_date() noexcept { return fmt; } -inline bool to_str(CharType* str, Int32 limit, Int32 base) noexcept { +inline bool to_str(Char* str, Int32 limit, Int32 base) noexcept { if (limit == 0) return false; Int32 copy_limit = limit; diff --git a/dev/LibCompiler/PEF.h b/dev/LibCompiler/PEF.h index f2662c8..3f5a2b1 100644 --- a/dev/LibCompiler/PEF.h +++ b/dev/LibCompiler/PEF.h @@ -71,7 +71,7 @@ enum { /* PEF container */ typedef struct PEFContainer final { - CharType Magic[kPefMagicLen]; + Char Magic[kPefMagicLen]; UInt32 Linker; /* Linker used to link executable */ UInt32 Version; UInt32 Kind; @@ -90,7 +90,7 @@ typedef struct PEFContainer final { /* PEF executable section and commands. */ typedef struct PEFCommandHeader final { - CharType Name[kPefNameLen]; /* container name */ + Char Name[kPefNameLen]; /* container name */ UInt32 Cpu; /* container cpu */ UInt32 SubCpu; /* container sub-cpu */ UInt32 Flags; /* container flags */ diff --git a/dev/LibCompiler/Util/DylibHelpers.h b/dev/LibCompiler/Util/DylibHelpers.h index e61cf9c..be8119f 100644 --- a/dev/LibCompiler/Util/DylibHelpers.h +++ b/dev/LibCompiler/Util/DylibHelpers.h @@ -9,5 +9,5 @@ #include #include -typedef Int32 (*LibCompilerEntrypoint)(Int32 argc, CharType const* argv[]); +typedef Int32 (*LibCompilerEntrypoint)(Int32 argc, Char const* argv[]); typedef VoidPtr DylibHandle; diff --git a/dev/LibCompiler/src/Backend/AssemblerARM64.cc b/dev/LibCompiler/src/Backend/AssemblerARM64.cc index 142dcc7..a1cc6dc 100644 --- a/dev/LibCompiler/src/Backend/AssemblerARM64.cc +++ b/dev/LibCompiler/src/Backend/AssemblerARM64.cc @@ -45,7 +45,7 @@ constexpr auto cPowerIPAlignment = 0x1U; -static CharType kOutputArch = LibCompiler::kPefArchARM64; +static Char kOutputArch = LibCompiler::kPefArchARM64; static std::size_t kCounter = 1UL; diff --git a/dev/LibCompiler/src/Backend/AssemblerPowerPC.cc b/dev/LibCompiler/src/Backend/AssemblerPowerPC.cc index 3134748..b979f64 100644 --- a/dev/LibCompiler/src/Backend/AssemblerPowerPC.cc +++ b/dev/LibCompiler/src/Backend/AssemblerPowerPC.cc @@ -45,7 +45,7 @@ constexpr auto cPowerIPAlignment = 0x4U; -static CharType kOutputArch = LibCompiler::kPefArchPowerPC; +static Char kOutputArch = LibCompiler::kPefArchPowerPC; static std::size_t kCounter = 1UL; diff --git a/dev/LibCompiler/src/BasicString.cc b/dev/LibCompiler/src/BasicString.cc index 78bfcaa..41989fe 100644 --- a/dev/LibCompiler/src/BasicString.cc +++ b/dev/LibCompiler/src/BasicString.cc @@ -21,11 +21,11 @@ #include namespace LibCompiler { -CharType* BasicString::Data() { +Char* BasicString::Data() { return m_Data; } -const CharType* BasicString::CData() const { +const Char* BasicString::CData() const { return m_Data; } @@ -43,7 +43,7 @@ bool BasicString::operator==(const BasicString& rhs) const { return true; } -bool BasicString::operator==(const CharType* rhs) const { +bool BasicString::operator==(const Char* rhs) const { if (string_length(rhs) != Length()) return false; for (SizeType index = 0; index < string_length(rhs); ++index) { @@ -63,7 +63,7 @@ bool BasicString::operator!=(const BasicString& rhs) const { return true; } -bool BasicString::operator!=(const CharType* rhs) const { +bool BasicString::operator!=(const Char* rhs) const { if (string_length(rhs) != Length()) return false; for (SizeType index = 0; index < string_length(rhs); ++index) { @@ -73,7 +73,7 @@ bool BasicString::operator!=(const CharType* rhs) const { return true; } -BasicString StringBuilder::Construct(const CharType* data) { +BasicString StringBuilder::Construct(const Char* data) { if (!data || *data == 0) return BasicString(0); BasicString view(strlen(data)); @@ -92,7 +92,7 @@ const char* StringBuilder::FromInt(const char* fmt, int i) { memset(ret, 0, ret_len); - CharType result[sizeof(int64_t)]; + Char result[sizeof(int64_t)]; if (!to_str(result, sizeof(int64_t), i)) { delete[] ret; @@ -183,7 +183,7 @@ const char* StringBuilder::Format(const char* fmt, const char* fmtRight) { return ret; } -BasicString& BasicString::operator+=(const CharType* rhs) { +BasicString& BasicString::operator+=(const Char* rhs) { if (strlen(rhs) > this->m_Sz) { throw std::runtime_error("out_of_bounds: BasicString"); } diff --git a/dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc b/dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc index 820b06d..b58c786 100644 --- a/dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc +++ b/dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc @@ -68,7 +68,7 @@ namespace Detail { struct DynamicLinkerBlob final { - std::vector mBlob{}; // PEF code/bss/data blob. + std::vector mBlob{}; // PEF code/bss/data blob. UIntPtr mOffset{0UL}; // the offset of the PEF container header... }; } // namespace Detail @@ -89,8 +89,8 @@ static Bool kStartFound = false; static Bool kDuplicateSymbols = false; /* ld64 is to be found, mld is to be found at runtime. */ -static const CharType* kLdDefineSymbol = ":UndefinedSymbol:"; -static const CharType* kLdDynamicSym = ":RuntimeSymbol:"; +static const Char* kLdDefineSymbol = ":UndefinedSymbol:"; +static const Char* kLdDynamicSym = ":RuntimeSymbol:"; /* object code and list. */ static std::vector kObjectList; diff --git a/tools/cxxdrv.cc b/tools/cxxdrv.cc index bc790ff..99696aa 100644 --- a/tools/cxxdrv.cc +++ b/tools/cxxdrv.cc @@ -15,7 +15,7 @@ static auto kPath = "/usr/local/lib/libCompiler.dylib"; -Int32 main(Int32 argc, CharType const* argv[]) { +Int32 main(Int32 argc, Char const* argv[]) { DylibHandle handler = dlopen(kPath, RTLD_LAZY | RTLD_GLOBAL); if (!handler) { diff --git a/tools/dbg.cc b/tools/dbg.cc index 1d89c21..35b0702 100644 --- a/tools/dbg.cc +++ b/tools/dbg.cc @@ -9,10 +9,10 @@ /// @file dbg.cxx /// @brief NE debugger. -LC_IMPORT_C int DebuggerMachPOSIX(int argc, char const* argv[]); +LC_IMPORT_C Int32 DebuggerMachPOSIX(Int32 argc, Char const* argv[]); /// @brief Debugger entrypoint. /// @return Status code of debugger. -int main(int argc, char const* argv[]) { +Int32 main(Int32 argc, Char const* argv[]) { return DebuggerMachPOSIX(argc, argv); } diff --git a/tools/kdbg.cc b/tools/kdbg.cc index 08a0465..7616abe 100644 --- a/tools/kdbg.cc +++ b/tools/kdbg.cc @@ -9,10 +9,10 @@ /// @file kdbg.cxx /// @brief NeKernel debugger. -LC_IMPORT_C int DebuggerNeKernel(int argc, char const* argv[]); +LC_IMPORT_C Int32 DebuggerNeKernel(Int32 argc, Char const* argv[]); /// @brief Debugger entrypoint. /// @return Status code of debugger. -int main(int argc, char const* argv[]) { +Int32 main(Int32 argc, Char const* argv[]) { return DebuggerNeKernel(argc, argv); } -- cgit v1.2.3