summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/CompilerKit/Detail/AMD64.h7
-rw-r--r--include/CompilerKit/MachO.h48
-rw-r--r--include/CompilerKit/Utilities/Assembler.h40
-rw-r--r--include/CompilerKit/XCOFF.h35
4 files changed, 62 insertions, 68 deletions
diff --git a/include/CompilerKit/Detail/AMD64.h b/include/CompilerKit/Detail/AMD64.h
index 8f16968..f1c0671 100644
--- a/include/CompilerKit/Detail/AMD64.h
+++ b/include/CompilerKit/Detail/AMD64.h
@@ -39,13 +39,18 @@ struct CpuOpcodeAMD64 {
#define kJumpLimitStandard 0xE3
#define kJumpLimitStandardLimit 0xEB
+/// @brief Base opcodes for push/pop instructions
+#define kAsmPushOpcode 0x50
+#define kAsmPopOpcode 0x58
+
inline std::vector<CpuOpcodeAMD64> kOpcodesAMD64 = {
CK_ASM_OPCODE("int", 0xCD) CK_ASM_OPCODE("into", 0xCE) CK_ASM_OPCODE("intd", 0xF1)
CK_ASM_OPCODE("int3", 0xC3) CK_ASM_OPCODE("iret", 0xCF) CK_ASM_OPCODE("retf", 0xCB)
CK_ASM_OPCODE("retn", 0xC3) CK_ASM_OPCODE("ret", 0xC3) CK_ASM_OPCODE("sti", 0xfb)
CK_ASM_OPCODE("cli", 0xfa) CK_ASM_OPCODE("hlt", 0xf4) CK_ASM_OPCODE("nop", 0x90)
CK_ASM_OPCODE("mov", 0x48) CK_ASM_OPCODE("call", 0xFF)
- CK_ASM_OPCODE("syscall", 0x0F) CK_ASM_OPCODE("xor", 0x48)};
+ CK_ASM_OPCODE("syscall", 0x0F) CK_ASM_OPCODE("xor", 0x48) CK_ASM_OPCODE(
+ "push", kAsmPushOpcode) CK_ASM_OPCODE("pop", kAsmPopOpcode)};
#define kAsmRegisterLimit 16
diff --git a/include/CompilerKit/MachO.h b/include/CompilerKit/MachO.h
new file mode 100644
index 0000000..a22fa9d
--- /dev/null
+++ b/include/CompilerKit/MachO.h
@@ -0,0 +1,48 @@
+// Copyright 2026, Amlal El Mahrouss (amlal@nekernel.org)
+// Licensed under the Apache License, Version 2.0 (See accompanying
+// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0)
+// Official repository: https://github.com/nekernel-org/nectar
+
+#ifndef _NECTAR_MACHO_H_
+#define _NECTAR_MACHO_H_
+
+#include <CompilerKit/Detail/Config.h>
+
+#include <mach-o/loader.h>
+#include <mach-o/nlist.h>
+
+namespace CompilerKit {
+namespace MachO {
+
+ /// @brief Mach-O segment names
+ constexpr const char* kSegmentText = "__TEXT";
+ constexpr const char* kSegmentData = "__DATA";
+ constexpr const char* kSegmentLinkedit = "__LINKEDIT";
+
+ /// @brief Mach-O section names
+ constexpr const char* kSectionText = "__text";
+ constexpr const char* kSectionData = "__data";
+ constexpr const char* kSectionBss = "__bss";
+
+ /// @brief Default base address for Mach-O executables
+ constexpr uint64_t kDefaultBaseAddress = 0x100000000ULL;
+
+ /// @brief Page size for alignment
+ constexpr uint64_t kPageSize = 0x4000ULL; // 16KB for arm64, also works for x86_64
+
+ /// @brief Section alignment (2^4 = 16 bytes)
+ constexpr uint32_t kSectionAlign = 4;
+
+ /// @brief Helper to align a value to page boundary
+ inline uint64_t AlignToPage(uint64_t value) { return (value + kPageSize - 1) & ~(kPageSize - 1); }
+
+ /// @brief Helper to copy segment/section name safely
+ inline void CopySegmentName(char* dest, const char* src) {
+ std::memset(dest, 0, 16);
+ std::strncpy(dest, src, 16);
+ }
+
+} // namespace MachO
+} // namespace CompilerKit
+
+#endif // ifndef _NECTAR_MACHO_H_
diff --git a/include/CompilerKit/Utilities/Assembler.h b/include/CompilerKit/Utilities/Assembler.h
index 249d69d..9f81dcf 100644
--- a/include/CompilerKit/Utilities/Assembler.h
+++ b/include/CompilerKit/Utilities/Assembler.h
@@ -28,14 +28,8 @@ inline NumberCast32 GetNumber32(STLString lineBuffer, STLString numberKey) {
switch (lineBuffer[pos + 1]) {
case 'x': {
- if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 16); !res) {
- if (errno != 0) {
- Detail::print_error("invalid hex number: " + lineBuffer, "CompilerKit");
- throw std::runtime_error("invalid_hex");
- }
- }
-
- NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 16));
+ auto res = strtol(lineBuffer.substr(pos + 2).c_str(), nullptr, 16);
+ NumberCast32 numOffset(strtol(lineBuffer.substr(pos + 2).c_str(), nullptr, 16));
if (kVerbose) {
kStdOut << "asm: found a base 16 number here: " << lineBuffer.substr(pos) << "\n";
@@ -44,14 +38,8 @@ inline NumberCast32 GetNumber32(STLString lineBuffer, STLString numberKey) {
return numOffset;
}
case 'b': {
- if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 2); !res) {
- if (errno != 0) {
- Detail::print_error("invalid binary number:" + lineBuffer, "CompilerKit");
- throw std::runtime_error("invalid_bin");
- }
- }
-
- NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 2));
+ auto res = strtol(lineBuffer.substr(pos + 2).c_str(), nullptr, 2);
+ NumberCast32 numOffset(strtol(lineBuffer.substr(pos + 2).c_str(), nullptr, 2));
if (kVerbose) {
kStdOut << "asm: found a base 2 number here:" << lineBuffer.substr(pos) << "\n";
@@ -60,14 +48,8 @@ inline NumberCast32 GetNumber32(STLString lineBuffer, STLString numberKey) {
return numOffset;
}
case 'o': {
- if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 7); !res) {
- if (errno != 0) {
- Detail::print_error("invalid octal number: " + lineBuffer, "CompilerKit");
- throw std::runtime_error("invalid_octal");
- }
- }
-
- NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 7));
+ auto res = strtol(lineBuffer.substr(pos + 2).c_str(), nullptr, 8);
+ NumberCast32 numOffset(strtol(lineBuffer.substr(pos + 2).c_str(), nullptr, 8));
if (kVerbose) {
kStdOut << "asm: found a base 8 number here:" << lineBuffer.substr(pos) << "\n";
@@ -76,14 +58,8 @@ inline NumberCast32 GetNumber32(STLString lineBuffer, STLString numberKey) {
return numOffset;
}
default: {
- if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 10); !res) {
- if (errno != 0) {
- Detail::print_error("invalid hex number: " + lineBuffer, "CompilerKit");
- throw std::runtime_error("invalid_hex");
- }
- }
-
- NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 10));
+ auto res = strtol(lineBuffer.substr(pos + 2).c_str(), nullptr, 10);
+ NumberCast32 numOffset(strtol(lineBuffer.substr(pos + 2).c_str(), nullptr, 10));
if (kVerbose) {
kStdOut << "asm: found a base 10 number here:" << lineBuffer.substr(pos) << kStdEndl;
diff --git a/include/CompilerKit/XCOFF.h b/include/CompilerKit/XCOFF.h
deleted file mode 100644
index 45a116a..0000000
--- a/include/CompilerKit/XCOFF.h
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
-// Licensed under the Apache License, Version 2.0 (See accompanying
-// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0)
-// Official repository: https://github.com/nekernel-org/nectar
-
-#ifndef _NECTAR_XCOFF_H_
-#define _NECTAR_XCOFF_H_
-
-#include <CompilerKit/Detail/Config.h>
-
-#define kXCOFF64Magic 0x01F7
-
-#define kXCOFFRelFlg 0x0001
-#define kXCOFFExecutable 0x0002
-#define kXCOFFLnno 0x0004
-#define kXCOFFLSyms 0x0008
-
-namespace CompilerKit {
-struct XCoffFileHeader;
-
-/// @brief XCoff file 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;
-
-typedef struct XCoffFileHeader* XCoffFileHeaderPtr;
-} // namespace CompilerKit
-
-#endif // ifndef _NECTAR_XCOFF_H_