Skip to main content

rucc_object/
section.rs

1//! What an object writer is given, which is a section of bytes and what the linker has to be
2//! told about them.
3//!
4//! Design: `spec/11-asm-objects-debug.md` sections 11.1 and 11.3.
5//!
6//! These types are here rather than beside the assembler that fills them in because they are what
7//! an object file is made of, and because a writer cannot depend on the thing that produces its
8//! input without the graph going the wrong way round. The assembler at layer rank 10 reaches down
9//! to these at rank 8, which is the direction `spec/18-package-layout.md` asks for.
10
11/// What a function is aligned to when nothing asked for more.
12///
13/// Sixteen because that is what every x86-64 toolchain puts a function at, and because it is what
14/// keeps the loop inside one from straddling one more cache line than it has to. Here rather than
15/// beside the assembler because the assembler pads to it and the writer records it, and two
16/// copies of one number is how the padding and the record come apart.
17pub const FUNC_ALIGN: u32 = 16;
18
19/// A text section, and what the linker has to be told about it.
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct Text {
22    /// The instructions, in the order they were laid out.
23    pub bytes: Vec<u8>,
24    /// Where each function starts and how long it is, in the order they were written.
25    pub funcs: Vec<Extent>,
26    /// Every place in the bytes that names something the linker has to find.
27    pub relocs: Vec<Reloc>,
28    /// What the whole section has to be aligned to, which is the largest alignment any function
29    /// in it asked for.
30    ///
31    /// A function is at a fixed offset inside the section, so a function at a multiple of two
32    /// hundred and fifty six is one only if the section itself is at one. The padding between the
33    /// functions is the assembler's half of the same job and this is the linker's.
34    pub align: u32,
35}
36
37impl Default for Text {
38    fn default() -> Self {
39        Self { bytes: Vec::new(), funcs: Vec::new(), relocs: Vec::new(), align: FUNC_ALIGN }
40    }
41}
42
43/// Where one function ended up.
44///
45/// How long a function is is a fact ELF records and Mach-O has no way to, so it is handed over
46/// rather than worked out again: the writer that wants it has it and the one that does not
47/// ignores it.
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct Extent {
50    /// The function's name, as the C program spelled it. The underscore an Apple symbol carries
51    /// is the object writer's business, not this one's.
52    pub name: String,
53    /// Where its first instruction is.
54    pub start: usize,
55    /// How many bytes of instructions it is, not counting the padding in front of the next one.
56    pub len: usize,
57    /// How the linker sees the name, which is what the C `static` reaches the object file as.
58    pub binding: Binding,
59}
60
61/// The variables a file defines, and what the linker has to be told about them.
62///
63/// One entry per variable rather than one section of everything, because where a variable goes is
64/// worked out from what it is and two of them that land in one section still have their own
65/// alignment, their own size and their own symbol. Putting them together is the writer's job and
66/// is the one part of it the three formats disagree about.
67#[derive(Debug, Clone, Default, PartialEq, Eq)]
68pub struct Data {
69    /// Every variable this file defines, in the order the module held them.
70    pub objects: Vec<Object>,
71}
72
73/// One global variable, laid out.
74#[derive(Debug, Clone, PartialEq, Eq)]
75pub struct Object {
76    /// Its name, as the C program spelled it. The underscore an Apple symbol carries is the
77    /// object writer's business, not this one's.
78    pub name: String,
79    /// Its image, and nothing at all when it is zero filled and the file carries none of it.
80    pub bytes: Vec<u8>,
81    /// How many bytes it occupies, which is the length of the image except when there is none.
82    pub size: u64,
83    /// What it has to be aligned to, always a power of two.
84    pub align: u64,
85    /// Which section it goes in.
86    pub place: Place,
87    /// How the linker sees the name.
88    pub binding: Binding,
89    /// Every place in its image that holds the address of a symbol, counted from the start of
90    /// the image rather than from the start of the section it lands in.
91    pub relocs: Vec<Reloc>,
92}
93
94/// Which section a variable goes in.
95///
96/// Worked out from what the variable is rather than named by it, except in the one case where the
97/// program named it. A reader who wants to know why a variable is in `.rodata` should be able to
98/// find the answer in the variable.
99#[derive(Debug, Clone, PartialEq, Eq)]
100pub enum Place {
101    /// Written to, and its image is not all zeros. `.data`.
102    Written,
103    /// Never written to, so it can go in a page the loader maps read only and every process
104    /// running the program can share. `.rodata`.
105    ReadOnly,
106    /// All zeros, so the file says how big it is and carries none of it. `.bss`.
107    Zero,
108    /// A tentative definition, which is not in a section at all: the linker is asked for that
109    /// much zeroed space and merges every definition of the name into one. `.comm`.
110    Merged,
111    /// The section the program named, from `__attribute__((section(...)))`.
112    Named(String),
113}
114
115/// How the linker sees a name.
116///
117/// Three of the five linkages the IR has, because that is how many an object file can say. Which
118/// of the two weak ones a symbol had is a fact the optimizer needs and the linker does not.
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum Binding {
121    /// Visible to every other object, and the definition here is the definition.
122    Global,
123    /// Invisible outside this object, which is what `static` at file scope means.
124    Local,
125    /// Visible, and allowed to lose to a definition in another object.
126    Weak,
127}
128
129/// One reference to something this file does not contain.
130#[derive(Debug, Clone, PartialEq, Eq)]
131pub struct Reloc {
132    /// Where the bytes the linker writes over begin.
133    pub at: usize,
134    /// What is wanted, as the C program spelled it.
135    pub symbol: String,
136    /// What the linker is being asked for.
137    pub kind: Reference,
138    /// What to add to the distance, which is the constant the instruction already meant plus the
139    /// bytes between the hole and the end of the instruction, negated. An instruction counts from
140    /// where it ends and a relocation counts from where it starts, and this is the difference.
141    pub addend: i64,
142}
143
144/// What kind of thing a relocation is asking the linker for.
145///
146/// The first two are the distance from the end of an instruction to something, which is what every
147/// reference the code makes is, because this compiler generates position independent code and
148/// nothing else. They are told apart because the linker may answer one of them with a stub and may
149/// not answer the other one that way. The third is not a distance at all and is the only kind an
150/// image asks for, since an initializer holding the address of something holds the address itself.
151#[derive(Debug, Clone, Copy, PartialEq, Eq)]
152pub enum Reference {
153    /// A call, which the linker may satisfy with a stub that reaches further than the four bytes
154    /// would. `R_X86_64_PLT32` on ELF, and the same relocation a branch gets on the other two.
155    Call,
156    /// A datum, reached from the instruction pointer. `R_X86_64_PC32` on ELF.
157    Data,
158    /// The address itself, written into an image. `int *p = &y;` and nothing else in C.
159    Address {
160        /// How many bytes of it are written, which is the pointer width except on a target with
161        /// a narrower relocation for it. `R_X86_64_64` and `R_X86_64_32` on ELF.
162        bytes: u8,
163    },
164}