diff --git a/CMakeLists.txt b/CMakeLists.txt index 0606bdb..a1095c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,10 @@ endif () set(CMAKE_CXX_STANDARD 20) add_executable(Compiler - src/ELF.cpp include/ELF.h + src/ELF64_Sym.cpp include/ELF64_Sym.h + src/ELF64_Section.cpp include/ELF64_Section.h + src/ELF64_Program.cpp include/ELF64_Program.h + src/ELF64.cpp include/ELF64.h src/main.cpp ) diff --git a/cmake-build-debug/.cmake/api/v1/reply/cmakeFiles-v1-c002049034cb93e91036.json b/cmake-build-debug/.cmake/api/v1/reply/cmakeFiles-v1-c002049034cb93e91036.json deleted file mode 100644 index caca1f4..0000000 --- a/cmake-build-debug/.cmake/api/v1/reply/cmakeFiles-v1-c002049034cb93e91036.json +++ /dev/null @@ -1,156 +0,0 @@ -{ - "inputs" : - [ - { - "path" : "CMakeLists.txt" - }, - { - "isGenerated" : true, - "path" : "cmake-build-debug/CMakeFiles/3.27.8/CMakeSystem.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeSystemSpecificInitialize.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Platform/Linux-Initialize.cmake" - }, - { - "isGenerated" : true, - "path" : "cmake-build-debug/CMakeFiles/3.27.8/CMakeCCompiler.cmake" - }, - { - "isGenerated" : true, - "path" : "cmake-build-debug/CMakeFiles/3.27.8/CMakeCXXCompiler.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeSystemSpecificInformation.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeGenericSystem.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeInitializeConfigs.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Platform/Linux.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Platform/UnixPaths.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeCInformation.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeLanguageInformation.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Compiler/GNU-C.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Compiler/GNU.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Compiler/CMakeCommonCompilerMacros.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Platform/Linux-GNU-C.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Platform/Linux-GNU.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeCommonLanguageInclude.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeCXXInformation.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeLanguageInformation.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Compiler/GNU-CXX.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Compiler/GNU.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Platform/Linux-GNU-CXX.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/Platform/Linux-GNU.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/CMakeCommonLanguageInclude.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/FindVulkan.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake" - }, - { - "isCMake" : true, - "isExternal" : true, - "path" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27/Modules/FindPackageMessage.cmake" - } - ], - "kind" : "cmakeFiles", - "paths" : - { - "build" : "/home/karutoh/CLionProjects/Compiler/cmake-build-debug", - "source" : "/home/karutoh/CLionProjects/Compiler" - }, - "version" : - { - "major" : 1, - "minor" : 0 - } -} diff --git a/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-7763de202a4897ee489a.json b/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-7763de202a4897ee489a.json deleted file mode 100644 index 88c17b6..0000000 --- a/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-7763de202a4897ee489a.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "configurations" : - [ - { - "directories" : - [ - { - "build" : ".", - "jsonFile" : "directory-.-Debug-f5ebdc15457944623624.json", - "minimumCMakeVersion" : - { - "string" : "3.18.4" - }, - "projectIndex" : 0, - "source" : ".", - "targetIndexes" : - [ - 0 - ] - } - ], - "name" : "Debug", - "projects" : - [ - { - "directoryIndexes" : - [ - 0 - ], - "name" : "Compiler", - "targetIndexes" : - [ - 0 - ] - } - ], - "targets" : - [ - { - "directoryIndex" : 0, - "id" : "Compiler::@6890427a1f51a3e7e1df", - "jsonFile" : "target-Compiler-Debug-b504b707cc3938b58fc6.json", - "name" : "Compiler", - "projectIndex" : 0 - } - ] - } - ], - "kind" : "codemodel", - "paths" : - { - "build" : "/home/karutoh/CLionProjects/Compiler/cmake-build-debug", - "source" : "/home/karutoh/CLionProjects/Compiler" - }, - "version" : - { - "major" : 2, - "minor" : 6 - } -} diff --git a/cmake-build-debug/.cmake/api/v1/reply/index-2024-02-14T13-33-01-0401.json b/cmake-build-debug/.cmake/api/v1/reply/index-2024-02-14T13-33-01-0401.json deleted file mode 100644 index 07e836f..0000000 --- a/cmake-build-debug/.cmake/api/v1/reply/index-2024-02-14T13-33-01-0401.json +++ /dev/null @@ -1,108 +0,0 @@ -{ - "cmake" : - { - "generator" : - { - "multiConfig" : false, - "name" : "Ninja" - }, - "paths" : - { - "cmake" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/bin/cmake", - "cpack" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/bin/cpack", - "ctest" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/bin/ctest", - "root" : "/home/karutoh/.local/share/JetBrains/Toolbox/apps/clion/bin/cmake/linux/x64/share/cmake-3.27" - }, - "version" : - { - "isDirty" : false, - "major" : 3, - "minor" : 27, - "patch" : 8, - "string" : "3.27.8", - "suffix" : "" - } - }, - "objects" : - [ - { - "jsonFile" : "codemodel-v2-7763de202a4897ee489a.json", - "kind" : "codemodel", - "version" : - { - "major" : 2, - "minor" : 6 - } - }, - { - "jsonFile" : "cache-v2-525bf0c12142f71983da.json", - "kind" : "cache", - "version" : - { - "major" : 2, - "minor" : 0 - } - }, - { - "jsonFile" : "cmakeFiles-v1-c002049034cb93e91036.json", - "kind" : "cmakeFiles", - "version" : - { - "major" : 1, - "minor" : 0 - } - }, - { - "jsonFile" : "toolchains-v1-17c20dde199508adeef8.json", - "kind" : "toolchains", - "version" : - { - "major" : 1, - "minor" : 0 - } - } - ], - "reply" : - { - "cache-v2" : - { - "jsonFile" : "cache-v2-525bf0c12142f71983da.json", - "kind" : "cache", - "version" : - { - "major" : 2, - "minor" : 0 - } - }, - "cmakeFiles-v1" : - { - "jsonFile" : "cmakeFiles-v1-c002049034cb93e91036.json", - "kind" : "cmakeFiles", - "version" : - { - "major" : 1, - "minor" : 0 - } - }, - "codemodel-v2" : - { - "jsonFile" : "codemodel-v2-7763de202a4897ee489a.json", - "kind" : "codemodel", - "version" : - { - "major" : 2, - "minor" : 6 - } - }, - "toolchains-v1" : - { - "jsonFile" : "toolchains-v1-17c20dde199508adeef8.json", - "kind" : "toolchains", - "version" : - { - "major" : 1, - "minor" : 0 - } - } - } -} diff --git a/cmake-build-debug/.cmake/api/v1/reply/target-Compiler-Debug-b504b707cc3938b58fc6.json b/cmake-build-debug/.cmake/api/v1/reply/target-Compiler-Debug-b504b707cc3938b58fc6.json deleted file mode 100644 index b569999..0000000 --- a/cmake-build-debug/.cmake/api/v1/reply/target-Compiler-Debug-b504b707cc3938b58fc6.json +++ /dev/null @@ -1,186 +0,0 @@ -{ - "artifacts" : - [ - { - "path" : "Compiler" - } - ], - "backtrace" : 1, - "backtraceGraph" : - { - "commands" : - [ - "add_executable", - "target_link_directories", - "target_link_libraries", - "add_compile_options", - "add_compile_definitions" - ], - "files" : - [ - "CMakeLists.txt" - ], - "nodes" : - [ - { - "file" : 0 - }, - { - "command" : 0, - "file" : 0, - "line" : 29, - "parent" : 0 - }, - { - "command" : 1, - "file" : 0, - "line" : 39, - "parent" : 0 - }, - { - "command" : 2, - "file" : 0, - "line" : 44, - "parent" : 0 - }, - { - "command" : 3, - "file" : 0, - "line" : 11, - "parent" : 0 - }, - { - "command" : 4, - "file" : 0, - "line" : 36, - "parent" : 0 - } - ] - }, - "compileGroups" : - [ - { - "compileCommandFragments" : - [ - { - "fragment" : " -fPIC -g -std=gnu++20 -fdiagnostics-color=always" - }, - { - "backtrace" : 4, - "fragment" : "-Wno-stringop-overflow" - } - ], - "defines" : - [ - { - "backtrace" : 5, - "define" : "LWE_WS_XCB" - }, - { - "backtrace" : 5, - "define" : "VK_USE_PLATFORM_XCB_KHR" - } - ], - "language" : "CXX", - "languageStandard" : - { - "backtraces" : - [ - 1 - ], - "standard" : "20" - }, - "sourceIndexes" : - [ - 0 - ] - } - ], - "id" : "Compiler::@6890427a1f51a3e7e1df", - "link" : - { - "commandFragments" : - [ - { - "fragment" : "-fPIC -g", - "role" : "flags" - }, - { - "fragment" : "", - "role" : "flags" - }, - { - "backtrace" : 2, - "fragment" : "-L/home/karutoh/.local/lib", - "role" : "libraryPath" - }, - { - "fragment" : "-Wl,-rpath,/home/karutoh/.local/lib", - "role" : "libraries" - }, - { - "backtrace" : 3, - "fragment" : "-lxcb", - "role" : "libraries" - }, - { - "backtrace" : 3, - "fragment" : "-lxcb-cursor", - "role" : "libraries" - }, - { - "backtrace" : 3, - "fragment" : "-lxcb-xfixes", - "role" : "libraries" - }, - { - "backtrace" : 3, - "fragment" : "-lxcb-xinput", - "role" : "libraries" - }, - { - "backtrace" : 3, - "fragment" : "-lz", - "role" : "libraries" - }, - { - "backtrace" : 3, - "fragment" : "-lasound", - "role" : "libraries" - }, - { - "backtrace" : 3, - "fragment" : "-lEHS", - "role" : "libraries" - } - ], - "language" : "CXX" - }, - "name" : "Compiler", - "nameOnDisk" : "Compiler", - "paths" : - { - "build" : ".", - "source" : "." - }, - "sourceGroups" : - [ - { - "name" : "Source Files", - "sourceIndexes" : - [ - 0 - ] - } - ], - "sources" : - [ - { - "backtrace" : 1, - "compileGroupIndex" : 0, - "path" : "main.cpp", - "sourceGroupIndex" : 0 - } - ], - "type" : "EXECUTABLE" -} diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps index 097f843..9a07b69 100644 Binary files a/cmake-build-debug/.ninja_deps and b/cmake-build-debug/.ninja_deps differ diff --git a/cmake-build-debug/.ninja_log b/cmake-build-debug/.ninja_log index fcb7bce..2c39655 100644 --- a/cmake-build-debug/.ninja_log +++ b/cmake-build-debug/.ninja_log @@ -1,7 +1,22 @@ # ninja log v5 -167 224 1707921734641901585 Compiler 415d67d1a4aaa864 -0 57 1707955165530194762 build.ninja 8efbe7f6ed9511af -0 167 1707921734585234642 CMakeFiles/Compiler.dir/main.cpp.o de3174f74a65d11b -0 3 0 clean aa1517684869203c +0 223 1707996608606923476 CMakeFiles/Compiler.dir/src/ELF64.cpp.o 7203a7b7f92d3a +1 162 1707992216831404362 CMakeFiles/Compiler.dir/src/ELF64_Program.cpp.o 6531657353b15e1e +1 166 1707996223750353941 CMakeFiles/Compiler.dir/src/ELF64_Section.cpp.o 886965ebb1584f45 +223 282 1707996608663590955 Compiler 6d7a3ac418bcbfae +0 15 1707998140776325315 build.ninja 8efbe7f6ed9511af 1 166 1707955167336856399 CMakeFiles/Compiler.dir/main.cpp.o 7e18d66cc0ec0bf7 -166 250 1707955167420189502 Compiler 9828d719de1a93e2 +0 184 1707996608566922903 CMakeFiles/Compiler.dir/src/main.cpp.o d6e32444f822aba9 +0 151 1707965004973740575 CMakeFiles/Compiler.dir/src/ELF.cpp.o bd59b885017e2e72 +0 3 0 clean aa1517684869203c +0 153 1708001529297448257 CMakeFiles/Compiler.dir/src/ELF64_Sym.cpp.o 9891c59f9d4d387b +0 170 1708001529314115065 CMakeFiles/Compiler.dir/src/ELF64_Section.cpp.o 886965ebb1584f45 +1 171 1708001529317448427 CMakeFiles/Compiler.dir/src/main.cpp.o d6e32444f822aba9 +0 227 1708001529374115574 CMakeFiles/Compiler.dir/src/ELF64.cpp.o 7203a7b7f92d3a +227 287 1708001529430782721 Compiler b603a0c69744a064 +1 165 1708001692258830975 CMakeFiles/Compiler.dir/src/main.cpp.o d6e32444f822aba9 +165 222 1708001692315498123 Compiler b603a0c69744a064 +1 162 1708001814103198322 CMakeFiles/Compiler.dir/src/main.cpp.o d6e32444f822aba9 +162 220 1708001814159865469 Compiler b603a0c69744a064 +0 151 1708001978274591713 CMakeFiles/Compiler.dir/src/ELF64_Sym.cpp.o 9891c59f9d4d387b +0 173 1708001978297925244 CMakeFiles/Compiler.dir/src/main.cpp.o d6e32444f822aba9 +173 236 1708001978357925753 Compiler b603a0c69744a064 diff --git a/cmake-build-debug/CMakeFiles/Compiler.dir/main.cpp.o b/cmake-build-debug/CMakeFiles/Compiler.dir/main.cpp.o index ac8dd46..eb15a7a 100644 Binary files a/cmake-build-debug/CMakeFiles/Compiler.dir/main.cpp.o and b/cmake-build-debug/CMakeFiles/Compiler.dir/main.cpp.o differ diff --git a/cmake-build-debug/Compiler b/cmake-build-debug/Compiler index 09290f5..8227d43 100755 Binary files a/cmake-build-debug/Compiler and b/cmake-build-debug/Compiler differ diff --git a/cmake-build-debug/Testing/Temporary/LastTest.log b/cmake-build-debug/Testing/Temporary/LastTest.log index f90750c..9535fca 100644 --- a/cmake-build-debug/Testing/Temporary/LastTest.log +++ b/cmake-build-debug/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Feb 14 06:42 PST +Start testing: Feb 15 05:04 PST ---------------------------------------------------------- -End testing: Feb 14 06:42 PST +End testing: Feb 15 05:04 PST diff --git a/cmake-build-debug/build.ninja b/cmake-build-debug/build.ninja index 2395adc..bef0680 100644 --- a/cmake-build-debug/build.ninja +++ b/cmake-build-debug/build.ninja @@ -49,12 +49,45 @@ cmake_ninja_workdir = /home/karutoh/CLionProjects/Compiler/cmake-build-debug/ build cmake_object_order_depends_target_Compiler: phony || CMakeFiles/Compiler.dir -build CMakeFiles/Compiler.dir/main.cpp.o: CXX_COMPILER__Compiler_unscanned_Debug /home/karutoh/CLionProjects/Compiler/main.cpp || cmake_object_order_depends_target_Compiler +build CMakeFiles/Compiler.dir/src/ELF64_Sym.cpp.o: CXX_COMPILER__Compiler_unscanned_Debug /home/karutoh/CLionProjects/Compiler/src/ELF64_Sym.cpp || cmake_object_order_depends_target_Compiler DEFINES = -DLWE_WS_XCB - DEP_FILE = CMakeFiles/Compiler.dir/main.cpp.o.d + DEP_FILE = CMakeFiles/Compiler.dir/src/ELF64_Sym.cpp.o.d FLAGS = -g -std=gnu++20 -fdiagnostics-color=always -Wno-stringop-overflow + INCLUDES = -I/home/karutoh/CLionProjects/Compiler/include OBJECT_DIR = CMakeFiles/Compiler.dir - OBJECT_FILE_DIR = CMakeFiles/Compiler.dir + OBJECT_FILE_DIR = CMakeFiles/Compiler.dir/src + +build CMakeFiles/Compiler.dir/src/ELF64_Section.cpp.o: CXX_COMPILER__Compiler_unscanned_Debug /home/karutoh/CLionProjects/Compiler/src/ELF64_Section.cpp || cmake_object_order_depends_target_Compiler + DEFINES = -DLWE_WS_XCB + DEP_FILE = CMakeFiles/Compiler.dir/src/ELF64_Section.cpp.o.d + FLAGS = -g -std=gnu++20 -fdiagnostics-color=always -Wno-stringop-overflow + INCLUDES = -I/home/karutoh/CLionProjects/Compiler/include + OBJECT_DIR = CMakeFiles/Compiler.dir + OBJECT_FILE_DIR = CMakeFiles/Compiler.dir/src + +build CMakeFiles/Compiler.dir/src/ELF64_Program.cpp.o: CXX_COMPILER__Compiler_unscanned_Debug /home/karutoh/CLionProjects/Compiler/src/ELF64_Program.cpp || cmake_object_order_depends_target_Compiler + DEFINES = -DLWE_WS_XCB + DEP_FILE = CMakeFiles/Compiler.dir/src/ELF64_Program.cpp.o.d + FLAGS = -g -std=gnu++20 -fdiagnostics-color=always -Wno-stringop-overflow + INCLUDES = -I/home/karutoh/CLionProjects/Compiler/include + OBJECT_DIR = CMakeFiles/Compiler.dir + OBJECT_FILE_DIR = CMakeFiles/Compiler.dir/src + +build CMakeFiles/Compiler.dir/src/ELF64.cpp.o: CXX_COMPILER__Compiler_unscanned_Debug /home/karutoh/CLionProjects/Compiler/src/ELF64.cpp || cmake_object_order_depends_target_Compiler + DEFINES = -DLWE_WS_XCB + DEP_FILE = CMakeFiles/Compiler.dir/src/ELF64.cpp.o.d + FLAGS = -g -std=gnu++20 -fdiagnostics-color=always -Wno-stringop-overflow + INCLUDES = -I/home/karutoh/CLionProjects/Compiler/include + OBJECT_DIR = CMakeFiles/Compiler.dir + OBJECT_FILE_DIR = CMakeFiles/Compiler.dir/src + +build CMakeFiles/Compiler.dir/src/main.cpp.o: CXX_COMPILER__Compiler_unscanned_Debug /home/karutoh/CLionProjects/Compiler/src/main.cpp || cmake_object_order_depends_target_Compiler + DEFINES = -DLWE_WS_XCB + DEP_FILE = CMakeFiles/Compiler.dir/src/main.cpp.o.d + FLAGS = -g -std=gnu++20 -fdiagnostics-color=always -Wno-stringop-overflow + INCLUDES = -I/home/karutoh/CLionProjects/Compiler/include + OBJECT_DIR = CMakeFiles/Compiler.dir + OBJECT_FILE_DIR = CMakeFiles/Compiler.dir/src # ============================================================================= @@ -64,7 +97,7 @@ build CMakeFiles/Compiler.dir/main.cpp.o: CXX_COMPILER__Compiler_unscanned_Debug ############################################# # Link the executable Compiler -build Compiler: CXX_EXECUTABLE_LINKER__Compiler_Debug CMakeFiles/Compiler.dir/main.cpp.o +build Compiler: CXX_EXECUTABLE_LINKER__Compiler_Debug CMakeFiles/Compiler.dir/src/ELF64_Sym.cpp.o CMakeFiles/Compiler.dir/src/ELF64_Section.cpp.o CMakeFiles/Compiler.dir/src/ELF64_Program.cpp.o CMakeFiles/Compiler.dir/src/ELF64.cpp.o CMakeFiles/Compiler.dir/src/main.cpp.o FLAGS = -g LINK_LIBRARIES = -Wl,-rpath,/home/karutoh/.local/lib -lxcb -lxcb-cursor -lxcb-xfixes -lxcb-xinput -lz -lasound -lEHS LINK_PATH = -L/home/karutoh/.local/lib diff --git a/include/ELF.h b/include/ELF.h deleted file mode 100644 index 0203f99..0000000 --- a/include/ELF.h +++ /dev/null @@ -1,101 +0,0 @@ -#pragma once - -#include - -/// Architecture's memory bit-depth. -#define ELFH_ARCH_32 0x01 -#define ELFH_ARCH_64 0x02 - -/// Architecture's endianness. -#define ELFH_END_LE 0x01 -#define ELFH_END_BE 0x02 - -/// OS' ABI Types. -#define ELFH_ABI_SYSTEMV 0x00 - -/// ELF Binary Types. -#define ELFH_TYPE_EXEC 0x02 -#define ELFH_TYPE_DYN 0x03 - -/// Machine architectures. -#define ELFH_MARCH_AMD_X86_64 0x003E - -/// Program types. -#define PRGH_TYPE_LOAD 0x00000001 - -/// Program flags. -#define PRGH_FLAG_EXEC 0x00000001 -#define PRGH_FLAG_WRITE 0x00000002 -#define PRGH_FLAG_READ 0x00000004 - -/// Section types. -#define SECH_TYPE_NULL 0x00 -#define SECH_TYPE_PROGBITS 0x01 -#define SECH_TYPE_STRTAB 0x03 - -/// Section flags. -#define SECH_FLAG_ALLOC 0x02 -#define SECH_FLAG_EXEC 0x04 -#define SECH_FLAG_STRINGS 0x20 - -/// True header sizes. -#define ELFH_SIZE 64 -#define PRGH_SIZE 56 -#define SECH_SIZE 64 - -/// ELF Header -struct ElfHeader -{ - ehs::UInt_8 architecture; - ehs::UInt_8 endianness; - ehs::UInt_8 elfV1; - ehs::UInt_8 targetABI; - ehs::UInt_8 abiV; - ehs::UInt_16 elfType; - ehs::UInt_16 machineArch; - ehs::UInt_32 elfV2; - ehs::UInt_64 entryAddress; - ehs::UInt_64 programsOffset; - ehs::UInt_64 sectionsOffset; - ehs::UInt_32 flags; - ehs::UInt_16 headerSize; - ehs::UInt_16 pHeaderSize; - ehs::UInt_16 pEntries; - ehs::UInt_16 sSize; - ehs::UInt_16 sEntries; - ehs::UInt_16 strTabIndex; -}; - -/// ELF Program Header -struct ElfPrgHeader -{ - ehs::UInt_32 type; - ehs::UInt_32 flags; - ehs::UInt_64 offset; - ehs::UInt_64 vAddr; - ehs::UInt_64 pAddr; - ehs::UInt_64 fileSize; - ehs::UInt_64 memSize; - ehs::UInt_64 align; -}; - -/// ELF Section Header -struct ElfSecHeader -{ - ehs::UInt_32 index; - ehs::UInt_32 type; - ehs::UInt_64 flags; - ehs::UInt_64 vAddr; - ehs::UInt_64 offset; - ehs::UInt_64 size; - ehs::UInt_32 associatedIndex; - ehs::UInt_32 info; - ehs::UInt_64 align; - ehs::UInt_64 entrySize; -}; - -void WriteHeader(const ElfHeader& header, ehs::Serializer& data); - -void WriteHeader(const ElfPrgHeader& header, ehs::Serializer& data); - -void WriteHeader(const ElfSecHeader& header, ehs::Serializer& data); \ No newline at end of file diff --git a/include/ELF64.h b/include/ELF64.h new file mode 100644 index 0000000..a6d8b0f --- /dev/null +++ b/include/ELF64.h @@ -0,0 +1,95 @@ +#pragma once + +#include "ELF64_Section.h" +#include "ELF64_Program.h" + +#include + +/// Architecture's memory bit-depth. +#define ELFH_ARCH_32 0x01 +#define ELFH_ARCH_64 0x02 + +/// Architecture's endianness. +#define ELFH_END_LE 0x01 +#define ELFH_END_BE 0x02 + +/// OS' ABI Types. +#define ELFH_ABI_SYSTEMV 0x00 + +/// ELF Binary Types. +#define ELFH_TYPE_EXEC 0x02 +#define ELFH_TYPE_DYN 0x03 + +/// Machine architectures. +#define ELFH_MARCH_AMD_X86_64 0x003E + +/// True header sizes. +#define ELFH_SIZE 64 + +class ELF64 +{ +private: + ehs::UInt_8 endianness; + ehs::UInt_8 abi; + ehs::UInt_16 type; + ehs::UInt_16 arch; + ehs::UInt_16 entryPoint; + ehs::Vector programs; + ehs::Vector sections; + +public: + ELF64(); + + ELF64(ehs::UInt_8 end, ehs::UInt_8 abi, ehs::UInt_16 type, ehs::UInt_16 arch); + + ELF64(ehs::Serializer& data); + + ELF64(ELF64&& header) noexcept; + + ELF64(const ELF64& header); + + ELF64& operator=(ELF64&& header) noexcept; + + ELF64& operator=(const ELF64& header); + + ehs::UInt_8 GetEndianness() const; + + ehs::UInt_8 GetABI() const; + + ehs::UInt_16 GetType() const; + + ehs::UInt_16 GetArchitecture() const; + + void SetEntryPoint(ehs::UInt_16 programIndex); + + ELF64_Program* GetEntryPoint() const; + + void AddProgram(ELF64_Program newProgram); + + bool HasSection(ehs::UInt_64 hashId); + + bool HasSection(ehs::Str_8& id); + + void AddSection(ELF64_Section newSection); + + ELF64_Section* GetSection(ehs::UInt_64 sectionHashId) const; + + ELF64_Section* GetSection(const ehs::Str_8& sectionId) const; + + ehs::UInt_64 GetLastSegmentOffset() const; + + ehs::Vec2_u64 SegmentRange(ehs::UInt_64 sectionHashId); + + ehs::Vec2_u64 SegmentRange(const ehs::Str_8& sectionId); + + ehs::Serializer Serialize() const; + +private: + void InitializeSections(); + + ehs::UInt_64 RetrieveSectionsOffset() const; + + ehs::UInt_16 FindShStrTabIndex() const; + + ELF64_Section* FindShStrTab() const; +}; \ No newline at end of file diff --git a/include/ELF64_Program.h b/include/ELF64_Program.h new file mode 100644 index 0000000..dc8af33 --- /dev/null +++ b/include/ELF64_Program.h @@ -0,0 +1,53 @@ +#pragma once + +#include + +/// Program types. +#define PRGH_TYPE_LOAD 0x00000001 + +/// Program flags. +#define PRGH_FLAG_EXEC 0x00000001 +#define PRGH_FLAG_WRITE 0x00000002 +#define PRGH_FLAG_READ 0x00000004 + +#define PRGH_SIZE 56 + +class ELF64; + +class ELF64_Program +{ +private: + friend class ELF64; + + ehs::UInt_32 type; + ehs::UInt_32 flags; + ehs::UInt_64 offset; + ehs::UInt_64 vAddr; + ehs::UInt_64 pAddr; + ehs::UInt_64 fileSize; + ehs::UInt_64 adjustedMemSize; + ehs::UInt_64 align; + +public: + ELF64_Program(); + + ELF64_Program(ehs::UInt_32 type, ehs::UInt_32 flags, ehs::UInt_64 offset, ehs::UInt_64 address, ehs::UInt_64 size, ehs::UInt_64 adjustedMemSize, ehs::UInt_64 align); + + ELF64_Program(ehs::Serializer& data); + + ELF64_Program(ELF64_Program&& program) noexcept; + + ELF64_Program(const ELF64_Program& program); + + ELF64_Program& operator=(ELF64_Program&& program) noexcept; + + ELF64_Program& operator=(const ELF64_Program& program); + + ehs::UInt_32 GetType() const; + + ehs::UInt_32 GetFlags() const; + + ehs::UInt_64 GetAlign() const; + + void Serialize(ehs::Serializer& inData) const; +}; \ No newline at end of file diff --git a/include/ELF64_Section.h b/include/ELF64_Section.h new file mode 100644 index 0000000..e2af4dd --- /dev/null +++ b/include/ELF64_Section.h @@ -0,0 +1,75 @@ +#pragma once + +#include +#include +#include + +/// Section types. +#define SECH_TYPE_NULL 0x0000 +#define SECH_TYPE_PROGBITS 0x0001 +#define SECH_TYPE_SYMTAB 0x0002 +#define SECH_TYPE_STRTAB 0x0003 + +/// Section flags. +#define SECH_FLAG_ALLOC 0x02 +#define SECH_FLAG_EXEC 0x04 +#define SECH_FLAG_STRINGS 0x20 + +#define SECH_SIZE 64 + +class ELF64_Section +{ +private: + friend class ELF64; + + ehs::UInt_64 hashId; + ehs::Str_8 id; + ehs::UInt_32 nameOffset; + ehs::UInt_64 fileOffset; + ehs::UInt_32 type; + ehs::UInt_64 flags; + ehs::UInt_64 vAddr; + ehs::UInt_64 segmentOffset; + ehs::UInt_32 link; + ehs::UInt_32 info; + ehs::UInt_64 align; + ehs::UInt_64 entries; + ehs::Serializer data; + +public: + ELF64_Section(); + + ELF64_Section(ehs::Str_8 id, ehs::UInt_64 fileOffset, ehs::UInt_32 type, ehs::UInt_64 flags, ehs::UInt_64 vAddr, ehs::UInt_64 align); + + ELF64_Section(ELF64_Section&& sect) noexcept; + + ELF64_Section(const ELF64_Section& sect); + + ELF64_Section& operator=(ELF64_Section&& sect) noexcept; + + ELF64_Section& operator=(const ELF64_Section& sect); + + ehs::UInt_64 GetHashId() const; + + ehs::Str_8 GetId() const; + + ehs::UInt_32 GetType() const; + + ehs::UInt_64 GetFlags() const; + + ehs::UInt_64 GetVirtualAddress() const; + + void SetLink(ehs::UInt_32 newLink); + + void SetInfo(ehs::UInt_32 newInfo); + + ehs::UInt_64 GetAlignment() const; + + void SetEntries(ehs::UInt_64 newEntries); + + void SetData(ehs::Serializer newData); + + ehs::Serializer& GetData(); + + void Serialize(ehs::Serializer& inData) const; +}; \ No newline at end of file diff --git a/include/ELF64_Sym.h b/include/ELF64_Sym.h new file mode 100644 index 0000000..2da1a10 --- /dev/null +++ b/include/ELF64_Sym.h @@ -0,0 +1,93 @@ +#pragma once + +#include + +/// The symbol's type is not specified. +#define SYM_TYPE_NOTYPE 0x00 + +/// The symbol is associated with a data object, such as a variable, an array, and so on. +#define SYM_TYPE_OBJECT 0x01 + +/// The symbol is associated with a function or other executable code. +#define SYM_TYPE_FUNC 0x02 + +/// The symbol is associated with a section. Symbol table entries of this type exist primarily for relocation and normally have STB_LOCAL binding. +#define SYM_TYPE_SECTION 0x03 + +/// Conventionally, the symbol's name gives the name of the source file associated with the object file. A file symbol has STB_LOCAL binding, its section index is SHN_ABS, and it precedes the other STB_LOCAL symbols for the file, if it is present. +#define SYM_TYPE_FILE 0x04 + +/// The symbol labels an uninitialized common block. +#define SYM_TYPE_COMMON 0x05 + +/// The symbol specifies a Thread-Local Storage entity. When defined, it gives the assigned offset for the symbol, not the actual address. Symbols of type STT_TLS can be referenced by only special thread-local storage relocations and thread-local storage relocations can only reference symbols with type STT_TLS. Implementation need not support thread-local storage. +#define SYM_TYPE_TLS 0x06 + +/// Values in this inclusive range are reserved for operating system-specific semantics. +#define SYM_TYPE_LOOS 0x0A + +/// Values in this inclusive range are reserved for operating system-specific semantics. +#define SYM_TYPE_HIOS 0x0C + +/// Values in this inclusive range are reserved for processor-specific semantics. If meanings are specified, the processor supplement explains them. +#define SYM_TYPE_LOPROC 0x0D + +/// Values in this inclusive range are reserved for processor-specific semantics. If meanings are specified, the processor supplement explains them. +#define SYM_TYPE_HIPROC 0x0F + +/// Local symbols are not visible outside the object file containing their definition. Local symbols of the same name may exist in multiple files without interfering with each other. +#define SYM_BIND_LOCAL 0x00 + +/// Global symbols are visible to all object files being combined. One file's definition of a global symbol will satisfy another file's undefined reference to the same global symbol. +#define SYM_BIND_GLOBAL 0x01 + +/// Weak symbols resemble global symbols, but their definitions have lower precedence. +#define SYM_BIND_WEAK 0x02 + +/// Values in this inclusive range are reserved for operating system-specific semantics. +#define SYM_BIND_LOOS 0x0A + +/// Values in this inclusive range are reserved for operating system-specific semantics. +#define SYM_BIND_HIOS 0x0C + +/// Values in this inclusive range are reserved for processor-specific semantics. If meanings are specified, the processor supplement explains them. +#define SYM_BIND_LOPROC 0x0D + +/// Values in this inclusive range are reserved for processor-specific semantics. If meanings are specified, the processor supplement explains them. +#define SYM_BIND_HIPROC 0x0F + +#define SYM_BIND(i) ((i)>>4) +#define SYM_TYPE(i) ((i)&0xf) +#define SYM_INFO(b,t) (((b)<<4)+((t)&0xf)) + +/// The visibility of symbols with the STV_DEFAULT attribute is as specified by the symbol's binding type. That is, global and weak symbols are visible outside of their defining component (executable file or shared object). Local symbols are hidden, as described below. Global and weak symbols are also preemptable, that is, they may by preempted by definitions of the same name in another component. +#define SYM_VIS_DEFAULT 0x00 + +/// The meaning of this visibility attribute may be defined by processor supplements to further constrain hidden symbols. A processor supplement's definition should be such that generic tools can safely treat internal symbols as hidden. +#define SYM_VIS_INTERNAL 0x01 + +/// A symbol defined in the current component is hidden if its name is not visible to other components. Such a symbol is necessarily protected. This attribute may be used to control the external interface of a component. Note that an object named by such a symbol may still be referenced from another component if its address is passed outside. +#define SYM_VIS_HIDDEN 0x02 + +/// A symbol defined in the current component is protected if it is visible in other components but not preemptable, meaning that any reference to such a symbol from within the defining component must be resolved to the definition in that component, even if there is a definition in another component that would preempt by the default rules. A symbol with STB_LOCAL binding may not have STV_PROTECTED visibility. If a symbol definition with STV_PROTECTED visibility from a shared object is taken as resolving a reference from an executable or another shared object, the SHN_UNDEF symbol table entry created has STV_DEFAULT visibility. +#define SYM_VIS_PROTECTED 0x03 + +#define SYMH_SIZE 24 + +class ELF64_Sym +{ +private: + ehs::UInt_32 nameOffset; + ehs::UInt_8 type; + ehs::UInt_8 visibility; + ehs::UInt_16 section; + ehs::UInt_64 value; + ehs::UInt_64 size; + +public: + ELF64_Sym(); + + ELF64_Sym(ehs::UInt_32 nameOffset, ehs::UInt_8 type, ehs::UInt_8 visibility, ehs::UInt_16 section, ehs::UInt_64 value, ehs::UInt_64 size); + + void Serialize(ehs::Serializer& inData) const; +}; \ No newline at end of file diff --git a/src/ELF.cpp b/src/ELF.cpp deleted file mode 100644 index 553a2df..0000000 --- a/src/ELF.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "ELF.h" - -constexpr ehs::Char_8 Elf_Magic[] = "\x7F" "ELF"; -constexpr ehs::UInt_8 Elf_Magic_Size = sizeof(Elf_Magic) - 1; - -void WriteHeader(const ElfHeader& header, ehs::Serializer& data) -{ - data.Resize(Elf_Magic_Size); - ehs::Util::Copy(&data[0], Elf_Magic, Elf_Magic_Size); - data.SetOffset(Elf_Magic_Size); - - data.Write(header.architecture); - data.Write(header.endianness); - data.Write(header.elfV1); - data.Write(header.targetABI); - data.Write(header.abiV); - - data.Resize(data.Size() + 7); - ehs::Util::Zero(&data[data.GetOffset()], 7); - data.SetOffset(data.GetOffset() + 7); - - data.Write(header.elfType); - data.Write(header.machineArch); - data.Write(header.elfV2); - data.Write(header.entryAddress); - data.Write(header.programsOffset); - data.Write(header.sectionsOffset); - data.Write(header.flags); - data.Write(header.headerSize); - data.Write(header.pHeaderSize); - data.Write(header.pEntries); - data.Write(header.sSize); - data.Write(header.sEntries); - data.Write(header.strTabIndex); -} - -void WriteHeader(const ElfPrgHeader& header, ehs::Serializer& data) -{ - data.Write(header.type); - data.Write(header.flags); - data.Write(header.offset); - data.Write(header.vAddr); - data.Write(header.pAddr); - data.Write(header.fileSize); - data.Write(header.memSize); - data.Write(header.align); -} - -void WriteHeader(const ElfSecHeader& header, ehs::Serializer& data) -{ - data.Write(header.index); - data.Write(header.type); - data.Write(header.flags); - data.Write(header.vAddr); - data.Write(header.offset); - data.Write(header.size); - data.Write(header.associatedIndex); - data.Write(header.info); - data.Write(header.align); - data.Write(header.entrySize); -} \ No newline at end of file diff --git a/src/ELF64.cpp b/src/ELF64.cpp new file mode 100644 index 0000000..b3872a0 --- /dev/null +++ b/src/ELF64.cpp @@ -0,0 +1,358 @@ +#include "ELF64.h" + +constexpr ehs::Char_8 Elf_Magic[] = "\x7F" "ELF"; +constexpr ehs::UInt_8 Elf_Magic_Size = sizeof(Elf_Magic) - 1; + +ELF64::ELF64() + : endianness(ELFH_END_LE), abi(ELFH_ABI_SYSTEMV), type(ELFH_TYPE_EXEC), + arch(ELFH_MARCH_AMD_X86_64), entryPoint(0) +{ + InitializeSections(); +} + +ELF64::ELF64(ehs::UInt_8 end, ehs::UInt_8 abi, ehs::UInt_16 type, ehs::UInt_16 arch) + : endianness(end), abi(abi), type(type), arch(arch), entryPoint(0) +{ + InitializeSections(); +} + +ELF64::ELF64(ehs::Serializer& data) +{ +} + +ELF64::ELF64(ELF64&& header) noexcept + : endianness(header.endianness), abi(header.abi), type(header.type), arch(header.arch), + entryPoint(header.entryPoint), programs((ehs::Vector&&)header.programs), + sections((ehs::Vector&&)header.sections) +{ + header.endianness = ELFH_END_LE; + header.abi = ELFH_ABI_SYSTEMV; + header.type = ELFH_TYPE_EXEC; + header.arch = ELFH_MARCH_AMD_X86_64; + header.entryPoint = 0; + + header.InitializeSections(); +} + +ELF64::ELF64(const ELF64& header) + : endianness(header.endianness), abi(header.abi), type(header.type), arch(header.arch), + entryPoint(header.entryPoint), programs(header.programs), sections(header.sections) +{ +} + +ELF64& ELF64::operator=(ELF64&& header) noexcept +{ + if (this == &header) + return *this; + + endianness = header.endianness; + abi = header.abi; + type = header.type; + arch = header.arch; + entryPoint = header.entryPoint; + programs = (ehs::Vector&&)header.programs; + sections = (ehs::Vector&&)header.sections; + + header.endianness = ELFH_END_LE; + header.abi = ELFH_ABI_SYSTEMV; + header.type = ELFH_TYPE_EXEC; + header.arch = ELFH_MARCH_AMD_X86_64; + header.entryPoint = 0; + + header.InitializeSections(); + + return *this; +} + +ELF64& ELF64::operator=(const ELF64& header) +{ + if (this == &header) + return *this; + + endianness = header.endianness; + abi = header.abi; + type = header.type; + arch = header.arch; + entryPoint = header.entryPoint; + programs = header.programs; + sections = header.sections; + + return *this; +} + +ehs::UInt_8 ELF64::GetEndianness() const +{ + return endianness; +} + +ehs::UInt_8 ELF64::GetABI() const +{ + return abi; +} + +ehs::UInt_16 ELF64::GetType() const +{ + return type; +} + +ehs::UInt_16 ELF64::GetArchitecture() const +{ + return arch; +} + +void ELF64::SetEntryPoint(ehs::UInt_16 programIndex) +{ + if (programIndex > programs.End()) + { + EHS_LOG("Warning", 0, "The given index, \"" + ehs::Str_8::FromNum(programIndex) + + "\", is outside the bounds of, \"" + ehs::Str_8::FromNum(programs.End()) + "\"."); + + return; + } + + entryPoint = programIndex; +} + +ELF64_Program* ELF64::GetEntryPoint() const +{ + return &programs[entryPoint]; +} + +void ELF64::AddProgram(ELF64_Program newProgram) +{ + /* + for (ehs::UInt_16 i = 0; i < programs.Size(); i++) + { + if (programs[i].fileSize < newProgram.fileSize) + continue; + + EHS_LOG("Warning", 0, "The new program file size, \"" + ehs::Str_8::FromNum(newProgram.fileSize) + + "\" must be greater than, \"" + ehs::Str_8::FromNum(programs[i].fileSize) + "\"."); + + return; + } + */ + + programs.Push((ELF64_Program&&)newProgram); +} + +bool ELF64::HasSection(ehs::UInt_64 hashId) +{ + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + if (sections[i].GetHashId() == hashId) + return true; + + return false; +} + +bool ELF64::HasSection(ehs::Str_8& id) +{ + return HasSection(id.Hash_64()); +} + +void ELF64::AddSection(ELF64_Section newSection) +{ + if (HasSection(newSection.GetHashId())) + { + EHS_LOG("Warning", 0, "The section, \"" + newSection.GetId() + "\", already exists."); + return; + } + + ehs::Str_8 newId = newSection.GetId(); + + ELF64_Section* shStrTab = FindShStrTab(); + ehs::Serializer& data = shStrTab->GetData(); + + newSection.nameOffset = data.GetOffset(); + + data.Resize(data.Size() + newId.Size() + 1); + ehs::Util::Copy(&data[data.GetOffset()], &newId[0], newId.Size()); + data.SetOffset(data.GetOffset() + newId.Size()); + data.Write('\0'); + + sections.Push((ELF64_Section&&)newSection); +} + +ELF64_Section* ELF64::GetSection(ehs::UInt_64 sectionHashId) const +{ + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + if (sections[i].GetHashId() == sectionHashId) + return §ions[i]; + + return nullptr; +} + +ELF64_Section* ELF64::GetSection(const ehs::Str_8& sectionId) const +{ + return GetSection(sectionId.Hash_64()); +} + +ehs::UInt_64 ELF64::GetLastSegmentOffset() const +{ + ehs::UInt_64 offset = ELFH_SIZE + PRGH_SIZE * programs.Size(); + + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + { + if (sections[i].fileOffset) + offset = sections[i].fileOffset; + + offset += sections[i].GetData().Size(); + } + + return offset; +} + +ehs::Vec2_u64 ELF64::SegmentRange(ehs::UInt_64 sectionHashId) +{ + ehs::UInt_64 offset = ELFH_SIZE + PRGH_SIZE * programs.Size(); + if (sectionHashId) + { + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + { + if (sections[i].GetHashId() == sectionHashId) + return {offset, sections[i].GetData().Size()}; + else + { + if (sections[i].fileOffset) + offset = sections[i].fileOffset; + + offset += sections[i].GetData().Size(); + } + } + } + else + { + ehs::UInt_64 scale = 0; + + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + scale += sections[i].GetData().Size(); + + return {offset, scale}; + } + + return {}; +} + +ehs::Vec2_u64 ELF64::SegmentRange(const ehs::Str_8& sectionId) +{ + return SegmentRange(sectionId.Hash_64()); +} + +ehs::Serializer ELF64::Serialize() const +{ + ehs::Serializer result(ehs::Endianness::LE); + result.Resize(Elf_Magic_Size); + ehs::Util::Copy(&result[0], Elf_Magic, Elf_Magic_Size); + result.SetOffset(Elf_Magic_Size); + + result.Write(ELFH_ARCH_64); + result.Write(endianness); + result.Write(1); + result.Write(abi); + result.Write(0); + + result.Resize(result.Size() + 7); + ehs::Util::Zero(&result[result.GetOffset()], 7); + result.SetOffset(result.GetOffset() + 7); + + result.Write(type); + result.Write(arch); + result.Write(1); + result.Write(programs[entryPoint].pAddr); + result.Write(ELFH_SIZE); + result.Write(RetrieveSectionsOffset()); + result.Write(0); + result.Write(ELFH_SIZE); + result.Write(PRGH_SIZE); + result.Write(programs.Size()); + result.Write(SECH_SIZE); + result.Write(sections.Size()); + result.Write(FindShStrTabIndex()); + + for (ehs::UInt_16 i = 0; i < programs.Size(); i++) + programs[i].Serialize(result); + + for (ehs::UInt_16 i = 1; i < sections.Size(); i++) + { + if (sections[i].fileOffset) + { + ehs::UInt_64 padding = sections[i].fileOffset - result.Size(); + result.Resize(result.Size() + padding); + ehs::Util::Zero(&result[result.GetOffset()], padding); + result.SetOffset(result.GetOffset() + padding); + } + + sections[i].segmentOffset = result.GetOffset(); + result.WriteSer(sections[i].GetData()); + } + + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + sections[i].Serialize(result); + + return result; +} + +void ELF64::InitializeSections() +{ + ehs::Str_8 nullName = ".NULL"; + + ELF64_Section null(nullName, 0, SECH_TYPE_NULL, 0, 0, 0); + + ehs::Str_8 shStrTabName = ".shstrtab"; + + ELF64_Section shStrTab(shStrTabName, 0, SECH_TYPE_STRTAB, SECH_FLAG_STRINGS, 0, 1); + + ehs::Serializer& data = shStrTab.GetData(); + + data.Resize(nullName.Size() + shStrTabName.Size() + 2); + + null.nameOffset = data.GetOffset(); + ehs::Util::Copy(&data[data.GetOffset()], &nullName[0], nullName.Size()); + data.SetOffset(data.GetOffset() + nullName.Size()); + data.Write('\0'); + + shStrTab.nameOffset = data.GetOffset(); + ehs::Util::Copy(&data[data.GetOffset()], &shStrTabName[0], shStrTabName.Size()); + data.SetOffset(data.GetOffset() + shStrTabName.Size()); + data.Write('\0'); + + sections.Push((ELF64_Section&&)null); + sections.Push((ELF64_Section&&)shStrTab); +} + +ehs::UInt_64 ELF64::RetrieveSectionsOffset() const +{ + ehs::UInt_64 result = ELFH_SIZE + PRGH_SIZE * programs.Size(); + + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + { + if (sections[i].fileOffset) + result = sections[i].fileOffset; + + result += sections[i].GetData().Size(); + } + + return result; +} + +ehs::UInt_16 ELF64::FindShStrTabIndex() const +{ + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + if (sections[i].GetHashId() == 7974729948679187860ull) + return i; + + EHS_LOG("Error", 0, "Could not find the \".shstrtab\" index."); + + return 0; +} + +ELF64_Section* ELF64::FindShStrTab() const +{ + for (ehs::UInt_16 i = 0; i < sections.Size(); i++) + if (sections[i].GetHashId() == 7974729948679187860ull) + return §ions[i]; + + EHS_LOG("Error", 0, "Could not find the \".shstrtab\"."); + + return nullptr; +} diff --git a/src/ELF64_Program.cpp b/src/ELF64_Program.cpp new file mode 100644 index 0000000..5dd844d --- /dev/null +++ b/src/ELF64_Program.cpp @@ -0,0 +1,109 @@ +#include "ELF64_Program.h" + +ELF64_Program::ELF64_Program() + : type(PRGH_TYPE_LOAD), flags(PRGH_FLAG_EXEC | PRGH_FLAG_READ), offset(0), vAddr(0), pAddr(0), fileSize(0), + adjustedMemSize(0), align(0x200000) +{ +} + +ELF64_Program::ELF64_Program(ehs::UInt_32 type, ehs::UInt_32 flags, ehs::UInt_64 offset, ehs::UInt_64 address, ehs::UInt_64 size, ehs::UInt_64 adjustedMemSize, ehs::UInt_64 align) + : type(type), flags(flags), offset(offset), vAddr(address), pAddr(address), fileSize(size), + adjustedMemSize(adjustedMemSize), align(align) +{ +} + +ELF64_Program::ELF64_Program(ehs::Serializer& data) + : type(PRGH_TYPE_LOAD), flags(PRGH_FLAG_EXEC | PRGH_FLAG_READ), offset(0), vAddr(0), pAddr(0), fileSize(0), + adjustedMemSize(0), align(0x200000) +{ +} + +ELF64_Program::ELF64_Program(ELF64_Program&& program) noexcept + : type(program.type), flags(program.flags), offset(program.offset), vAddr(program.vAddr), pAddr(program.pAddr), + fileSize(program.fileSize), adjustedMemSize(program.adjustedMemSize), align(program.align) +{ + program.type = PRGH_TYPE_LOAD; + program.flags = PRGH_FLAG_EXEC | PRGH_FLAG_READ; + program.offset = 0; + program.vAddr = 0; + program.pAddr = 0; + program.fileSize = 0; + program.adjustedMemSize = 0; + program.align = 0x200000; +} + +ELF64_Program::ELF64_Program(const ELF64_Program& program) + : type(program.type), flags(program.flags), offset(program.offset), vAddr(program.vAddr), pAddr(program.pAddr), + fileSize(program.fileSize), adjustedMemSize(program.adjustedMemSize), align(program.align) +{ +} + +ELF64_Program& ELF64_Program::operator=(ELF64_Program&& program) noexcept +{ + if (this == &program) + return *this; + + type = program.type; + flags = program.flags; + offset = program.offset; + vAddr = program.vAddr; + pAddr = program.pAddr; + fileSize = program.fileSize; + adjustedMemSize = program.adjustedMemSize; + align = program.align; + + program.type = PRGH_TYPE_LOAD; + program.flags = PRGH_FLAG_EXEC | PRGH_FLAG_READ; + program.offset = 0; + program.vAddr = 0; + program.pAddr = 0; + program.fileSize = 0; + program.adjustedMemSize = 0; + program.align = 0x200000; + + return *this; +} + +ELF64_Program& ELF64_Program::operator=(const ELF64_Program& program) +{ + if (this == &program) + return *this; + + type = program.type; + flags = program.flags; + offset = program.offset; + vAddr = program.vAddr; + pAddr = program.pAddr; + fileSize = program.fileSize; + adjustedMemSize = program.adjustedMemSize; + align = program.align; + + return *this; +} + +ehs::UInt_32 ELF64_Program::GetType() const +{ + return type; +} + +ehs::UInt_32 ELF64_Program::GetFlags() const +{ + return flags; +} + +ehs::UInt_64 ELF64_Program::GetAlign() const +{ + return align; +} + +void ELF64_Program::Serialize(ehs::Serializer& inData) const +{ + inData.Write(type); + inData.Write(flags); + inData.Write(offset); + inData.Write(vAddr); + inData.Write(pAddr); + inData.Write(fileSize); + inData.Write(fileSize + adjustedMemSize); + inData.Write(align); +} diff --git a/src/ELF64_Section.cpp b/src/ELF64_Section.cpp new file mode 100644 index 0000000..6eaea14 --- /dev/null +++ b/src/ELF64_Section.cpp @@ -0,0 +1,166 @@ +#include "ELF64_Section.h" + +ELF64_Section::ELF64_Section() + : hashId(0), nameOffset(0), fileOffset(0), type(0), flags(0), vAddr(0), segmentOffset(0), link(0), + info(0), align(0), entries(0), data(ehs::Endianness::LE) +{ +} + +ELF64_Section::ELF64_Section(ehs::Str_8 id, ehs::UInt_64 fileOffset, ehs::UInt_32 type, ehs::UInt_64 flags, ehs::UInt_64 vAddr, ehs::UInt_64 align) + : hashId(id.Hash_64()), id((ehs::Str_8&&)id), nameOffset(0), fileOffset(fileOffset), type(type), flags(flags), vAddr(vAddr), + segmentOffset(0), link(0), info(0), align(align), entries(0), data(ehs::Endianness::LE) +{ + +} + +ELF64_Section::ELF64_Section(ELF64_Section&& sect) noexcept + : hashId(sect.hashId), id((ehs::Str_8&&)sect.id), nameOffset(sect.nameOffset), fileOffset(sect.fileOffset), + type(sect.type), flags(sect.flags), vAddr(sect.vAddr), segmentOffset(sect.segmentOffset), + link(sect.link), info(sect.info), align(sect.align), entries(sect.entries), + data((ehs::Serializer&&)sect.data) +{ + sect.hashId = 0; + sect.nameOffset = 0; + sect.fileOffset = 0; + sect.type = 0; + sect.flags = 0; + sect.vAddr = 0; + sect.segmentOffset = 0; + sect.link = 0; + sect.info = 0; + sect.align = 0; + sect.entries = 0; +} + +ELF64_Section::ELF64_Section(const ELF64_Section& sect) + : hashId(sect.hashId), id(sect.id), nameOffset(0), fileOffset(sect.fileOffset), type(sect.type), + flags(sect.flags), vAddr(sect.vAddr), segmentOffset(sect.segmentOffset), link(sect.link), + info(sect.info), align(sect.align), entries(sect.entries), data(sect.data) +{ +} + +ELF64_Section& ELF64_Section::operator=(ELF64_Section&& sect) noexcept +{ + if (this == §) + return *this; + + hashId = sect.hashId; + id = (ehs::Str_8&&)sect.id; + nameOffset = sect.nameOffset; + fileOffset = sect.fileOffset; + type = sect.type; + flags = sect.flags; + vAddr = sect.vAddr; + segmentOffset = sect.segmentOffset; + link = sect.link; + info = sect.info; + align = sect.align; + entries = sect.entries; + data = (ehs::Serializer&&)sect.data; + + sect.hashId = 0; + sect.nameOffset = 0; + sect.fileOffset = 0; + sect.type = 0; + sect.flags = 0; + sect.vAddr = 0; + sect.segmentOffset = 0; + sect.link = 0; + sect.info = 0; + sect.align = 0; + sect.entries = 0; + sect.data = {ehs::Endianness::LE}; + + return *this; +} + +ELF64_Section& ELF64_Section::operator=(const ELF64_Section& sect) +{ + if (this == §) + return *this; + + hashId = sect.hashId; + id = sect.id; + nameOffset = 0; + fileOffset = sect.fileOffset; + type = sect.type; + flags = sect.flags; + vAddr = sect.vAddr; + segmentOffset = sect.segmentOffset; + link = sect.link; + info = sect.info; + align = sect.align; + entries = sect.entries; + data = sect.data; + + return *this; +} + +ehs::UInt_64 ELF64_Section::GetHashId() const +{ + return hashId; +} + +ehs::Str_8 ELF64_Section::GetId() const +{ + return id; +} + +ehs::UInt_32 ELF64_Section::GetType() const +{ + return type; +} + +ehs::UInt_64 ELF64_Section::GetFlags() const +{ + return flags; +} + +ehs::UInt_64 ELF64_Section::GetVirtualAddress() const +{ + return vAddr; +} + +void ELF64_Section::SetLink(ehs::UInt_32 newLink) +{ + link = newLink; +} + +void ELF64_Section::SetInfo(ehs::UInt_32 newInfo) +{ + info = newInfo; +} + +ehs::UInt_64 ELF64_Section::GetAlignment() const +{ + return align; +} + +void ELF64_Section::SetEntries(ehs::UInt_64 newEntries) +{ + entries = newEntries; +} + +void ELF64_Section::SetData(ehs::Serializer newData) +{ + data = (ehs::Serializer&&)newData; +} + +ehs::Serializer& ELF64_Section::GetData() +{ + return data; +} + +void ELF64_Section::Serialize(ehs::Serializer& inData) const +{ + inData.Write(nameOffset); + inData.Write(type); + inData.Write(flags); + inData.Write(vAddr); + inData.Write(segmentOffset); + inData.Write(data.Size()); + inData.Write(link); + inData.Write(info); + inData.Write(align); + inData.Write(entries); +} diff --git a/src/ELF64_Sym.cpp b/src/ELF64_Sym.cpp new file mode 100644 index 0000000..9120d5b --- /dev/null +++ b/src/ELF64_Sym.cpp @@ -0,0 +1,23 @@ +#include "ELF64_Sym.h" + +ELF64_Sym::ELF64_Sym() + : nameOffset(0), type(0), visibility(0), section(0), value(0), size(0) +{ + +} + +ELF64_Sym::ELF64_Sym(ehs::UInt_32 nameOffset, ehs::UInt_8 type, ehs::UInt_8 visibility, ehs::UInt_16 section, ehs::UInt_64 value, ehs::UInt_64 size) + : nameOffset(nameOffset), type(type), visibility(visibility), section(section), value(value), size(size) +{ +} + +void ELF64_Sym::Serialize(ehs::Serializer& inData) const +{ + inData.Write(nameOffset); + inData.Write(type); + inData.Write(visibility); + inData.Write(section); + inData.Write(value); + inData.Write(size); +} + diff --git a/src/main.cpp b/src/main.cpp index 8e95e6d..94cc78b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ -#include "ELF.h" +#include "ELF64.h" +#include "ELF64_Sym.h" #include #include @@ -23,92 +24,45 @@ ehs::SInt_32 Main(ehs::Str_8* appName, ehs::Str_8* appVerId, ehs::Version* appVe ehs::Serializer code = codeFile.ReadSerializer_64(ehs::Endianness::LE, codeFile.Size()); codeFile.Release(); - char sections[] = "\0.text\0.shstrtab\0"; + ELF64 executable(ELFH_END_LE, ELFH_ABI_SYSTEMV, ELFH_TYPE_EXEC, ELFH_MARCH_AMD_X86_64); - ElfHeader elfHeader = {}; - elfHeader.architecture = ELFH_ARCH_64; - elfHeader.endianness = ELFH_END_LE; - elfHeader.elfV1 = 1; - elfHeader.targetABI = ELFH_ABI_SYSTEMV; - elfHeader.abiV = 0; - elfHeader.elfType = ELFH_TYPE_EXEC; - elfHeader.machineArch = ELFH_MARCH_AMD_X86_64; - elfHeader.elfV2 = 1; - elfHeader.entryAddress = 0x400000 + ELFH_SIZE + PRGH_SIZE; - elfHeader.programsOffset = ELFH_SIZE; - elfHeader.sectionsOffset = ELFH_SIZE + PRGH_SIZE + code.Size() + sizeof(sections); - elfHeader.flags = 0; - elfHeader.headerSize = ELFH_SIZE; - elfHeader.pHeaderSize = PRGH_SIZE; - elfHeader.pEntries = 1; - elfHeader.sSize = SECH_SIZE; - elfHeader.sEntries = 3; - elfHeader.strTabIndex = 2; + ehs::UInt_64 lSgmtOffset = executable.GetLastSegmentOffset(); - ElfPrgHeader programHeader = {}; - programHeader.type = PRGH_TYPE_LOAD; - programHeader.flags = PRGH_FLAG_EXEC | PRGH_FLAG_READ; - programHeader.offset = 0; - programHeader.vAddr = 0x400000; - programHeader.pAddr = 0x400000; - programHeader.fileSize = ELFH_SIZE + PRGH_SIZE + code.Size(); - programHeader.memSize = ELFH_SIZE + PRGH_SIZE + code.Size(); - programHeader.align = 0x200000; + ELF64_Section text(".text", 0x1000, SECH_TYPE_PROGBITS, SECH_FLAG_ALLOC | SECH_FLAG_EXEC, 0x401000, 16); + text.SetData(code); + executable.AddSection((ELF64_Section&&)text); - ElfSecHeader null = {}; - null.index = 0; - null.type = SECH_TYPE_NULL; - null.flags = 0; - null.vAddr = 0; - null.offset = 0; - null.size = 0; - null.associatedIndex = 0; - null.info = 0; - null.align = 0; - null.entrySize = 0; + ELF64_Section strTab(".strtab", 0, SECH_TYPE_STRTAB, 0, 0, 1); + ehs::Serializer& strTabData = strTab.GetData(); - ElfSecHeader text = {}; - text.index = 1; - text.type = SECH_TYPE_PROGBITS; - text.flags = SECH_FLAG_ALLOC | SECH_FLAG_EXEC; - text.vAddr = 0x400000 + ELFH_SIZE + PRGH_SIZE; - text.offset = ELFH_SIZE + PRGH_SIZE; - text.size = sizeof(code.Size()); - text.associatedIndex = 0; - text.info = 0; - text.align = 16; - text.entrySize = 0; + constexpr ehs::Char_8 symName[] = "\0_start\0"; + strTabData.Resize(sizeof(symName)); + ehs::Util::Copy(&strTabData[0], symName, sizeof(symName)); + strTabData.SetOffset(sizeof(symName)); - ElfSecHeader strTbl = {}; - strTbl.index = 7; - strTbl.type = SECH_TYPE_STRTAB; - strTbl.flags = SECH_FLAG_STRINGS; - strTbl.vAddr = 0; - strTbl.offset = ELFH_SIZE + PRGH_SIZE + code.Size(); - strTbl.size = sizeof(sections); - strTbl.associatedIndex = 0; - strTbl.info = 0; - strTbl.align = 0; - strTbl.entrySize = 0; + executable.AddSection((ELF64_Section&&)strTab); - ehs::Serializer data(ehs::Endianness::LE); - WriteHeader(elfHeader, data); - WriteHeader(programHeader, data); + ELF64_Section symTab(".symtab", 0, SECH_TYPE_SYMTAB, 0, 0, 8); + symTab.SetLink(3); + symTab.SetInfo(2); + symTab.SetEntries(SYMH_SIZE); + ehs::Serializer& symTabData = symTab.GetData(); - data.Resize(data.Size() + code.Size()); - ehs::Util::Copy(&data[data.GetOffset()], code, code.Size()); - data.SetOffset(data.GetOffset() + code.Size()); + ELF64_Sym nullSym; + nullSym.Serialize(symTabData); - data.Resize(data.Size() + sizeof(sections)); - ehs::Util::Copy(&data[data.GetOffset()], sections, sizeof(sections)); - data.SetOffset(data.GetOffset() + sizeof(sections)); + ELF64_Sym _start(1, SYM_INFO(SYM_BIND_GLOBAL, SYM_TYPE_NOTYPE), SYM_VIS_DEFAULT, 2, 0x401000, 0); + _start.Serialize(symTabData); - WriteHeader(null, data); - WriteHeader(text, data); - WriteHeader(strTbl, data); + executable.AddSection((ELF64_Section&&)symTab); + + executable.AddProgram({PRGH_TYPE_LOAD, PRGH_FLAG_READ, 0, 0x400000, lSgmtOffset, 0, 0x1000}); + executable.AddProgram({PRGH_TYPE_LOAD, PRGH_FLAG_EXEC | PRGH_FLAG_READ, 0x1000, 0x401000, code.Size(), 0, 0x1000}); + + executable.SetEntryPoint(1); ehs::File file(args[2].RemoveAll("\""), ehs::Mode::WRITE, ehs::Disposition::CREATE_PERSISTENT); - file.WriteSerializer_64(data); + file.WriteSerializer_64(executable.Serialize()); return 0; }