commonlibsse_ng\rel\module/segment.rs
1// C++ Original code
2// - https://github.com/SARDONYX-forks/CommonLibVR/blob/ng/include/REL/Module.h
3// - load_segments, clear: https://github.com/SARDONYX-forks/CommonLibVR/blob/ng/src/REL/Module.cpp
4// SPDX-FileCopyrightText: (C) 2018 Ryan-rsm-McKenzie
5// SPDX-License-Identifier: MIT
6//
7// SPDX-FileCopyrightText: (C) 2025 SARDONYX
8// SPDX-License-Identifier: Apache-2.0 OR MIT
9
10//! Defines segment(e.g. `.text`) types.
11
12use super::ModuleHandle;
13
14/// Represents a memory segment in a module.
15#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub struct Segment {
17 /// Base address of the proxy module.
18 pub proxy_base: ModuleHandle,
19 /// Virtual address of the segment.
20 pub address: u32,
21 /// Size of the segment in bytes.
22 pub size: u32,
23}
24
25impl Segment {
26 pub const fn const_default() -> Self {
27 Self { proxy_base: ModuleHandle::const_default(), address: 0, size: 0 }
28 }
29
30 /// Computes the offset of the segment from the proxy base.
31 #[inline]
32 pub fn offset(&self) -> usize {
33 (self.address as usize).wrapping_sub(self.proxy_base.0.addr().get())
34 }
35}
36
37#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
38#[repr(usize)]
39/// Represents different sections in a binary executable.
40pub enum SegmentName {
41 /// Executable code section (typically `.text`).
42 #[default]
43 Textx,
44
45 /// Initialized data section (typically `.idata`).
46 Idata,
47
48 /// Read-only data section (typically `.rdata`).
49 Rdata,
50
51 /// Writable data section (typically `.data`).
52 Data,
53
54 /// Exception handling metadata section (typically `.pdata`).
55 Pdata,
56
57 /// Thread-local storage section (typically `.tls`).
58 Tls,
59
60 /// Writable text section (uncommon, but may exist for self-modifying code).
61 Textw,
62
63 /// Global function identifiers section (typically `.gfids`).
64 Gfids,
65}