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 11 reaches down
9//! to these at rank 9, 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 /// How far outside a shared library holding this the name reaches.
60 pub visibility: Visibility,
61}
62
63/// The variables a file defines, and what the linker has to be told about them.
64///
65/// One entry per variable rather than one section of everything, because where a variable goes is
66/// worked out from what it is and two of them that land in one section still have their own
67/// alignment, their own size and their own symbol. Putting them together is the writer's job and
68/// is the one part of it the three formats disagree about.
69#[derive(Debug, Clone, Default, PartialEq, Eq)]
70pub struct Data {
71 /// Every variable this file defines, in the order the module held them.
72 pub objects: Vec<Object>,
73}
74
75/// A second name for something the same file defines.
76///
77/// Not a section and not a byte of anything, which is the whole point of it: an alias is a symbol
78/// table entry pointing at an address something else already occupies, so a file with one in it is
79/// no larger than the same file without. `.set b, a` is what an assembler is told and a second
80/// entry at the first one's section, value and size is what a writer produces, and the two say the
81/// same thing.
82///
83/// The target is a name rather than an index into anything above, because the two output paths
84/// find it in different places: a listing hands the name to an assembler that resolves it, and a
85/// writer looks it up among the symbols it has already added.
86#[derive(Debug, Clone, PartialEq, Eq)]
87pub struct Alias {
88 /// The name being defined, as the C program spelled it.
89 pub name: String,
90 /// The name it stands for, which has to be something this same file defines.
91 pub target: String,
92 /// How the linker sees the new name, which is not always how it sees the old one: the target
93 /// of `extern int b __attribute__((alias("a")))` may be a `static`.
94 pub binding: Binding,
95 /// How far outside a shared library holding this the new name reaches, which is its own
96 /// answer for the same reason the binding is: the attribute is written on the alias.
97 pub visibility: Visibility,
98}
99
100/// One global variable, laid out.
101#[derive(Debug, Clone, PartialEq, Eq)]
102pub struct Object {
103 /// Its name, as the C program spelled it. The underscore an Apple symbol carries is the
104 /// object writer's business, not this one's.
105 pub name: String,
106 /// Its image, and nothing at all when it is zero filled and the file carries none of it.
107 pub bytes: Vec<u8>,
108 /// How many bytes it occupies, which is the length of the image except when there is none.
109 pub size: u64,
110 /// What it has to be aligned to, always a power of two.
111 pub align: u64,
112 /// Which section it goes in.
113 pub place: Place,
114 /// How the linker sees the name.
115 pub binding: Binding,
116 /// How far outside a shared library holding this the name reaches.
117 pub visibility: Visibility,
118 /// Every place in its image that holds the address of a symbol, counted from the start of
119 /// the image rather than from the start of the section it lands in.
120 pub relocs: Vec<Reloc>,
121}
122
123/// Which section a variable goes in.
124///
125/// Worked out from what the variable is rather than named by it, except in the one case where the
126/// program named it. A reader who wants to know why a variable is in `.rodata` should be able to
127/// find the answer in the variable.
128#[derive(Debug, Clone, PartialEq, Eq)]
129pub enum Place {
130 /// Written to, and its image is not all zeros. `.data`.
131 Written,
132 /// Never written to, so it can go in a page the loader maps read only and every process
133 /// running the program can share. `.rodata`.
134 ReadOnly,
135 /// Never written to by the program, but written once by the dynamic linker, because its image
136 /// holds the address of something and an address is not known until the image is loaded.
137 /// `.data.rel.ro`.
138 ///
139 /// The section has to be writable for that one write and read only afterwards, which is what
140 /// the `PT_GNU_RELRO` segment is: the loader maps it, the relocations are applied, and then it
141 /// is turned read only before the program starts. Putting the variable in `.rodata` instead
142 /// means asking the linker to leave a relocation in a section that is never writable, and what
143 /// it does about that is give the whole image `DT_TEXTREL`, which gives up the protection the
144 /// section was for. Some hardened toolchains refuse the link outright.
145 RelocReadOnly {
146 /// Whether every address in the image is of something this file defines and does not
147 /// export, which means the link can resolve them all and none can be interposed.
148 ///
149 /// Those go in `.data.rel.ro.local`, which the linker puts in the first pages of the
150 /// segment, so the pages holding them are the ones the loader is done with soonest. It is
151 /// a hint about layout rather than a difference in what the section is.
152 local: bool,
153 },
154 /// All zeros, so the file says how big it is and carries none of it. `.bss`.
155 Zero,
156 /// A tentative definition, which is not in a section at all: the linker is asked for that
157 /// much zeroed space and merges every definition of the name into one. `.comm`.
158 Merged,
159 /// The section the program named, from `__attribute__((section(...)))`.
160 Named(String),
161}
162
163/// How the linker sees a name.
164///
165/// Three of the five linkages the IR has, because that is how many an object file can say. Which
166/// of the two weak ones a symbol had is a fact the optimizer needs and the linker does not.
167#[derive(Debug, Clone, Copy, PartialEq, Eq)]
168pub enum Binding {
169 /// Visible to every other object, and the definition here is the definition.
170 Global,
171 /// Invisible outside this object, which is what `static` at file scope means.
172 Local,
173 /// Visible, and allowed to lose to a definition in another object.
174 Weak,
175}
176
177/// How far outside a shared library a name reaches.
178///
179/// A different question from [`Binding`] and asked of a different linker. The binding is what the
180/// static linker does with a name while it is building the output, and this is what the dynamic
181/// linker may do with it once the output is a shared library and is being loaded. A hidden name is
182/// still global to the static link, so two files in the same library can call each other by it; it
183/// is simply not in the dynamic symbol table afterwards, so nothing outside can name it.
184///
185/// Written down here as its own thing rather than folded into the binding because it is the
186/// mistake tamnd/rucc#733 was: a writer that has one word for both ends up saying something about
187/// visibility while it thinks it is saying something about linkage, and what it said was hidden.
188///
189/// It means nothing for a [`Binding::Local`] name. `static` is already invisible to the whole
190/// world outside the file, and ELF records `STV_DEFAULT` for one, which is what gcc writes.
191#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
192pub enum Visibility {
193 /// In the dynamic symbol table, and a reference from inside the library may be satisfied by a
194 /// definition somewhere else, which is what makes `LD_PRELOAD` work. What a name gets when
195 /// nothing said otherwise.
196 #[default]
197 Default,
198 /// Not in the dynamic symbol table at all, so nothing outside the library can name it and
199 /// every reference to it from inside binds here. `__attribute__((visibility("hidden")))`.
200 Hidden,
201 /// In the dynamic symbol table, so something outside can name it, but a reference from inside
202 /// the library binds to the definition inside it and cannot be interposed.
203 Protected,
204}
205
206/// One reference to something this file does not contain.
207#[derive(Debug, Clone, PartialEq, Eq)]
208pub struct Reloc {
209 /// Where the bytes the linker writes over begin.
210 pub at: usize,
211 /// What is wanted, as the C program spelled it.
212 pub symbol: String,
213 /// What the linker is being asked for.
214 pub kind: Reference,
215 /// What to add to the distance, which is the constant the instruction already meant plus the
216 /// bytes between the hole and the end of the instruction, negated. An instruction counts from
217 /// where it ends and a relocation counts from where it starts, and this is the difference.
218 pub addend: i64,
219}
220
221/// What kind of thing a relocation is asking the linker for.
222///
223/// The first three are the distance from the end of an instruction to something, which is what
224/// every reference the code makes is, because this compiler generates position independent code and
225/// nothing else. They are told apart by what the linker is allowed to do about each one. The fourth
226/// is not a distance at all and is the only kind an image asks for, since an initializer holding the
227/// address of something holds the address itself.
228#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229pub enum Reference {
230 /// A call, which the linker may satisfy with a stub that reaches further than the four bytes
231 /// would. `R_X86_64_PLT32` on ELF, and the same relocation a branch gets on the other two.
232 Call,
233 /// A datum, reached from the instruction pointer. `R_X86_64_PC32` on ELF.
234 Data,
235 /// A slot of the global offset table, reached from the instruction pointer, holding the
236 /// address of something another object may be the one that defines.
237 ///
238 /// The distance to the slot rather than to the thing, which is the whole difference: the
239 /// distance to the thing is a number only a link that puts the thing in this program can
240 /// work out, and a shared library is a link that does not. `R_X86_64_REX_GOTPCRELX` on ELF,
241 /// which says the instruction is a `mov` with a REX prefix and lets the linker turn it back
242 /// into the `lea` it would have been if the symbol had been here all along.
243 Got,
244 /// The address itself, written into an image. `int *p = &y;` and nothing else in C.
245 Address {
246 /// How many bytes of it are written, which is the pointer width except on a target with
247 /// a narrower relocation for it. `R_X86_64_64` and `R_X86_64_32` on ELF.
248 bytes: u8,
249 },
250}