Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ This is just a little personal experiment i'm doing with llvm, the code is not g
Join the project [Discord][dis] server!

### Name
"LLVM360" LLVM + xbox360, but it's just a temporany, i don't really like it so will probably change
"LLVM360" LLVM + xbox360, but it's just temporary, i don't really like it so will probably change

## Building
- Clang compiler is required
- Git Clone this repository
- idk that's it
- If everything is good and i or you didn't messed up something, it should compile fine
- Git Clone this repository by doing `git clone https://github.com/AleBello7276/LLVM360 --recursive --shallow-submodules`, so it doesnt take 500 years to download llvm (but it will still take a long time because it's 1.4gb which is stupid)
- Do `cmake .`
- If everything is good and i or you didn't mess something up, it should compile fine

## Contributing
if want to contribute, make a fork and a PR :} also join the discord server!


if want to contribute, make a fork and a PR :} also join the discord server!

[dis]: https://discord.gg/JufwFS9mmf
1 change: 0 additions & 1 deletion llvm360/Emulator/src/Shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

#include <cstdint>
#include <cstdio>
#include <windows.h>
#include <iostream>

16 changes: 10 additions & 6 deletions llvm360/Emulator/src/emulator.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#include "Shared.h"
#include "Naive+.h"
#include <locale>
#include <codecvt>





int main()
int main(int argc, char *argv[])
{

PBinaryHandle* handle = TranslateBinary(L"./kernel17559.exe", false, true);
std::wstring path;
if (argc < 2) {
path = L"./kernel17559.exe";
} else {
path = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(argv[1]);
}
PBinaryHandle* handle = TranslateBinary(path, false, true);
printf("YEY");
return 0;
}
4 changes: 2 additions & 2 deletions llvm360/Naive+/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(Naive+)

set(Loader
src/Loader/PEImage.h
src/Loader/XexImage.h
src/Loader/XEXImage.h
src/Loader/ImageLoader.cpp
src/Loader/ImageLoader.h
src/Loader/table/ImportTable.h
Expand Down Expand Up @@ -43,4 +43,4 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
add_library(Naive+ SHARED ${CMAKE_CURRENT_SOURCE_DIR}/${SRC})
target_link_libraries(Naive+ PRIVATE ext_llvm)
target_compile_definitions(Naive+ PRIVATE NAIVE_EXPORT)
target_include_directories(Naive+ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/Naive+)
target_include_directories(Naive+ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/Naive+)
2 changes: 1 addition & 1 deletion llvm360/Naive+/src/Decoder/InstructionRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void InstructionRegistry::InitialiseOpCodeKey(uint32_t mainOP, uint32_t extMASK)
void InstructionRegistry::AddDescriptorToKey(uint32_t mainOP, std::string mnemonic, FormType type, uint32_t extOP)
{
auto it = m_mainOPs.find(mainOP);
if (it == m_mainOPs.end()) { printf("AddDescriptorToKey, Something is wrong :/, did you registerInstructions for this main Opcode? "); _CrtDbgBreak(); }
if (it == m_mainOPs.end()) { printf("AddDescriptorToKey, Something is wrong :/, did you registerInstructions for this main Opcode? "); BREAKPOINT(); }
OpcodeKey& key = it->second;
uint32_t shift = __builtin_ctz(key.m_extMASK);
InstructionDescriptor desc = { mnemonic, type };
Expand Down
9 changes: 7 additions & 2 deletions llvm360/Naive+/src/Decoder/InstructionRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#include <unordered_map>
#include <string>

#ifdef _WIN32
#define BREAKPOINT _CrtDbgBreak
#else
#define BREAKPOINT __builtin_trap
#endif

enum FormType
{
Expand Down Expand Up @@ -173,12 +178,12 @@ struct InstructionRegistry

// get OpcodeKey by main Opcode
auto it = m_mainOPs.find(operands.DEF.OPCD);
if (it == m_mainOPs.end()) { printf("Instruction::DecodeInstr %s : %d", "MAIN OPCODE HAS NO KEY", operands.DEF.OPCD); _CrtDbgBreak(); }
if (it == m_mainOPs.end()) { printf("Instruction::DecodeInstr %s : %d", "MAIN OPCODE HAS NO KEY", operands.DEF.OPCD); BREAKPOINT(); }

OpcodeKey& key = it->second;
uint32_t extOpcode = (data & key.m_extMASK);
auto itDesc = key.m_descriptors.find(extOpcode);
if (itDesc == key.m_descriptors.end()) { printf("Instruction::DecodeInstr %s %d", "OpCodeKey HAS NO DESCRIPTOR FOR THIS EXTOP", extOpcode >> (__builtin_ctz(key.m_extMASK))); _CrtDbgBreak(); }
if (itDesc == key.m_descriptors.end()) { printf("Instruction::DecodeInstr %s %d", "OpCodeKey HAS NO DESCRIPTOR FOR THIS EXTOP", extOpcode >> (__builtin_ctz(key.m_extMASK))); BREAKPOINT(); }

InstructionDescriptor& desc = itDesc->second;
instr.address = address;
Expand Down
2 changes: 1 addition & 1 deletion llvm360/Naive+/src/LLVM360.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void PBinaryHandle::LoadBinary()
while (address <= end)
{
// get and byteswap
uint32_t data = __bswapd( (uint32_t) * (uint32_t*)(secDataPtr + (address - start)) );
uint32_t data = __bswap_32( (uint32_t) * (uint32_t*)(secDataPtr + (address - start)) );
Instruction instruction = registry.DecodeInstr(data, address);
//if (instructionSize == 0)
//{
Expand Down
12 changes: 10 additions & 2 deletions llvm360/Naive+/src/Loader/ImageLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <fstream>
#include <memory>
#include <cstring>
#ifndef _WIN32
#include <locale>
#include <codecvt>
#endif

namespace XLoader
{
Expand Down Expand Up @@ -47,7 +51,11 @@ namespace XLoader
// ImageLoader implementation
std::unique_ptr<IImage> ImageLoader::load(const std::wstring& path) {
// Open file
#ifdef _WIN32
std::ifstream file(path, std::ios::binary | std::ios::ate);
#else
std::ifstream file(std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(path), std::ios::binary | std::ios::ate);
#endif
if (!file.is_open()) {
printf("Failed to open file\n");
return nullptr;
Expand Down Expand Up @@ -306,7 +314,7 @@ namespace XLoader
auto& importDir = m_optHeader.dataDirectories[1];
if (importDir.VirtualAddress != 0 && importDir.Size != 0) {
// Would parse import descriptors here
printf("Import directory found at RVA 0x%08X\n", importDir.VirtualAddress);
printf("Import directory found at RVA 0x%08lX\n", importDir.VirtualAddress);
}
}

Expand Down Expand Up @@ -848,4 +856,4 @@ import->funcImportAddr = recordAddr;

return true;
}
}
}
13 changes: 12 additions & 1 deletion llvm360/Naive+/src/Loader/PEImage.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#pragma once
#include "ImageLoader.h"
#ifdef _WIN32
#include <windows.h>
#else
typedef unsigned long DWORD;
typedef struct _IMAGE_DATA_DIRECTORY {
DWORD VirtualAddress;
DWORD Size;
} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
#define IMAGE_SCN_MEM_EXECUTE 0x20000000
#define IMAGE_SCN_MEM_READ 0x40000000
#define IMAGE_SCN_MEM_WRITE 0x80000000
#endif



Expand Down Expand Up @@ -126,4 +137,4 @@ namespace XLoader
std::vector<std::unique_ptr<Import>> m_imports;
};

}
}
10 changes: 5 additions & 5 deletions llvm360/Naive+/src/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ void LOG_PRINT(LogLevel level, const char* name, const char* fmt, ...)
printf("\033[0m\n");
}

#define LOG_INFO(name, fmt, ...) LOG_PRINT(LogLevel::LEVEL_INFO, name, fmt, __VA_ARGS__)
#define LOG_WARNING(name, fmt, ...) LOG_PRINT(LogLevel::LEVEL_WARNING, name, fmt, __VA_ARGS__)
#define LOG_ERROR(name, fmt, ...) LOG_PRINT(LogLevel::LEVEL_ERROR, name, fmt, __VA_ARGS__)
#define LOG_DEBUG(name, fmt, ...) LOG_PRINT(LogLevel::LEVEL_DEBUG, name, fmt, __VA_ARGS__)
#define LOG_FATAL(name, fmt, ...) LOG_PRINT(LogLevel::LEVEL_FATAL, name, fmt, __VA_ARGS__)
#define LOG_INFO(name, fmt, args...) LOG_PRINT(LogLevel::LEVEL_INFO, name, fmt, ## args)
#define LOG_WARNING(name, fmt, args...) LOG_PRINT(LogLevel::LEVEL_WARNING, name, fmt, ## args)
#define LOG_ERROR(name, fmt, args...) LOG_PRINT(LogLevel::LEVEL_ERROR, name, fmt, ## args)
#define LOG_DEBUG(name, fmt, args...) LOG_PRINT(LogLevel::LEVEL_DEBUG, name, fmt, ## args)
#define LOG_FATAL(name, fmt, args...) LOG_PRINT(LogLevel::LEVEL_FATAL, name, fmt, ## args)


6 changes: 4 additions & 2 deletions llvm360/Naive+/src/Naive+/Naive+.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#include <stdint.h>


#ifdef NAIVE_EXPORT
//#ifndef NAIVE_EXPORT
#ifdef _WIN32
#define NAIVE_EXPORT __declspec(dllexport)
#else
#define NAIVE_EXPORT __declspec(dllimport)
#define NAIVE_EXPORT __attribute__((visibility("default")))
#endif
//#endif


struct Instruction;
Expand Down
6 changes: 5 additions & 1 deletion llvm360/Naive+/src/Shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
#include <stdio.h>
#include <sstream>
#include <iomanip>
#ifdef _WIN32
#include <conio.h>
#include <Windows.h>
#else
#include <cstdarg>
#endif
#include <chrono>
#include <functional>
#include <Windows.h>


#include "Decoder/InstructionRegistry.h"
Expand Down