rucc_asm/format.rs
1//! What an assembler is told about a function or a variable, which is the object format's answer
2//! rather than the machine's.
3//!
4//! Design: `spec/11-asm-objects-debug.md` section 11.3, which is about the object files
5//! themselves. The directives here are the same facts said in text: which section code and data go
6//! in, how a symbol is spelled, which symbols leave the file, and where each one ends.
7//!
8//! They are not the same on the three formats and the differences are not cosmetic. A Mach-O
9//! symbol carries an underscore in front of the C name and an ELF one does not, so a listing that
10//! got that wrong would fail to link against every library on the machine. A local label is
11//! spelled `.L` on ELF and COFF and `L` on Mach-O, and a label that is not spelled the local way
12//! ends up in the symbol table, where it is a name a debugger and a backtrace will show. And ELF
13//! wants a marker saying the stack is not executable, whose absence makes it executable, which
14//! section 11.3 calls out as a real and recurring security bug.
15
16use std::fmt::Write as _;
17
18use rucc_mir as mir;
19use rucc_object::{Alias, Binding, Place};
20use rucc_target::ObjectFormat;
21
22use crate::data::Variable;
23
24/// The directives one object format wraps a function in.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum Directives {
27 /// ELF, which is Linux and the freestanding targets.
28 Elf,
29 /// Mach-O, which is Apple's.
30 MachO,
31 /// COFF, which is Windows.
32 Coff,
33}
34
35impl Directives {
36 /// The directives that go with that object format.
37 #[must_use]
38 pub const fn of(format: ObjectFormat) -> Directives {
39 match format {
40 ObjectFormat::Elf => Directives::Elf,
41 ObjectFormat::MachO => Directives::MachO,
42 ObjectFormat::Coff => Directives::Coff,
43 }
44 }
45
46 /// What goes in front of a C name to make the name the linker sees.
47 ///
48 /// Mach-O keeps the underscore that every Unix linker once had, so `main` in C is `_main` in
49 /// the object, and a listing that leaves it off refers to a symbol nothing defines.
50 #[must_use]
51 pub const fn symbol(self) -> &'static str {
52 match self {
53 Directives::Elf | Directives::Coff => "",
54 Directives::MachO => "_",
55 }
56 }
57
58 /// What goes in front of a label that belongs to one function and leaves no symbol behind.
59 #[must_use]
60 pub const fn local(self) -> &'static str {
61 match self {
62 Directives::Elf | Directives::Coff => ".L",
63 Directives::MachO => "L",
64 }
65 }
66
67 /// The directive that opens the section code goes in.
68 #[must_use]
69 pub const fn text(self) -> &'static str {
70 match self {
71 Directives::Elf | Directives::Coff => "\t.text",
72 Directives::MachO => "\t.section\t__TEXT,__text,regular,pure_instructions",
73 }
74 }
75
76 /// What is said about a function before its first instruction.
77 ///
78 /// The binding is written the way it is written for a variable, and a local one gets no
79 /// directive at all: a name no directive mentions is still in the symbol table, as a local,
80 /// which is what `static` is. Windows says the same thing as a storage class, where three is
81 /// the local one and two the rest.
82 ///
83 /// `align` is in bytes and is a power of two, and the padding is `0x90` because the space in
84 /// front of a function is reached by falling off the end of the one before it.
85 pub fn open(self, out: &mut String, name: &str, align: u32, binding: Binding) {
86 let symbol = self.symbol();
87 let _ = writeln!(out, "\t.p2align\t{}, 0x90", align.max(1).trailing_zeros());
88 match binding {
89 Binding::Global => {
90 let _ = writeln!(out, "\t.globl\t{symbol}{name}");
91 }
92 Binding::Weak => {
93 let _ = writeln!(out, "\t.weak\t{symbol}{name}");
94 }
95 Binding::Local => {}
96 }
97 match self {
98 Directives::Elf => {
99 let _ = writeln!(out, "\t.type\t{name}, @function");
100 }
101 // Windows says the storage class and the type code, and thirty two is a function.
102 Directives::Coff => {
103 let scl = if binding == Binding::Local { 3 } else { 2 };
104 let _ = writeln!(out, "\t.def\t{name}\n\t.scl\t{scl}\n\t.type\t32\n\t.endef");
105 }
106 Directives::MachO => {}
107 }
108 let _ = writeln!(out, "{symbol}{name}:");
109 }
110
111 /// The directive that opens the section a variable goes in.
112 ///
113 /// The three formats disagree about the names and about how much has to be said. ELF and COFF
114 /// have a directive per section that every assembler knows, and both want the flags spelled
115 /// out for a section the program named, since nothing else says whether it may be written to.
116 /// Mach-O has one directive and a segment in front of every section name.
117 pub fn section(self, out: &mut String, place: &Place) {
118 match (self, place) {
119 // A tentative definition is not in a section at all, and the caller is what decides
120 // that. It is answered here as the section it would otherwise have gone in, so that
121 // the match stays about sections and nothing has to be said twice.
122 (Directives::Elf | Directives::Coff, Place::Written | Place::Merged) => {
123 out.push_str("\t.data\n");
124 }
125 (Directives::Elf | Directives::Coff, Place::Zero) => out.push_str("\t.bss\n"),
126 (Directives::Elf, Place::ReadOnly) => out.push_str("\t.section\t.rodata\n"),
127 (Directives::Elf, Place::RelocReadOnly { local }) => {
128 let name = if *local { ".data.rel.ro.local" } else { ".data.rel.ro" };
129 let _ = writeln!(out, "\t.section\t{name},\"aw\",@progbits");
130 }
131 // COFF has no section of this kind and needs none. A Windows image is relocated as a
132 // whole rather than a symbol at a time, and the loader makes whatever pages it has to
133 // write writable for as long as it is writing them and puts them back afterwards, so
134 // an address in a read only section costs a base relocation and nothing else.
135 (Directives::Coff, Place::ReadOnly | Place::RelocReadOnly { .. }) => {
136 out.push_str("\t.section\t.rdata,\"dr\"\n");
137 }
138 (Directives::Elf, Place::Named(name)) => {
139 let _ = writeln!(out, "\t.section\t{name},\"aw\",@progbits");
140 }
141 (Directives::Coff, Place::Named(name)) => {
142 let _ = writeln!(out, "\t.section\t{name},\"dw\"");
143 }
144 (Directives::MachO, Place::ReadOnly) => out.push_str("\t.section\t__TEXT,__const\n"),
145 // Mach-O has the same problem and the same answer under a different name. A section in
146 // `__TEXT` is never writable, so a constant holding an address goes in `__DATA,__const`
147 // instead, which `dyld` writes and then protects. There is no `.local` half: the layout
148 // hint is an ELF linker's, and this one has nothing to do with it.
149 (Directives::MachO, Place::RelocReadOnly { .. }) => {
150 out.push_str("\t.section\t__DATA,__const\n");
151 }
152 // A Mach-O section name carries the segment it is in, so a program that named one
153 // named both halves and there is nothing to add to it.
154 (Directives::MachO, Place::Named(name)) => {
155 let _ = writeln!(out, "\t.section\t{name}");
156 }
157 (Directives::MachO, _) => out.push_str("\t.section\t__DATA,__data\n"),
158 }
159 }
160
161 /// What is said about a variable before its image, and whether an image follows.
162 ///
163 /// Two kinds of variable are one directive rather than a section, a label and bytes. A
164 /// tentative definition is a request to the linker for that much zeroed space on every format,
165 /// and on Mach-O so is a variable whose image is all zeros, because the section that would
166 /// hold it is one nothing may write bytes into.
167 pub fn variable(self, out: &mut String, var: &Variable) -> bool {
168 let symbol = self.symbol();
169 let align = var.align.max(1).trailing_zeros();
170 match (self, &var.place) {
171 (_, Place::Merged) => {
172 let comm = if var.binding == Binding::Local { ".lcomm" } else { ".comm" };
173 let name = &var.name;
174 let _ = writeln!(out, "\t{comm}\t{symbol}{name},{},{}", var.size, var.align);
175 return false;
176 }
177 (Directives::MachO, Place::Zero) => {
178 let name = &var.name;
179 let _ =
180 writeln!(out, "\t.zerofill\t__DATA,__bss,{symbol}{name},{},{align}", var.size);
181 return false;
182 }
183 _ => {}
184 }
185 self.section(out, &var.place);
186 match var.binding {
187 Binding::Global => {
188 let _ = writeln!(out, "\t.globl\t{symbol}{}", var.name);
189 }
190 Binding::Weak => {
191 let _ = writeln!(out, "\t.weak\t{symbol}{}", var.name);
192 }
193 // Nothing, which is what makes it invisible outside the file. A name no directive
194 // mentions is still in the symbol table as a local one, which is what `static` is.
195 Binding::Local => {}
196 }
197 let _ = writeln!(out, "\t.p2align\t{align}");
198 if self == Directives::Elf {
199 let _ = writeln!(out, "\t.type\t{}, @object", var.name);
200 }
201 let _ = writeln!(out, "{symbol}{}:", var.name);
202 true
203 }
204
205 /// What is said about a function after its last instruction.
206 ///
207 /// The size, on the format that has one. It is written as the distance from the label to here
208 /// rather than as a number, because the assembler is the one that knows how long an
209 /// instruction turned out to be and this file is what it is about to find out from.
210 pub fn close(self, out: &mut String, name: &str) {
211 if self == Directives::Elf {
212 let _ = writeln!(out, "\t.size\t{name}, .-{name}");
213 }
214 }
215
216 /// A second name for something the file already wrote down.
217 ///
218 /// The binding and then `.set`, which is all gcc writes and all an assembler needs: the type
219 /// and the size of the new symbol are taken from the old one, so writing them again would
220 /// only be a second chance to disagree. Nothing opens a section first, because the symbol is
221 /// an entry in a table rather than a byte of anything, and no `.size` closes it for the same
222 /// reason.
223 pub fn alias(self, out: &mut String, alias: &Alias) {
224 let symbol = self.symbol();
225 match alias.binding {
226 Binding::Global => {
227 let _ = writeln!(out, "\t.globl\t{symbol}{}", alias.name);
228 }
229 Binding::Weak => {
230 let _ = writeln!(out, "\t.weak\t{symbol}{}", alias.name);
231 }
232 Binding::Local => {}
233 }
234 let _ = writeln!(out, "\t.set\t{symbol}{},{symbol}{}", alias.name, alias.target);
235 }
236
237 /// What is said once, after every function.
238 pub fn end(self, out: &mut String) {
239 match self {
240 // Without this the stack is executable, which is not a default anybody chose.
241 Directives::Elf => out.push_str("\t.section\t.note.GNU-stack,\"\",@progbits\n"),
242 // What lets the linker throw away a function nothing calls, which it cannot do
243 // without being told that the boundaries between them are real.
244 Directives::MachO => out.push_str("\t.subsections_via_symbols\n"),
245 Directives::Coff => {}
246 }
247 }
248}
249
250/// What the object file is told about a function's name, from what the machine function carries.
251///
252/// Two names for one set of three, because the machine IR is not allowed to know what an object
253/// file is and the object writer is not allowed to know what a machine function is. This crate is
254/// where they meet, which is where the two spellings are put side by side.
255#[must_use]
256pub(crate) fn binding(binding: mir::Binding) -> Binding {
257 match binding {
258 mir::Binding::Global => Binding::Global,
259 mir::Binding::Local => Binding::Local,
260 mir::Binding::Weak => Binding::Weak,
261 }
262}
263
264#[cfg(test)]
265mod tests {
266 use rucc_object::FUNC_ALIGN;
267
268 use super::*;
269
270 #[test]
271 fn a_mach_o_symbol_is_the_c_name_with_an_underscore_in_front_of_it() {
272 let mut out = String::new();
273 Directives::MachO.open(&mut out, "main", 16, Binding::Global);
274 assert!(out.contains("\t.globl\t_main\n"), "{out}");
275 assert!(out.contains("\n_main:\n"), "{out}");
276 // No type and no size, neither of which Mach-O has.
277 assert!(!out.contains(".type"), "{out}");
278 let mut close = String::new();
279 Directives::MachO.close(&mut close, "main");
280 assert_eq!(close, "");
281 }
282
283 #[test]
284 fn an_elf_function_says_what_it_is_and_how_long_it_is() {
285 let mut out = String::new();
286 Directives::Elf.open(&mut out, "main", 16, Binding::Global);
287 Directives::Elf.close(&mut out, "main");
288 assert!(out.contains("\t.type\tmain, @function\n"), "{out}");
289 assert!(out.contains("\t.size\tmain, .-main\n"), "{out}");
290 }
291
292 #[test]
293 fn a_function_that_asked_to_be_more_aligned_is_written_at_that_alignment() {
294 let mut out = String::new();
295 Directives::Elf.open(&mut out, "f", 256, Binding::Global);
296 // The directive counts in powers of two and the attribute counts in bytes, and two
297 // hundred and fifty six bytes is eight of them.
298 assert!(out.contains("\t.p2align\t8, 0x90\n"), "{out}");
299 let mut plain = String::new();
300 Directives::Elf.open(&mut plain, "f", FUNC_ALIGN, Binding::Global);
301 assert!(plain.contains("\t.p2align\t4, 0x90\n"), "{plain}");
302 }
303
304 #[test]
305 fn an_elf_file_says_the_stack_is_not_executable() {
306 // The absence of this is what makes it executable, so the test is that it is there
307 // rather than that it is spelled a particular way.
308 let mut out = String::new();
309 Directives::Elf.end(&mut out);
310 assert!(out.contains(".note.GNU-stack"), "{out}");
311 }
312
313 #[test]
314 fn every_object_format_has_directives() {
315 for format in [ObjectFormat::Elf, ObjectFormat::MachO, ObjectFormat::Coff] {
316 let directives = Directives::of(format);
317 assert!(directives.text().starts_with('\t'));
318 let mut out = String::new();
319 directives.open(&mut out, "f", 16, Binding::Global);
320 directives.close(&mut out, "f");
321 directives.end(&mut out);
322 assert!(out.ends_with('\n'), "{format:?} left a line unfinished");
323 }
324 }
325}