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 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/// Whether each function and each variable gets a section to itself.
20///
21/// Design: `spec/11-asm-objects-debug.md` section 11.3, and `spec/04-driver-and-cli.md` section 4.7
22/// for the flags that ask for it.
23///
24/// A linker can drop a section nothing reaches and cannot drop half of one, so a file whose
25/// functions share a section keeps every function that file defines in the output as soon as any
26/// one of them is called. Splitting them is what makes `--gc-sections` do anything, which is how an
27/// embedded image or a kernel gets small, and it is the whole of what these two flags are for. The
28/// cost is a section header per name, which is why it is asked for rather than always done.
29///
30/// Not one flag, because gcc has two and a build that wants one of them and not the other is a
31/// build that measured something. Splitting the code is nearly free at link time; splitting the
32/// data can defeat the linker's ordering of what is next to what.
33#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
34pub struct Sections {
35    /// `-ffunction-sections`. Each function in `.text.<name>` rather than all of them in `.text`.
36    pub functions: bool,
37    /// `-fdata-sections`. Each variable in a section named after it rather than in the one its
38    /// contents would otherwise have chosen.
39    pub data: bool,
40}
41
42impl Sections {
43    /// Whether either of them was asked for.
44    #[must_use]
45    pub const fn any(self) -> bool {
46        self.functions || self.data
47    }
48}
49
50/// What a file says it was built to have checked, which is what `-fcf-protection=` asks for.
51///
52/// Design: `spec/11-asm-objects-debug.md` section 11.3, and `spec/04-driver-and-cli.md` section 4.7
53/// for the flag.
54///
55/// A machine's control flow checks are turned on for a whole process or not at all, never for one
56/// function, so a program made of one object built with them and one built without has to be run
57/// one way or the other. What everybody settled on is that each object records what it was built
58/// for, the linker keeps only what every input agreed on, and the loader turns on what is left. So
59/// an object that records nothing turns the check off for every object it is linked with, which is
60/// why this is written even when the flag changed no instruction in the file.
61///
62/// One number rather than a pair of flags, because that is what the record holds: a word of bits
63/// whose meaning is the machine's, and a linker that has never heard of a bit still knows to drop
64/// it when one input does not have it.
65#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
66pub struct Property {
67    /// The bits of the x86 feature word, which are [`Self::IBT`] and [`Self::SHSTK`].
68    pub features: u32,
69}
70
71impl Property {
72    /// Which property the feature word is, which is the key the record is written under.
73    pub const X86_FEATURES: u32 = 0xc000_0002;
74    /// Indirect branch tracking: every indirect call and jump in the file arrives at a landing
75    /// pad, so the machine may fault on one that does not.
76    pub const IBT: u32 = 1;
77    /// The shadow stack: every return in the file goes where a second copy of the return address
78    /// says it should, so the machine may fault when the two disagree.
79    pub const SHSTK: u32 = 2;
80
81    /// Whether anything is recorded at all, which is whether the record is written.
82    #[must_use]
83    pub const fn any(self) -> bool {
84        self.features != 0
85    }
86}
87
88/// What the command line decided about the file being written, as against what the code in it
89/// decided.
90///
91/// Two answers with nothing to do with each other, together because they arrive together: neither
92/// can be worked out from a function, and the listing and the byte writer have to be handed the
93/// same pair or the two outputs of one command line would not be the same file.
94#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
95pub struct Output {
96    /// Whether each function and each variable gets a section to itself.
97    pub sections: Sections,
98    /// What the file says it was built to have checked.
99    pub property: Property,
100}
101
102/// A text section, and what the linker has to be told about it.
103#[derive(Debug, Clone, PartialEq, Eq)]
104pub struct Text {
105    /// The instructions, in the order they were laid out.
106    pub bytes: Vec<u8>,
107    /// Where each function starts and how long it is, in the order they were written.
108    pub funcs: Vec<Extent>,
109    /// Every place in the bytes that names something the linker has to find.
110    pub relocs: Vec<Reloc>,
111    /// Every place inside a function that has a name of its own, in the order they were written.
112    pub labels: Vec<Marker>,
113    /// What the whole section has to be aligned to, which is the largest alignment any function
114    /// in it asked for.
115    ///
116    /// A function is at a fixed offset inside the section, so a function at a multiple of two
117    /// hundred and fifty six is one only if the section itself is at one. The padding between the
118    /// functions is the assembler's half of the same job and this is the linker's.
119    pub align: u32,
120    /// What an unwinder is told about the functions, which is empty for a format that has no such
121    /// section or a build that asked for none.
122    pub unwind: Unwind,
123}
124
125impl Default for Text {
126    fn default() -> Self {
127        Self {
128            bytes: Vec::new(),
129            funcs: Vec::new(),
130            relocs: Vec::new(),
131            labels: Vec::new(),
132            align: FUNC_ALIGN,
133            unwind: Unwind::default(),
134        }
135    }
136}
137
138/// The unwind table, as the bytes of the section or two it goes in and what the linker has to be
139/// told about them.
140///
141/// Bytes rather than rows, because what a record is is the platform's answer rather than the object
142/// writer's, and the layer that knows what a frame did is the one that can say it in the fewest of
143/// them. What is left for the writer is where the sections go and what their relocations are.
144///
145/// Two of them, because the two platforms lay the same facts out differently. ELF writes one section
146/// of records, each a little program an unwinder runs to rebuild the frame at an address, and each
147/// carrying its own codes, so [`Self::info`] is empty there. Windows writes a table of fixed rows
148/// sorted by address, one per function, each pointing at the description of that function's prologue
149/// in a second section, which is what [`Self::info`] holds.
150///
151/// Every record says where its function is, and where a function is is a number no compilation
152/// knows: a function is at a fixed offset inside its own section and the section is placed by the
153/// linker. So there are relocations, and which kind they are is the format's answer too.
154#[derive(Debug, Clone, Default, PartialEq, Eq)]
155pub struct Unwind {
156    /// The records: one shared header and one per function on ELF, and one row per function on
157    /// Windows.
158    pub bytes: Vec<u8>,
159    /// Every place in them that names something the linker has to place, which is the functions
160    /// they are about and, where there is a second section, the description each row points at.
161    pub relocs: Vec<Reloc>,
162    /// What those records point at, on the format that keeps the two apart, and nothing at all on
163    /// the one whose records carry their own.
164    pub info: Vec<u8>,
165    /// The names inside [`Self::info`], one per function that has a description there, which are
166    /// what the relocations above ask for.
167    ///
168    /// Names rather than offsets because a relocation names a symbol, and the record and the thing
169    /// it points at are in two different sections, so there is no distance either of them can be
170    /// written with instead.
171    pub labels: Vec<Marker>,
172}
173
174/// Where the room a patcher was promised at the top of a function ended up.
175///
176/// What `-fpatchable-function-entry=` asks for, once it is bytes rather than instructions. Two
177/// numbers because the writer has two questions: where the address it records points, and how much
178/// of the function is in front of the symbol.
179///
180/// They are not the same number. The room can be split by the landing pad a function opens with,
181/// since the pad has to be the first instruction after the label and the room does not, so the part
182/// in front of the label and the part after it are not always next to each other. What is recorded
183/// is the front of the whole thing, which is the part in front of the label when there is one.
184#[derive(Debug, Clone, Copy, PartialEq, Eq)]
185pub struct Patch {
186    /// Where the room begins, as an offset into the same bytes [`Extent::start`] is one into.
187    pub at: usize,
188    /// How many bytes of the function are in front of [`Extent::start`], which is where its symbol
189    /// is and where an unwinder is told the function begins.
190    pub before: usize,
191}
192
193/// Where a place inside a function that has a name of its own ended up.
194///
195/// What asks for one is GNU's address of a label in the initializer of an object with static
196/// storage duration. A label is somewhere a jump goes and a jump is a distance the assembler works
197/// out, so no ordinary label is in the symbol table at all. An image is the other case: it is in
198/// another section, so what it holds is a relocation, and a relocation names a symbol.
199///
200/// The name is never one the program wrote, so nothing outside this file looks it up and it is
201/// always local: it is here for a relocation in this same file to resolve against, and a linker
202/// that offered it to another file would be offering the middle of a function.
203#[derive(Debug, Clone, PartialEq, Eq)]
204pub struct Marker {
205    /// The name, which is whatever the compiler minted for it.
206    pub name: String,
207    /// Where it is, as an offset into the same bytes [`Extent::start`] is one into.
208    pub at: usize,
209}
210
211/// Where one function ended up.
212///
213/// How long a function is is a fact ELF records and Mach-O has no way to, so it is handed over
214/// rather than worked out again: the writer that wants it has it and the one that does not
215/// ignores it.
216#[derive(Debug, Clone, PartialEq, Eq)]
217pub struct Extent {
218    /// The function's name, as the C program spelled it. The underscore an Apple symbol carries
219    /// is the object writer's business, not this one's.
220    pub name: String,
221    /// Where its first instruction is.
222    pub start: usize,
223    /// How many bytes of instructions it is, not counting the padding in front of the next one.
224    pub len: usize,
225    /// What this one function asked to be aligned to, which is not always what the section it is
226    /// in was aligned to.
227    ///
228    /// The two are the same number only when this function is the one that asked for the most.
229    /// Under [`Sections::functions`] each function is a section of its own and this is what that
230    /// section is aligned to, so the number has to survive the trip rather than be recovered from
231    /// the offset, which says nothing once the function is at zero in a section of its own.
232    pub align: u32,
233    /// How the linker sees the name, which is what the C `static` reaches the object file as.
234    pub binding: Binding,
235    /// How far outside a shared library holding this the name reaches.
236    pub visibility: Visibility,
237    /// Where the room a patcher was promised is, or `None` in a function promised none, which is
238    /// every function on a command line that did not ask. See [`Patch`].
239    pub patch: Option<Patch>,
240}
241
242/// The variables a file defines, and what the linker has to be told about them.
243///
244/// One entry per variable rather than one section of everything, because where a variable goes is
245/// worked out from what it is and two of them that land in one section still have their own
246/// alignment, their own size and their own symbol. Putting them together is the writer's job and
247/// is the one part of it the three formats disagree about.
248#[derive(Debug, Clone, Default, PartialEq, Eq)]
249pub struct Data {
250    /// Every variable this file defines, in the order the module held them.
251    pub objects: Vec<Object>,
252}
253
254/// A second name for something the same file defines.
255///
256/// Not a section and not a byte of anything, which is the whole point of it: an alias is a symbol
257/// table entry pointing at an address something else already occupies, so a file with one in it is
258/// no larger than the same file without. `.set b, a` is what an assembler is told and a second
259/// entry at the first one's section, value and size is what a writer produces, and the two say the
260/// same thing.
261///
262/// The target is a name rather than an index into anything above, because the two output paths
263/// find it in different places: a listing hands the name to an assembler that resolves it, and a
264/// writer looks it up among the symbols it has already added.
265#[derive(Debug, Clone, PartialEq, Eq)]
266pub struct Alias {
267    /// The name being defined, as the C program spelled it.
268    pub name: String,
269    /// The name it stands for, which has to be something this same file defines.
270    pub target: String,
271    /// How the linker sees the new name, which is not always how it sees the old one: the target
272    /// of `extern int b __attribute__((alias("a")))` may be a `static`.
273    pub binding: Binding,
274    /// How far outside a shared library holding this the new name reaches, which is its own
275    /// answer for the same reason the binding is: the attribute is written on the alias.
276    pub visibility: Visibility,
277}
278
279/// One global variable, laid out.
280#[derive(Debug, Clone, PartialEq, Eq)]
281pub struct Object {
282    /// Its name, as the C program spelled it. The underscore an Apple symbol carries is the
283    /// object writer's business, not this one's.
284    pub name: String,
285    /// Its image, and nothing at all when it is zero filled and the file carries none of it.
286    pub bytes: Vec<u8>,
287    /// How many bytes it occupies, which is the length of the image except when there is none.
288    pub size: u64,
289    /// What it has to be aligned to, always a power of two.
290    pub align: u64,
291    /// Which section it goes in.
292    pub place: Place,
293    /// How the linker sees the name.
294    pub binding: Binding,
295    /// How far outside a shared library holding this the name reaches.
296    pub visibility: Visibility,
297    /// Every place in its image that holds the address of a symbol, counted from the start of
298    /// the image rather than from the start of the section it lands in.
299    pub relocs: Vec<Reloc>,
300}
301
302/// Which section a variable goes in.
303///
304/// Worked out from what the variable is rather than named by it, except in the one case where the
305/// program named it. A reader who wants to know why a variable is in `.rodata` should be able to
306/// find the answer in the variable.
307#[derive(Debug, Clone, PartialEq, Eq)]
308pub enum Place {
309    /// Written to, and its image is not all zeros. `.data`.
310    Written,
311    /// Never written to, so it can go in a page the loader maps read only and every process
312    /// running the program can share. `.rodata`.
313    ReadOnly,
314    /// Never written to by the program, but written once by the dynamic linker, because its image
315    /// holds the address of something and an address is not known until the image is loaded.
316    /// `.data.rel.ro`.
317    ///
318    /// The section has to be writable for that one write and read only afterwards, which is what
319    /// the `PT_GNU_RELRO` segment is: the loader maps it, the relocations are applied, and then it
320    /// is turned read only before the program starts. Putting the variable in `.rodata` instead
321    /// means asking the linker to leave a relocation in a section that is never writable, and what
322    /// it does about that is give the whole image `DT_TEXTREL`, which gives up the protection the
323    /// section was for. Some hardened toolchains refuse the link outright.
324    RelocReadOnly {
325        /// Whether every address in the image is of something this file defines and does not
326        /// export, which means the link can resolve them all and none can be interposed.
327        ///
328        /// Those go in `.data.rel.ro.local`, which the linker puts in the first pages of the
329        /// segment, so the pages holding them are the ones the loader is done with soonest. It is
330        /// a hint about layout rather than a difference in what the section is.
331        local: bool,
332    },
333    /// All zeros, so the file says how big it is and carries none of it. `.bss`.
334    Zero,
335    /// One copy per thread rather than one copy per program. `.tdata` and `.tbss`.
336    ///
337    /// What the loader does with these two sections is what makes them different from every other
338    /// section here. Their contents are the template of a thread's own block of storage rather than
339    /// the storage itself: the image is laid out once, and every thread that starts gets a fresh
340    /// copy of it, so the address of a variable in one of them is a different address in every
341    /// thread and there is no single address for the link to write down. That is why a reference to
342    /// one is not the ordinary distance from the instruction pointer, and why the symbol is marked
343    /// as being of this kind so a linker refuses one that is.
344    ///
345    /// The pair is the same split as `.data` and `.bss` for the same reason, so an image that is
346    /// all zeros costs its size in the file and not its bytes.
347    Thread {
348        /// Whether the image is all zeros, which puts it in `.tbss` rather than `.tdata`.
349        zero: bool,
350    },
351    /// A tentative definition, which is not in a section at all: the linker is asked for that
352    /// much zeroed space and merges every definition of the name into one. `.comm`.
353    Merged,
354    /// The section the program named, from `__attribute__((section(...)))`.
355    Named(String),
356}
357
358impl Place {
359    /// What the section this variable goes in is called under [`Sections::data`], and nothing at
360    /// all for a variable that has no section of its own to be given.
361    ///
362    /// The name is the section it would otherwise have shared with a dot and the variable's name
363    /// after it, which is what gcc writes and is not merely a convention: `--gc-sections`, the
364    /// linker scripts a kernel and an embedded image are linked with, and the default placement
365    /// rules all match on the part in front of the dot, so a section called anything else would be
366    /// placed by whatever the catch all rule is.
367    ///
368    /// Two kinds of variable are left alone. A merged one is a request to the linker for that much
369    /// zeroed space rather than an image, so there is no section to split, and one the program put
370    /// a name on already has the answer the source gave, which this must not overrule.
371    ///
372    /// Here rather than beside either output path, so that the listing `-S` writes and the object
373    /// `-c` writes cannot come to disagree about where a variable went.
374    #[must_use]
375    pub fn split(&self, name: &str) -> Option<String> {
376        Some(format!("{}.{name}", self.base()?))
377    }
378
379    /// The section this variable goes in when nothing is being split up, and nothing at all for
380    /// the two kinds that are not in one.
381    #[must_use]
382    pub fn base(&self) -> Option<&'static str> {
383        Some(match self {
384            Place::Written => ".data",
385            Place::ReadOnly => ".rodata",
386            Place::RelocReadOnly { local: false } => ".data.rel.ro",
387            Place::RelocReadOnly { local: true } => ".data.rel.ro.local",
388            Place::Zero => ".bss",
389            Place::Thread { zero: false } => ".tdata",
390            Place::Thread { zero: true } => ".tbss",
391            Place::Merged | Place::Named(_) => return None,
392        })
393    }
394}
395
396/// A section holding function addresses for a C runtime to call rather than data for the program
397/// to read.
398///
399/// ELF has a type for each of the three, and a section of that type is what the startup code walks:
400/// the linker gathers every input section of the kind into one run and the CRT calls what it finds
401/// between the two ends. A section of the ordinary type with the same name would be gathered the
402/// same way and called by nothing, which is why the type is worth writing down rather than leaving
403/// to the default.
404///
405/// Only ELF says it this way. COFF sorts by what follows the `$` in a section name and Mach-O has
406/// a section attribute for it, so on those two the name carries the whole of the answer and there
407/// is nothing for this to be.
408#[derive(Debug, Clone, Copy, PartialEq, Eq)]
409pub enum Array {
410    /// Run on the way to `main`, in the order the linker sorted the sections into.
411    Init,
412    /// Run after `main` returns, in the reverse of that order.
413    Fini,
414    /// Run ahead of `.init_array` and ahead of the shared libraries a program is linked against,
415    /// which is a thing only the C library itself has a use for.
416    Preinit,
417}
418
419impl Array {
420    /// Which of them a section of this name is, and [`None`] for a name that is not one of them.
421    ///
422    /// The name itself or the name with a dot and a priority after it. A numbered `constructor` is
423    /// written as the second of those and is the same kind of section as the first: the number is
424    /// there so that the linker sorts it, not to make it a different thing.
425    #[must_use]
426    pub fn of(name: &str) -> Option<Array> {
427        let kinds = [
428            (".init_array", Array::Init),
429            (".fini_array", Array::Fini),
430            (".preinit_array", Array::Preinit),
431        ];
432        kinds.into_iter().find_map(|(base, array)| {
433            let rest = name.strip_prefix(base)?;
434            (rest.is_empty() || rest.starts_with('.')).then_some(array)
435        })
436    }
437
438    /// How the type is spelled in a `.section` directive.
439    #[must_use]
440    pub const fn asm(self) -> &'static str {
441        match self {
442            Array::Init => "@init_array",
443            Array::Fini => "@fini_array",
444            Array::Preinit => "@preinit_array",
445        }
446    }
447}
448
449/// How the linker sees a name.
450///
451/// Three of the five linkages the IR has, because that is how many an object file can say. Which
452/// of the two weak ones a symbol had is a fact the optimizer needs and the linker does not.
453#[derive(Debug, Clone, Copy, PartialEq, Eq)]
454pub enum Binding {
455    /// Visible to every other object, and the definition here is the definition.
456    Global,
457    /// Invisible outside this object, which is what `static` at file scope means.
458    Local,
459    /// Visible, and allowed to lose to a definition in another object.
460    Weak,
461}
462
463/// How far outside a shared library a name reaches.
464///
465/// A different question from [`Binding`] and asked of a different linker. The binding is what the
466/// static linker does with a name while it is building the output, and this is what the dynamic
467/// linker may do with it once the output is a shared library and is being loaded. A hidden name is
468/// still global to the static link, so two files in the same library can call each other by it; it
469/// is simply not in the dynamic symbol table afterwards, so nothing outside can name it.
470///
471/// Written down here as its own thing rather than folded into the binding because it is the
472/// mistake tamnd/rucc#733 was: a writer that has one word for both ends up saying something about
473/// visibility while it thinks it is saying something about linkage, and what it said was hidden.
474///
475/// It means nothing for a [`Binding::Local`] name. `static` is already invisible to the whole
476/// world outside the file, and ELF records `STV_DEFAULT` for one, which is what gcc writes.
477#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
478pub enum Visibility {
479    /// In the dynamic symbol table, and a reference from inside the library may be satisfied by a
480    /// definition somewhere else, which is what makes `LD_PRELOAD` work. What a name gets when
481    /// nothing said otherwise.
482    #[default]
483    Default,
484    /// Not in the dynamic symbol table at all, so nothing outside the library can name it and
485    /// every reference to it from inside binds here. `__attribute__((visibility("hidden")))`.
486    Hidden,
487    /// In the dynamic symbol table, so something outside can name it, but a reference from inside
488    /// the library binds to the definition inside it and cannot be interposed.
489    Protected,
490}
491
492/// One reference to something this file does not contain.
493#[derive(Debug, Clone, PartialEq, Eq)]
494pub struct Reloc {
495    /// Where the bytes the linker writes over begin.
496    pub at: usize,
497    /// What is wanted, as the C program spelled it.
498    pub symbol: String,
499    /// What the linker is being asked for.
500    pub kind: Reference,
501    /// What to add to the distance, which is the constant the instruction already meant plus the
502    /// bytes between the hole and the end of the instruction, negated. An instruction counts from
503    /// where it ends and a relocation counts from where it starts, and this is the difference.
504    pub addend: i64,
505    /// How many bytes of the instruction come after the four the linker writes over, which is zero
506    /// for everything except an instruction carrying an immediate behind its displacement.
507    ///
508    /// Already inside [`Self::addend`] and written down again because the two formats disagree about
509    /// which of the two numbers they want. ELF takes the one number and counts from where the hole
510    /// starts, so the difference between that and where the instruction ends is the writer's to fold
511    /// in and nothing after it ever has to be told apart again. COFF counts from where the
512    /// instruction ends and says how far that is in the relocation type itself, which is what
513    /// `IMAGE_REL_AMD64_REL32_1` through `REL32_5` are, so it needs the two apart. A writer cannot
514    /// recover one from the other, since a displacement of minus four and no trailing bytes and a
515    /// displacement of zero and four of them are the same sum.
516    ///
517    /// Zero for a relocation in an image, where there is no instruction and the question does not
518    /// arise.
519    pub after: u8,
520}
521
522/// What kind of thing a relocation is asking the linker for.
523///
524/// The first four are the distance from the end of an instruction to something, which is what every
525/// reference the code makes is, because this compiler generates position independent code and
526/// nothing else. They are told apart by what the linker is allowed to do about each one. The last
527/// two are not distances from an instruction at all and are what a table of data asks for: the
528/// address itself, which is what an initializer holding the address of something holds, and how far
529/// something is from the front of the image, which is what a table the runtime reads holds.
530#[derive(Debug, Clone, Copy, PartialEq, Eq)]
531pub enum Reference {
532    /// A call, which the linker may satisfy with a stub that reaches further than the four bytes
533    /// would. `R_X86_64_PLT32` on ELF, and the same relocation a branch gets on the other two.
534    Call,
535    /// A datum, reached from the instruction pointer. `R_X86_64_PC32` on ELF.
536    Data,
537    /// A slot of the global offset table, reached from the instruction pointer, holding the
538    /// address of something another object may be the one that defines.
539    ///
540    /// The distance to the slot rather than to the thing, which is the whole difference: the
541    /// distance to the thing is a number only a link that puts the thing in this program can
542    /// work out, and a shared library is a link that does not. `R_X86_64_REX_GOTPCRELX` on ELF,
543    /// which says the instruction is a `mov` with a REX prefix and lets the linker turn it back
544    /// into the `lea` it would have been if the symbol had been here all along.
545    Got,
546    /// A slot of the global offset table, reached from the instruction pointer, holding how far
547    /// into a thread's own block of storage a thread-local variable sits.
548    ///
549    /// An offset and not an address, which is what makes it a different relocation from the one
550    /// above rather than the same one against a different symbol: a thread-local variable has one
551    /// copy per thread and therefore no address for a link to write down, and what every copy has
552    /// in common is where it sits inside the block. Adding the block's own address, which the
553    /// machine keeps in a segment register, is what turns one into the other, and that addition is
554    /// in the code rather than in the relocation. `R_X86_64_GOTTPOFF` on ELF, which the linker
555    /// turns into a constant in the instruction when it is making an executable and therefore
556    /// knows how the blocks are laid out.
557    Thread,
558    /// The address itself, written into an image. `int *p = &y;` and nothing else in C.
559    Address {
560        /// How many bytes of it are written, which is the pointer width except on a target with
561        /// a narrower relocation for it. `R_X86_64_64` and `R_X86_64_32` on ELF.
562        bytes: u8,
563    },
564    /// How far the thing is from the front of the loaded image, written into four bytes.
565    ///
566    /// What every field of a Windows unwind table is. The table is read at run time by code that
567    /// already has the image's own address, so four bytes of distance from it reach anything in an
568    /// image a linker will build, which eight bytes of address would have cost twice as much to say
569    /// and a distance from the table itself could not have said at all: a row is looked up by
570    /// address in a sorted table, and a row whose meaning depended on where the row was would not
571    /// sort. `IMAGE_REL_AMD64_ADDR32NB`.
572    ///
573    /// ELF has no relocation of this kind because nothing it writes asks the question. Its unwind
574    /// records are found by walking rather than by binary search, and what they hold is the ordinary
575    /// distance from the record to the function.
576    Image,
577}