aboutsummaryrefslogtreecommitdiff
path: root/libs/elf
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2025-12-15 17:13:12 +0100
committerFelix Morgner <felix.morgner@ost.ch>2025-12-15 17:13:12 +0100
commit7b9482ae637126ac9337876e60f519b493437711 (patch)
tree6fc71a253c8b0325d303bd34c95b564ba536ed14 /libs/elf
parent116f9332a206767c45095950f09f7c7447b561cf (diff)
parenta9eeec745e29d89afd48ee43d09432eb6fc35be7 (diff)
downloadteachos-7b9482ae637126ac9337876e60f519b493437711.tar.xz
teachos-7b9482ae637126ac9337876e60f519b493437711.zip
os: rework kernel architecture
Rework the code structure and architecture of the kernel by separating platform-dependent and platform-independent code more cleanly. As of this patchset, full feature parity has not been achieved. Nonetheless, a sufficient subset of functionality has been ported to the new architecture to demonstrate the feasibility of the new structure.
Diffstat (limited to 'libs/elf')
-rw-r--r--libs/elf/CMakeLists.txt19
-rw-r--r--libs/elf/include/elf/format.hpp15
-rw-r--r--libs/elf/include/elf/section_header.hpp99
3 files changed, 133 insertions, 0 deletions
diff --git a/libs/elf/CMakeLists.txt b/libs/elf/CMakeLists.txt
new file mode 100644
index 0000000..66e59ee
--- /dev/null
+++ b/libs/elf/CMakeLists.txt
@@ -0,0 +1,19 @@
+add_library("elf" INTERFACE)
+add_library("libs::elf" ALIAS "elf")
+
+target_sources("elf" INTERFACE
+ FILE_SET HEADERS
+ BASE_DIRS "include"
+ FILES
+ "include/elf/format.hpp"
+ "include/elf/section_header.hpp"
+
+)
+
+target_include_directories("elf" INTERFACE
+ "include"
+)
+
+set_target_properties("elf" PROPERTIES
+ VERIFY_INTERFACE_HEADER_SETS YES
+) \ No newline at end of file
diff --git a/libs/elf/include/elf/format.hpp b/libs/elf/include/elf/format.hpp
new file mode 100644
index 0000000..b3220f5
--- /dev/null
+++ b/libs/elf/include/elf/format.hpp
@@ -0,0 +1,15 @@
+#ifndef ELF_FORMAT_HPP
+#define ELF_FORMAT_HPP
+
+namespace elf
+{
+
+ enum struct format
+ {
+ elf32,
+ elf64,
+ };
+
+} // namespace elf
+
+#endif \ No newline at end of file
diff --git a/libs/elf/include/elf/section_header.hpp b/libs/elf/include/elf/section_header.hpp
new file mode 100644
index 0000000..3afe334
--- /dev/null
+++ b/libs/elf/include/elf/section_header.hpp
@@ -0,0 +1,99 @@
+#ifndef ELF_SECTION_HEADER_HPP
+#define ELF_SECTION_HEADER_HPP
+
+#include "format.hpp"
+
+#include <cstdint>
+#include <limits>
+#include <type_traits>
+#include <utility>
+
+namespace elf
+{
+
+ template<format Format>
+ constexpr auto inline section_header_size = std::numeric_limits<std::size_t>::max();
+
+ template<>
+ constexpr auto inline section_header_size<format::elf32> = 40uz;
+
+ template<>
+ constexpr auto inline section_header_size<format::elf64> = 64uz;
+
+ template<format Format>
+ struct section_header
+ {
+ using format_uint = std::conditional_t<Format == format::elf32, std::uint32_t, std::uint64_t>;
+
+ enum struct header_type : std::uint32_t
+ {
+ null = 0, ///< Is inactive
+ program_data = 1, ///< Contains program data
+ symbol_table = 2, ///< Contains a symbol table
+ string_table = 3, ///< Contains a string table
+ relocation_entries_with_addends = 4, ///< Contains relocation information with addends
+ hash_table = 5, ///< Contains a symbol hash table
+ dynamic_linking_entries = 6, ///< Contains dynamic linking information
+ notes = 7, ///< Contains additional notes about the object file
+ no_content = 8, ///< Contains no data
+ relocation_entries_without_addends = 9, ///< Contains relocation information without addends
+ reserved = 10, ///< Reserved for future use
+ dynamic_linker_symbol_table = 11, ///< Contains the dynamic linker symbol table
+ init_array = 14, ///< Contains an array of constructor pointers
+ fini_array = 15, ///< Contains an array of destructor pointers
+ preinit_array = 16, ///< Contains an array of pre-constructor pointers
+ group_table = 17, ///< Defines a section group
+ extended_section_header_indices = 18, ///< Contains extended section header indices
+ };
+
+ enum struct header_flags : format_uint
+ {
+ writeable = 0x1, ///< Contains writable data
+ allocated = 0x2, ///< Occupies memory during execution
+ executable = 0x4, ///< Contains executable instructions
+ mergeable = 0x10, ///< Contained data may be merged for deduplication
+ strings = 0x20, ///< Contains null-terminated strings
+ info_link = 0x40, ///< Contains the section header index of linked section
+ link_order = 0x80, ///< Must respect linking location relative to linked section
+ os_specific = 0x100, ///< Must be handled in an OS specific way
+ group_member = 0x200, ///< Is a member of a section group
+ thread_local_storage = 0x400, ///< Contains thread local storage data
+ compressed = 0x800, ///< Is compressed
+ };
+
+ //! Check if the section is allocated
+ [[nodiscard]] constexpr auto allocated() const noexcept -> bool
+ {
+ return std::to_underlying(flags) & std::to_underlying(header_flags::allocated);
+ }
+
+ //! Check if the section is executable
+ [[nodiscard]] constexpr auto executable() const noexcept -> bool
+ {
+ return std::to_underlying(flags) & std::to_underlying(header_flags::executable);
+ }
+
+ //! Check if the section is writeable
+ [[nodiscard]] constexpr auto writable() const noexcept -> bool
+ {
+ return std::to_underlying(flags) & std::to_underlying(header_flags::writeable);
+ }
+
+ std::uint32_t name_offset; ///< Offset into the section header string table, defining the section name
+ header_type type; ///< Type of this section
+ header_flags flags; ///< Flags of this section
+ format_uint virtual_load_address; ///< Virtual address where this section is loaded
+ format_uint file_offset; ///< Offset of the start of this section's data in the file
+ format_uint size; ///< Size of this section in memory
+ std::uint32_t linked_section; ///< Index of a section this section is linked to
+ std::uint32_t extra_info; ///< Additional information for this section (type and flag dependent)
+ format_uint alignment; ///< Alignment requirement of this section in memory
+ format_uint entry_size; ///< Size of the entries inside this section (if any)
+ };
+
+ static_assert(sizeof(section_header<format::elf32>) == section_header_size<format::elf32>);
+ static_assert(sizeof(section_header<format::elf64>) == section_header_size<format::elf64>);
+
+} // namespace elf
+
+#endif \ No newline at end of file