Skip to main content

rucc_object/
file.rs

1//! Relocatable objects, in whichever of the formats the target wants.
2//!
3//! Design: `spec/11-asm-objects-debug.md` section 11.3, which says the three formats are written
4//! through the [`object`] crate's writer with our own layer above it for the parts it does not
5//! model. This is that layer, and what it holds is the part `object` cannot decide: which
6//! relocation an instruction wants, what a symbol's binding and type are, and the sections a
7//! linker expects to find whether or not anything was put in them.
8//!
9//! # One layout and two sets of answers
10//!
11//! Which sections a file has, what goes in each of them, which symbol says where each thing is and
12//! what each relocation is against are the same questions for ELF and for COFF, and they have the
13//! same answers, so they are asked once here. What differs is a short list: the number a relocation
14//! is, the field a visibility goes in, the note saying what the file was built to have checked, and
15//! the marker whose absence makes the stack executable. [`Flavour`] is that list, and the answers
16//! are in [`crate::elf`] and [`crate::coff`] beside each other where they can be read against one
17//! another.
18//!
19//! The alternative was two writers, and the reason against it is what a second copy of a layout
20//! decays into: a fix to one of them is a fix to one platform, and which platform got it is
21//! whichever the person who found the bug was building for.
22//!
23//! # What is not here
24//!
25//! Mach-O. The formats disagree about more than their headers: an Apple symbol carries an
26//! underscore in front of the C name and Mach-O has no way to say how long a function is, wanting
27//! `.subsections_via_symbols` instead. It is written when the target that needs it is.
28//!
29//! Thread-local storage. Reaching a thread-local variable is a different instruction sequence per
30//! model and the back end writes none of them, so a module carrying one is refused before it
31//! reaches here rather than written as an ordinary variable in the wrong section.
32
33use std::collections::{HashMap, HashSet};
34
35use object::write::{
36    Object as Writer, Relocation, StandardSection, Symbol, SymbolId, SymbolSection,
37};
38use object::{
39    Architecture, BinaryFormat, Endianness, RelocationFlags, SectionFlags, SectionKind,
40    SymbolFlags, SymbolKind, SymbolScope,
41};
42use rucc_target::{ObjectFormat, TargetInfo};
43use rucc_tuple::Arch;
44
45use crate::section::{
46    Alias, Array, Binding, Data, Object, Output, Place, Property, Reference, Reloc, Sections, Text,
47    Visibility,
48};
49use crate::{coff, elf};
50
51/// Which of the two formats is being written, and therefore which set of answers the questions this
52/// module cannot decide get.
53///
54/// A short list rather than a trait, because the list is short and closed: everything a format has
55/// an opinion about is a call to one of the methods below, so adding Mach-O is adding a third arm to
56/// each of them and the compiler names every one that was forgotten.
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub(crate) enum Flavour {
59    /// Linux, the BSDs and the freestanding targets.
60    Elf,
61    /// Windows, under either of its two runtimes.
62    Coff,
63}
64
65impl Flavour {
66    /// Which one a target wants, and nothing for the two formats that are not written.
67    fn of(target: &TargetInfo) -> Option<Flavour> {
68        match target.object_format {
69            ObjectFormat::Elf => Some(Flavour::Elf),
70            ObjectFormat::Coff => Some(Flavour::Coff),
71            ObjectFormat::MachO | ObjectFormat::Wasm => None,
72        }
73    }
74
75    /// The format the writer underneath is asked for.
76    fn binary(self) -> BinaryFormat {
77        match self {
78            Flavour::Elf => BinaryFormat::Elf,
79            Flavour::Coff => BinaryFormat::Coff,
80        }
81    }
82
83    /// Which relocation this reference is, or `None` for one this format has none of.
84    ///
85    /// `after` is how many bytes of the instruction come after the four the linker writes over,
86    /// which ELF has already folded into the addend and COFF wants told apart. See [`crate::Reloc`].
87    fn reloc(self, reference: Reference, after: u8) -> Option<RelocationFlags> {
88        match self {
89            Flavour::Elf => elf::r_type(reference).map(|r_type| RelocationFlags::Elf { r_type }),
90            Flavour::Coff => coff::reloc(reference, after),
91        }
92    }
93
94    /// Say how far a name reaches beyond what its scope already said.
95    ///
96    /// Nothing on COFF, where a symbol has nowhere to keep it. A file built with
97    /// `-fvisibility=hidden` for Windows is a file where that flag changed nothing, which is what
98    /// gcc does there as well.
99    fn see(self, obj: &mut Writer<'_>, id: SymbolId, binding: Binding, visibility: Visibility) {
100        match self {
101            Flavour::Elf => elf::see(obj, id, binding, visibility),
102            Flavour::Coff => {}
103        }
104    }
105
106    /// The section a variable the loader writes into before anything reads it goes in, when the
107    /// program asked for the half of it the linker keeps apart, or nothing for a format that has no
108    /// such half and puts one in ordinary read only data with the rest.
109    fn rel_ro_local(self) -> Option<&'static str> {
110        match self {
111            Flavour::Elf => elf::REL_RO_LOCAL,
112            Flavour::Coff => coff::REL_RO_LOCAL,
113        }
114    }
115
116    /// The type and flags a section of function addresses the startup code calls has, where the
117    /// format has something to say about it.
118    ///
119    /// Nothing on COFF, where such a section is refused by [`beyond`] before it reaches here rather
120    /// than written under a name nothing on that platform gathers.
121    fn gathered(self, array: Array) -> Option<SectionFlags> {
122        match self {
123            Flavour::Elf => Some(elf::gathered(array)),
124            Flavour::Coff => None,
125        }
126    }
127
128    /// The marker a linker looks for in every input, where there is one.
129    fn marker(self, obj: &mut Writer<'_>) {
130        match self {
131            Flavour::Elf => elf::marker(obj),
132            Flavour::Coff => coff::marker(obj),
133        }
134    }
135
136    /// What the file says it was built to have checked, where the format has a way to say it.
137    ///
138    /// ELF writes a note the linker keeps only the agreed part of. A PE image says the same thing in
139    /// the header of the finished image rather than in its inputs, so an object carries nothing and
140    /// the instructions the flag asked for are in the text either way.
141    fn property(self, obj: &mut Writer<'_>, property: Property) {
142        if !property.any() {
143            return;
144        }
145        match self {
146            Flavour::Elf => {
147                let note = obj.section_id(StandardSection::GnuProperty);
148                obj.append_section_data(note, &elf::record(property), 8);
149            }
150            Flavour::Coff => {}
151        }
152    }
153
154    /// Where the unwind table goes: the section the records are in and what it is aligned to, and
155    /// the second section holding what those records point at, on the format that keeps the two
156    /// apart.
157    fn tables(self) -> ((&'static str, u64), Option<(&'static str, u64)>) {
158        match self {
159            Flavour::Elf => (elf::FRAMES, None),
160            Flavour::Coff => (coff::FUNCTIONS, Some(coff::CODES)),
161        }
162    }
163
164    /// Anything that has to be written into the finished bytes rather than said to the writer.
165    fn finish(self, bytes: &mut [u8], ordered: &[String]) {
166        match self {
167            Flavour::Elf => elf::link(bytes, ordered),
168            Flavour::Coff => debug_assert!(ordered.is_empty(), "a record this format cannot write"),
169        }
170    }
171}
172
173/// Why an object file could not be written.
174#[derive(Debug, Clone, PartialEq, Eq)]
175pub enum Error {
176    /// A machine or a platform this does not write objects for.
177    Format {
178        /// The triple that was asked for.
179        triple: String,
180    },
181    /// The writer refused something it was given, which is a bug here rather than in a program.
182    Refused {
183        /// What it said, already formatted.
184        why: String,
185    },
186}
187
188impl std::fmt::Display for Error {
189    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
190        match self {
191            Error::Format { triple } => {
192                write!(f, "there is no object writer for {triple} in this compiler yet")
193            }
194            Error::Refused { why } => {
195                write!(f, "the object writer refused what it was given: {why}")
196            }
197        }
198    }
199}
200
201impl std::error::Error for Error {}
202
203/// One text section and the variables beside it, as a relocatable object in the target's format.
204///
205/// # Errors
206///
207/// [`Error::Format`] for a machine or a platform this does not write, and [`Error::Refused`] for
208/// anything the writer underneath objected to, which would be a bug here. An alias whose target
209/// this file does not define is refused the same way, since the front end is what reports that as
210/// a program's mistake and one reaching here means it did not. So is anything the target's format
211/// has no way to write, which for COFF is a thread-local variable, a reference through a table the
212/// platform does not have, a record of where a patcher's room is and a section the startup code is
213/// expected to gather. See [`Error`].
214pub fn write(
215    text: &Text,
216    data: &Data,
217    aliases: &[Alias],
218    target: &TargetInfo,
219    output: Output,
220) -> Result<Vec<u8>, Error> {
221    let Output { sections, property } = output;
222    let flavour = Flavour::of(target).filter(|_| target.tuple.arch() == Arch::X86_64);
223    let Some(flavour) = flavour else {
224        return Err(Error::Format { triple: target.tuple.to_string() });
225    };
226    if flavour == Flavour::Coff {
227        beyond(text, data)?;
228    }
229    let mut obj = Writer::new(flavour.binary(), Architecture::X86_64, Endianness::Little);
230    // The one that holds every function when they are not being split up. Asked for even when it
231    // will stay empty, because it is the section the writer underneath starts a file with anyway
232    // and gcc writes an empty `.text` under `-ffunction-sections` too.
233    let whole = obj.section_id(StandardSection::Text);
234    if !sections.functions {
235        obj.append_section_data(whole, &text.bytes, u64::from(text.align));
236    }
237
238    // Every function defined here, then every variable, then every name either of them wanted that
239    // is not. A name is looked up rather than added twice, because two symbols with one name is
240    // not a file a linker accepts.
241    let mut symbols = std::collections::BTreeMap::new();
242    // Where each function ended up, in the order they were written, so that a relocation inside
243    // one goes into the section that one is in and one that points at the start of one can be
244    // written against that section. The same list as `text.funcs` and in the same order, so the
245    // two are walked together below.
246    let mut split: Vec<(object::write::SectionId, u64)> = Vec::with_capacity(text.funcs.len());
247    // Which text section each record of where a patcher's room is belongs to, in the order the
248    // records were added, which is the order their headers come out in. See `link`.
249    let mut ordered: Vec<String> = Vec::new();
250    for func in &text.funcs {
251        // A section of its own, holding this function's bytes and nothing else, so the linker can
252        // drop it when nothing reaches it. The name is what gcc writes, and the leading `.text.`
253        // is not decoration: `--gc-sections` and the linker scripts that place code both match on
254        // it, and a section called something else would be placed by the catch all rule.
255        //
256        // The room a patcher was promised in front of the label goes in it too. Those bytes are
257        // the function's, they are just not under its name: the symbol is where the label was and
258        // the room is what came before, so a section holding one without the other would be a
259        // section a linker could place with the room missing.
260        let ahead = func.patch.map_or(0, |patch| patch.before);
261        let (section, at) = if sections.functions {
262            let name = format!(".text.{}", func.name).into_bytes();
263            let id = obj.add_section(Vec::new(), name, SectionKind::Text);
264            let bytes = &text.bytes[func.start - ahead..func.start + func.len];
265            obj.append_section_data(id, bytes, u64::from(func.align.max(1)));
266            (id, ahead as u64)
267        } else {
268            (whole, func.start as u64)
269        };
270        // Where the room is, in a section of its own that says nothing else. What reads it is a
271        // tracer patching every function in an image at once, and what it needs is every address
272        // in one place: a stripped kernel has no symbol table to walk instead, which is the whole
273        // reason the list is written rather than worked out later.
274        //
275        // The address is a relocation rather than a number, because a function is at a fixed
276        // offset in its own section and where that section lands is the linker's answer. It is
277        // written against the section rather than against the function's own name so that it still
278        // points at the room when the room is in front of the name.
279        //
280        // One section per function even when they all point at the same text, which is what gas
281        // produces and what lets a linker throw the record away with the function. `SHF_LINK_ORDER`
282        // is what ties the two together and it needs a section index the writer underneath does not
283        // set, so `link` fills it in afterwards. See `link`.
284        if let Some(patch) = func.patch {
285            let base = if sections.functions { func.start - ahead } else { 0 };
286            let name = elf::PATCHABLE.as_bytes().to_vec();
287            let id = obj.add_section(Vec::new(), name, SectionKind::Data);
288            obj.section_mut(id).flags = elf::ordered();
289            obj.append_section_data(id, &[0; 8], 8);
290            let symbol = obj.section_symbol(section);
291            let flags = flavour.reloc(Reference::Address { bytes: 8 }, 0).ok_or_else(|| {
292                Error::Refused { why: "no relocation holds an address here".to_owned() }
293            })?;
294            obj.add_relocation(
295                id,
296                Relocation { offset: 0, symbol, addend: (patch.at - base) as i64, flags },
297            )
298            .map_err(|why| Error::Refused { why: why.to_string() })?;
299            ordered.push(if sections.functions {
300                format!(".text.{}", func.name)
301            } else {
302                ".text".to_owned()
303            });
304        }
305        let id = obj.add_symbol(Symbol {
306            name: func.name.clone().into_bytes(),
307            value: at,
308            size: func.len as u64,
309            kind: SymbolKind::Text,
310            scope: scope_of(func.binding),
311            weak: func.binding == Binding::Weak,
312            section: SymbolSection::Section(section),
313            flags: SymbolFlags::None,
314        });
315        flavour.see(&mut obj, id, func.binding, func.visibility);
316        symbols.insert(func.name.clone(), id);
317        split.push((section, at));
318    }
319
320    // The places inside a function that have names of their own, which is where a label whose
321    // address an image holds is. After the functions, because the section one goes in is the
322    // section of the function it is inside and that is what the walk above worked out.
323    for label in &text.labels {
324        let after = text.funcs.partition_point(|func| func.start <= label.at);
325        let Some(index) = after.checked_sub(1) else {
326            let why = format!("'{}' is at {} and in front of every function", label.name, label.at);
327            return Err(Error::Refused { why });
328        };
329        let func = &text.funcs[index];
330        let (section, at) = if sections.functions {
331            // From the start of the section rather than from the symbol, which is the same
332            // correction a relocation inside a function gets below.
333            let base = func.start - func.patch.map_or(0, |patch| patch.before);
334            (split[index].0, (label.at - base) as u64)
335        } else {
336            (whole, label.at as u64)
337        };
338        let id = obj.add_symbol(Symbol {
339            name: label.name.clone().into_bytes(),
340            value: at,
341            // A label has no length. What is at it is the rest of the function, and a size here
342            // would be a claim that the bytes after it are a thing of their own.
343            size: 0,
344            kind: SymbolKind::Label,
345            // Never offered to another file. The name is one the compiler minted and what it
346            // points at is the middle of a function, so the only thing that resolves against it
347            // is the image in this same file that asked for it.
348            scope: SymbolScope::Compilation,
349            weak: false,
350            section: SymbolSection::Section(section),
351            flags: SymbolFlags::None,
352        });
353        symbols.insert(label.name.clone(), id);
354    }
355
356    // Where each variable's image landed in the section it went into, kept because a relocation in
357    // an image counts from the start of the image and one in a file counts from the start of the
358    // section. A variable that is not in a section has no entry, since nothing in a merged one can
359    // hold a relocation: the linker is being asked for zeroed space rather than for an image.
360    let mut placed = Vec::with_capacity(data.objects.len());
361    // The sections the writer has no name of its own for, remembered by name so that every variable
362    // that wants one lands in the same one. The rest come back from `section_id`, which already
363    // answers with the section it made the first time it was asked.
364    let mut named = HashMap::new();
365    for object in &data.objects {
366        let (section, offset) = put(&mut obj, object, &mut named, sections, flavour);
367        let id = obj.add_symbol(Symbol {
368            name: object.name.clone().into_bytes(),
369            // A common symbol says what it wants rather than where it is, and what it wants is
370            // recorded where an ordinary symbol records its address.
371            value: if object.place == Place::Merged { object.align } else { offset },
372            size: object.size,
373            // A thread-local variable is a different kind of symbol rather than a symbol in a
374            // different section, and it has to be both: the kind is what a linker checks a
375            // relocation against, so a `R_X86_64_PC32` aimed at one is refused rather than
376            // resolved to an address that would have been one thread's and is nobody's.
377            kind: match object.place {
378                Place::Thread { .. } => SymbolKind::Tls,
379                _ => SymbolKind::Data,
380            },
381            scope: scope_of(object.binding),
382            weak: object.binding == Binding::Weak,
383            section,
384            flags: SymbolFlags::None,
385        });
386        flavour.see(&mut obj, id, object.binding, object.visibility);
387        symbols.insert(object.name.clone(), id);
388        placed.push((section.id(), offset));
389    }
390
391    // A second name for something already added, which is where the alias's own binding is the
392    // only thing it does not take from what it points at: the target of one may be a `static` and
393    // the alias of it may not be. Before the loop below rather than after it, because a reference
394    // to the new name is a reference to something this file defines and would otherwise be added
395    // as a name this file wants from somewhere else.
396    for alias in aliases {
397        let Some(&id) = symbols.get(&alias.target) else {
398            let why =
399                format!("'{}' is aliased to '{}', which is not here", alias.name, alias.target);
400            return Err(Error::Refused { why });
401        };
402        let (value, size) = (obj.symbol(id).value, obj.symbol(id).size);
403        let (kind, section) = (obj.symbol(id).kind, obj.symbol(id).section);
404        let id = obj.add_symbol(Symbol {
405            name: alias.name.clone().into_bytes(),
406            value,
407            size,
408            kind,
409            scope: scope_of(alias.binding),
410            weak: alias.binding == Binding::Weak,
411            section,
412            flags: SymbolFlags::None,
413        });
414        flavour.see(&mut obj, id, alias.binding, alias.visibility);
415        symbols.insert(alias.name.clone(), id);
416    }
417
418    // Not the unwind table's, which name functions this file defines and are written against the
419    // section rather than against the name. A record for anything else is refused below, so a name
420    // added here for one would be a name nothing goes on to use.
421    // The names a declaration wrote `weak` on, which the link is allowed to leave undefined and
422    // whose references then read a zero address. The listing writes a `.weak` for each of the same
423    // names, so the two paths put the same entries in whether or not anything refers to one.
424    let weak: HashSet<&str> = data.weak.iter().map(String::as_str).collect();
425    let relocs = || text.relocs.iter().chain(data.objects.iter().flat_map(|o| &o.relocs));
426    // The names something here reaches through the thread pointer, which is the one thing about an
427    // undefined name this file does know. A reference to a thread-local variable is a different kind
428    // of reference from a reference to an ordinary one and the code that makes it is already
429    // different, so the file has been told, and ELF wants the symbol to say so as well.
430    let thread: HashSet<&str> = relocs()
431        .filter(|reloc| reloc.kind == Reference::Thread)
432        .map(|reloc| reloc.symbol.as_str())
433        .collect();
434    let wanted: Vec<&String> =
435        relocs().map(|reloc| &reloc.symbol).chain(data.weak.iter()).collect();
436    for name in wanted {
437        if symbols.contains_key(name) {
438            continue;
439        }
440        let id = obj.add_symbol(Symbol {
441            name: name.clone().into_bytes(),
442            value: 0,
443            size: 0,
444            // What kind of thing an undefined name is is not known here and does not have to be:
445            // a linker resolves an undefined symbol by its name, and the type of one that is not
446            // defined anywhere in this file is nothing this file can say. A thread-local one is the
447            // exception, and the linker makes it one. A reference to a thread-local variable is
448            // satisfied by an offset into a block rather than by an address, so the linker has to
449            // know which of the two it is being asked for before it has found the definition, and it
450            // refuses a link where one file says `STT_TLS` and another does not rather than picking
451            // one. That is tamnd/rucc#1461: libmpfr writes `__gmpfr_flags` in one file and reads it
452            // in a hundred others, and `ld` stopped at the first reader with a mismatch.
453            kind: if thread.contains(name.as_str()) {
454                SymbolKind::Tls
455            } else {
456                SymbolKind::Unknown
457            },
458            scope: SymbolScope::Dynamic,
459            weak: weak.contains(name.as_str()),
460            section: SymbolSection::Undefined,
461            flags: SymbolFlags::None,
462        });
463        symbols.insert(name.clone(), id);
464    }
465
466    for reloc in &text.relocs {
467        // Which function's bytes this one is in, which is the question only the split path has to
468        // ask: when there is one text section every offset in it is already the offset in it.
469        // Every relocation is inside some function, since the padding between two of them is
470        // instructions that do nothing and holds nothing a linker fills in.
471        let (section, at) = if sections.functions {
472            let after = text.funcs.partition_point(|func| func.start <= reloc.at);
473            let Some(func) = after.checked_sub(1).map(|i| &text.funcs[i]) else {
474                let why = format!("a relocation at {} is in front of every function", reloc.at);
475                return Err(Error::Refused { why });
476            };
477            // From the start of the section rather than from the symbol, and the two are not the
478            // same byte in a function with room in front of its label.
479            let base = func.start - func.patch.map_or(0, |patch| patch.before);
480            (split[after - 1].0, (reloc.at - base) as u64)
481        } else {
482            (whole, reloc.at as u64)
483        };
484        add(&mut obj, section, at, reloc, &symbols, flavour)?;
485    }
486
487    // The unwind table, if there is one. Its own section rather than part of the text, because it
488    // is read rather than run: the loader maps it and the linker gathers every input's into one
489    // table and builds the index the unwinder searches.
490    if !text.unwind.bytes.is_empty() {
491        let ((name, align), second) = flavour.tables();
492        let frames = obj.add_section(Vec::new(), name.into(), SectionKind::ReadOnlyData);
493        obj.append_section_data(frames, &text.unwind.bytes, align);
494        // What the rows point at, on the format that keeps the descriptions in a section of their
495        // own, and a name for each of them, because a row reaches one through a relocation and a
496        // relocation names a symbol. The names are never offered to another file: what they point
497        // at is one function's prologue, described for the runtime of this program and nothing else.
498        let mut described = HashMap::new();
499        if !text.unwind.info.is_empty() {
500            let Some((name, align)) = second else {
501                let why = "an unwind table here is one section and it was given two".to_owned();
502                return Err(Error::Refused { why });
503            };
504            let codes = obj.add_section(Vec::new(), name.into(), SectionKind::ReadOnlyData);
505            obj.append_section_data(codes, &text.unwind.info, align);
506            for label in &text.unwind.labels {
507                let id = obj.add_symbol(Symbol {
508                    name: label.name.clone().into_bytes(),
509                    value: label.at as u64,
510                    size: 0,
511                    kind: SymbolKind::Label,
512                    scope: SymbolScope::Compilation,
513                    weak: false,
514                    section: SymbolSection::Section(codes),
515                    flags: SymbolFlags::None,
516                });
517                described.insert(label.name.clone(), id);
518            }
519        }
520        for reloc in &text.unwind.relocs {
521            let (symbol, addend) = match described.get(&reloc.symbol) {
522                // A description in the section above, reached by its own name and needing no
523                // correction, since the name is at the description rather than at the front of the
524                // section it is in.
525                Some(&id) => (id, reloc.addend),
526                // A function, and against the section it is in rather than against its own name,
527                // which is the same reason the record of a patcher's room is written that way and
528                // one more besides. The section is the only one of the two that is settled here: a
529                // global name is answered at load time by whichever object defines it first, so a
530                // distance measured to one is not a distance the linker can work out, and it says
531                // so and stops. The effect was that nothing this compiler wrote could go into a
532                // shared library at all, because every function has a record and every record
533                // pointed at a name.
534                //
535                // A function defined elsewhere has no record here, so the lookup failing means the
536                // record is for something that is not a function in this file, and that is a bug
537                // rather than a shape to handle: the writer says what it was given rather than
538                // guessing.
539                None => {
540                    let found = text.funcs.iter().position(|func| func.name == reloc.symbol);
541                    let Some((section, at)) = found.map(|i| split[i]) else {
542                        let why = format!(
543                            "'{}' has an unwind record and is not a function here",
544                            reloc.symbol
545                        );
546                        return Err(Error::Refused { why });
547                    };
548                    // Where the function starts inside its section, since the section symbol is
549                    // where the section starts and the two are the same byte only for the first
550                    // function in one.
551                    (obj.section_symbol(section), reloc.addend + at as i64)
552                }
553            };
554            let flags = flavour.reloc(reloc.kind, reloc.after).ok_or_else(|| Error::Refused {
555                why: format!("no relocation is {:?}", reloc.kind),
556            })?;
557            let record = Relocation { offset: reloc.at as u64, symbol, addend, flags };
558            obj.add_relocation(frames, record)
559                .map_err(|why| Error::Refused { why: why.to_string() })?;
560        }
561    }
562    for (object, &(section, offset)) in data.objects.iter().zip(&placed) {
563        let Some(section) = section else { continue };
564        for reloc in &object.relocs {
565            add(&mut obj, section, offset + reloc.at as u64, reloc, &symbols, flavour)?;
566        }
567    }
568
569    // What the file was built to have checked, when it was built to have anything checked. Left
570    // out otherwise rather than written as a zero, because a linker treats a missing note and a
571    // note with no bits in it the same way and gcc writes nothing.
572    flavour.property(&mut obj, property);
573
574    // Written rather than left out, because a linker that does not find it in every input marks
575    // the stack executable, on the format that has one.
576    flavour.marker(&mut obj);
577
578    let mut bytes = obj.write().map_err(|why| Error::Refused { why: why.to_string() })?;
579    flavour.finish(&mut bytes, &ordered);
580    Ok(bytes)
581}
582
583/// Everything in this module the target's format has no way to write, refused by name.
584///
585/// Each of these is something ELF has and COFF does not, and each would otherwise be written as the
586/// nearest thing rather than refused, which is worse: a thread-local variable written as an ordinary
587/// one is a program where every thread shares what the source said each would have its own copy of,
588/// and a constructor list under a name the Windows runtime does not gather is a program whose
589/// constructors never run. A message naming the feature is what the caller turns into a diagnostic,
590/// and the front end refusing first is what stops one ever being seen.
591///
592/// # Errors
593///
594/// [`Error::Refused`], naming the one it found first.
595fn beyond(text: &Text, data: &Data) -> Result<(), Error> {
596    let why = |why: String| Err(Error::Refused { why });
597    if text.funcs.iter().any(|func| func.patch.is_some()) {
598        return why("a record of where a patcher's room is has no section flags here".to_owned());
599    }
600    for reloc in text.relocs.iter().chain(data.objects.iter().flat_map(|object| &object.relocs)) {
601        if matches!(reloc.kind, Reference::Got | Reference::Thread) {
602            return why(format!("nothing reaches '{}' through a table here", reloc.symbol));
603        }
604    }
605    for object in &data.objects {
606        if matches!(object.place, Place::Thread { .. }) {
607            return why(format!("'{}' is thread-local and this format is not", object.name));
608        }
609        let Place::Named(name) = &object.place else { continue };
610        if Array::of(name).is_some() {
611            return why(format!("'{name}' is not a list the startup code here gathers"));
612        }
613    }
614    Ok(())
615}
616
617/// Every name a linker can find in the object [`write()`] would write from the same input.
618///
619/// What asks for this is the archive writer. A static link resolves through the symbol index, so an
620/// index entry has to name a symbol the member really defines: an entry for a name that is not in
621/// the member is an archive the linker searches, pulls the member out of, and then still reports
622/// the name undefined. So the list comes from the writer rather than from the caller, because the
623/// writer is the only thing that knows what it wrote.
624///
625/// The names are as the C program spelled them, with nothing in front of them, which is what both
626/// the formats this writes have on this machine. Mach-O puts an underscore there and so does COFF on
627/// a 32-bit machine, and when either of those is written this is the function that has to say so,
628/// which is why it asks about the target it otherwise would not have to.
629///
630/// Order is the functions, then the variables, then the aliases, each in the order the module held
631/// them, which is the order [`write()`] adds the symbols in. A `static` is left out: it is a name the
632/// link has already finished with by the time an archive is searched, and an index entry for one
633/// would offer the linker a definition it is not allowed to use.
634///
635/// # Errors
636///
637/// [`Error::Format`] for a machine or a platform this does not write, which is the same refusal
638/// [`write()`] gives and is here for the same reason: a list of undecorated names for a format whose
639/// symbols carry an underscore is worse than no list at all.
640pub fn defines(
641    text: &Text,
642    data: &Data,
643    aliases: &[Alias],
644    target: &TargetInfo,
645) -> Result<Vec<String>, Error> {
646    if target.tuple.arch() != Arch::X86_64 || Flavour::of(target).is_none() {
647        return Err(Error::Format { triple: target.tuple.to_string() });
648    }
649    let names = text
650        .funcs
651        .iter()
652        .filter(|func| func.binding != Binding::Local)
653        .map(|func| func.name.clone())
654        .chain(
655            data.objects
656                .iter()
657                .filter(|object| object.binding != Binding::Local)
658                .map(|object| object.name.clone()),
659        )
660        .chain(
661            aliases
662                .iter()
663                .filter(|alias| alias.binding != Binding::Local)
664                .map(|alias| alias.name.clone()),
665        )
666        .collect();
667    Ok(names)
668}
669
670/// One variable's image into the section it belongs in, and where in that section it landed.
671///
672/// A zero filled variable takes as many bytes of the file as it is long on the way in and none on
673/// the way out, which is the whole point of the section it goes in. A merged one goes in no section
674/// at all: the linker is being asked for that much zeroed space under that name, and where it ends
675/// up is the linker's answer rather than this file's.
676fn put(
677    obj: &mut Writer<'_>,
678    object: &Object,
679    named: &mut HashMap<String, object::write::SectionId>,
680    sections: Sections,
681    flavour: Flavour,
682) -> (SymbolSection, u64) {
683    // A section of its own, named after the variable and after the section it would have gone in,
684    // which is what `-fdata-sections` asks for. A merged variable has no section to split and a
685    // named one was named by the program, so both are left where they are: the first is a request
686    // to the linker rather than an image, and the second would otherwise have the flag silently
687    // overrule what the source said.
688    if sections.data {
689        if let Some(name) = object.place.split(&object.name) {
690            let section = obj.add_section(Vec::new(), name.into_bytes(), kind_of(&object.place));
691            let offset = if carries_no_bytes(&object.place) {
692                obj.append_section_bss(section, object.size, object.align)
693            } else {
694                obj.append_section_data(section, &object.bytes, object.align)
695            };
696            return (SymbolSection::Section(section), offset);
697        }
698    }
699    let section = match &object.place {
700        Place::Written => obj.section_id(StandardSection::Data),
701        Place::ReadOnly => obj.section_id(StandardSection::ReadOnlyData),
702        // Read only after the loader has written it, which the writer knows as the relocatable
703        // read only data section and which is `.data.rel.ro` on ELF. The `.local` half is a layout
704        // hint the writer has no name for, so it is added by hand and remembered: asking again
705        // would make a second section with the same name, and a file with one of those per variable
706        // is a file whose section headers outweigh what they describe.
707        Place::RelocReadOnly { local } => match flavour.rel_ro_local().filter(|_| *local) {
708            Some(name) => made(obj, named, name, SectionKind::ReadOnlyDataWithRel),
709            None => obj.section_id(StandardSection::ReadOnlyDataWithRel),
710        },
711        Place::Zero => obj.section_id(StandardSection::UninitializedData),
712        Place::Thread { zero: false } => obj.section_id(StandardSection::Tls),
713        Place::Thread { zero: true } => obj.section_id(StandardSection::UninitializedTls),
714        Place::Merged => return (SymbolSection::Common, 0),
715        // A named section is the program's word for where this goes, and a program that names one
716        // wants what it named rather than what would have been chosen. It is written as ordinary
717        // data because nothing in the IR says otherwise, except for the three names the startup
718        // code calls what it finds in, which have a section type of their own and are gathered by
719        // the linker whether or not they carry it.
720        Place::Named(name) => {
721            let section = made(obj, named, name, SectionKind::Data);
722            if let Some(flags) = Array::of(name).and_then(|array| flavour.gathered(array)) {
723                obj.section_mut(section).flags = flags;
724            }
725            section
726        }
727    };
728    let offset = if carries_no_bytes(&object.place) {
729        obj.append_section_bss(section, object.size, object.align)
730    } else {
731        obj.append_section_data(section, &object.bytes, object.align)
732    };
733    (SymbolSection::Section(section), offset)
734}
735
736/// Whether the section this goes in says how big the variable is and holds none of its bytes.
737///
738/// Two of them, and they are the same answer twice: `.bss` is the image that is all zeros, and
739/// `.tbss` is a thread's own copy of one. A section like this costs its size in the section header
740/// and nothing in the file, which is what keeps a program with a large zeroed array small.
741fn carries_no_bytes(place: &Place) -> bool {
742    matches!(place, Place::Zero | Place::Thread { zero: true })
743}
744
745/// The section of this name, made the first time it is asked for and found afterwards.
746///
747/// Two variables the program put the same section name on belong in one section, the way two in
748/// `.data` do. Asking the writer for a new one each time would make a second header with the same
749/// name, which a linker takes and which makes a file with ten constructors in it carry ten section
750/// headers describing eight bytes each. `section_id` does this already for the sections it has
751/// names of its own for, and this is the same answer for the ones it does not.
752fn made(
753    obj: &mut Writer<'_>,
754    named: &mut HashMap<String, object::write::SectionId>,
755    name: &str,
756    kind: SectionKind,
757) -> object::write::SectionId {
758    if let Some(section) = named.get(name) {
759        return *section;
760    }
761    let section = obj.add_section(Vec::new(), name.as_bytes().to_vec(), kind);
762    named.insert(name.to_owned(), section);
763    section
764}
765
766/// What a section split off for one variable is, which is what the section it was split off from
767/// was.
768///
769/// Splitting changes the name and nothing else. A variable that was going to be in a page the
770/// loader maps read only is still in one, and a zero filled variable still costs the file nothing,
771/// so the flags a linker reads off the section header have to come out the same as they would
772/// have. The two kinds with no section of their own never reach here, and `Data` for them is a
773/// value that is never used rather than a claim about either.
774fn kind_of(place: &Place) -> SectionKind {
775    match place {
776        Place::ReadOnly => SectionKind::ReadOnlyData,
777        Place::RelocReadOnly { .. } => SectionKind::ReadOnlyDataWithRel,
778        Place::Zero => SectionKind::UninitializedData,
779        Place::Thread { zero: false } => SectionKind::Tls,
780        Place::Thread { zero: true } => SectionKind::UninitializedTls,
781        Place::Written | Place::Merged | Place::Named(_) => SectionKind::Data,
782    }
783}
784
785/// One relocation, `at` bytes into the section it ended up in.
786///
787/// The offset is worked out by the caller rather than here, because the two callers count from
788/// different places: a relocation in an image counts from the start of that image and a relocation
789/// in a function counts from the start of that function, and neither of those is where the section
790/// begins once something else is in front of it.
791fn add(
792    obj: &mut Writer<'_>,
793    section: object::write::SectionId,
794    at: u64,
795    reloc: &Reloc,
796    symbols: &std::collections::BTreeMap<String, SymbolId>,
797    flavour: Flavour,
798) -> Result<(), Error> {
799    let flags = flavour
800        .reloc(reloc.kind, reloc.after)
801        .ok_or_else(|| Error::Refused { why: format!("no relocation is {:?}", reloc.kind) })?;
802    obj.add_relocation(
803        section,
804        Relocation { offset: at, symbol: symbols[&reloc.symbol], addend: reloc.addend, flags },
805    )
806    .map_err(|why| Error::Refused { why: why.to_string() })
807}
808
809/// How far a name reaches, which is the one thing about a symbol ELF calls its binding.
810///
811/// `SymbolScope` is two facts in one word, and the trap is that the middle one is not the neutral
812/// answer it reads as. The writer turns `Compilation` into a local symbol, and it turns the choice
813/// between `Linkage` and `Dynamic` into `st_other`: `Linkage` is `STV_HIDDEN` and `Dynamic` is
814/// `STV_DEFAULT`. So there is no way to say global and decline to say anything about visibility,
815/// and picking the one whose name sounds like the smaller claim is picking hidden. That is what
816/// tamnd/rucc#733 was.
817///
818/// `Dynamic` is what every global asks for here, and the visibility is said afterwards by
819/// [`see`] rather than through this, so that nothing about `st_other` depends on reading one of
820/// these four names the way its author meant it.
821pub(crate) fn scope_of(binding: Binding) -> SymbolScope {
822    match binding {
823        Binding::Local => SymbolScope::Compilation,
824        Binding::Global | Binding::Weak => SymbolScope::Dynamic,
825    }
826}
827
828#[cfg(test)]
829mod tests {
830    use super::*;
831
832    use object::read::elf::Sym as _;
833    use object::read::{Object as _, ObjectSection as _, ObjectSymbol as _};
834    use object::{elf, pe};
835    use rucc_target::{Arch, Env, Os, Triple};
836
837    use crate::elf::PATCHABLE;
838    use crate::section::{Extent, Patch, Reloc};
839
840    /// A linux x86-64 target, which is the only one this writes.
841    fn target() -> TargetInfo {
842        TargetInfo::new(Triple::new(Arch::X86_64, Os::Linux, Env::Gnu))
843    }
844
845    /// One function of that name, at that offset, that many bytes long, and visible that far.
846    ///
847    /// Visibility is the field these cases mostly have no opinion about, so it is the one the
848    /// helper fills in and the two that do have an opinion write for themselves.
849    fn extent(name: String, start: usize, len: usize, binding: Binding) -> Extent {
850        Extent {
851            name,
852            start,
853            len,
854            align: crate::FUNC_ALIGN,
855            binding,
856            visibility: Visibility::Default,
857            patch: None,
858        }
859    }
860
861    /// A call to something outside the file, which is the shape every case here starts from.
862    fn calling(name: &str) -> Text {
863        Text {
864            bytes: vec![0xe8, 0, 0, 0, 0, 0xc3],
865            funcs: vec![extent("f".to_owned(), 0, 6, Binding::Global)],
866            relocs: vec![Reloc {
867                at: 1,
868                symbol: name.to_owned(),
869                kind: Reference::Call,
870                addend: -4,
871                after: 0,
872            }],
873            ..Text::default()
874        }
875    }
876
877    #[test]
878    fn the_bytes_come_back_out_of_the_section_they_went_into() {
879        let text = calling("puts");
880        let bytes =
881            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
882        let file = object::File::parse(&bytes[..]).expect("a readable object");
883        let section = file.section_by_name(".text").expect("a text section");
884        assert_eq!(section.data().expect("the bytes"), &text.bytes[..]);
885    }
886
887    #[test]
888    fn a_function_is_a_symbol_that_says_where_it_is_and_how_long_it_is() {
889        let mut text = calling("puts");
890        text.funcs.push(extent("g".to_owned(), 16, 1, Binding::Global));
891        text.bytes.resize(17, 0x90);
892        let bytes =
893            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
894        let file = object::File::parse(&bytes[..]).expect("a readable object");
895        let g = file.symbols().find(|s| s.name() == Ok("g")).expect("the second function");
896        assert_eq!(g.address(), 16);
897        assert_eq!(g.size(), 1);
898        assert_eq!(g.kind(), SymbolKind::Text);
899        assert!(g.is_global(), "nothing said otherwise about this one");
900    }
901
902    #[test]
903    fn a_function_no_other_file_can_see_is_a_local_symbol() {
904        let mut text = calling("puts");
905        text.funcs.push(extent("hidden".to_owned(), 16, 1, Binding::Local));
906        text.funcs.push(extent("shared".to_owned(), 32, 1, Binding::Weak));
907        text.bytes.resize(33, 0x90);
908        let bytes =
909            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
910        let file = object::File::parse(&bytes[..]).expect("a readable object");
911        let hidden = file.symbols().find(|s| s.name() == Ok("hidden")).expect("the static one");
912        // A symbol the linker keeps and does not let another file reach, which is the whole of
913        // what `static` on a function means and what two files each defining their own need.
914        assert!(hidden.is_local(), "a static function must not be offered to the linker");
915        assert!(!hidden.is_weak());
916        let shared = file.symbols().find(|s| s.name() == Ok("shared")).expect("the weak one");
917        assert!(shared.is_weak(), "a weak function has to be able to lose");
918        assert!(shared.is_global());
919    }
920
921    /// A global is `STV_DEFAULT`, so a shared library built from these objects exports something.
922    ///
923    /// The bug in tamnd/rucc#733. Every global came out `STV_HIDDEN`, which a static link does not
924    /// look at, so nothing here noticed and SQLite linked and ran and the whole test suite passed.
925    /// What it costs is the dynamic symbol table: `gcc -shared` over one of these objects produced
926    /// a library with an empty one, and `dlsym` could not find a function the file plainly defines.
927    ///
928    /// Written against `st_other` itself rather than against the reader's `scope`, because `scope`
929    /// is the word that was misread in the first place and a test that asks it the same question
930    /// would agree with whatever the writer did.
931    /// The record of where a patcher's room is, and what it says about it.
932    ///
933    /// Four things have to be right at once for a linker to take it: the flags, the alignment, the
934    /// relocation and the section it says it is ordered after. The last of those is the one the
935    /// writer underneath cannot say, so a zero there would be a file `ld` refuses and a test that
936    /// only looked at the bytes would not see it.
937    #[test]
938    fn where_a_patcher_may_write_is_recorded_in_a_section_tied_to_the_code_it_is_about() {
939        let mut text = calling("puts");
940        text.bytes.splice(0..0, [0x90, 0x90, 0x90]);
941        text.funcs[0].start = 3;
942        text.funcs[0].patch = Some(Patch { at: 0, before: 3 });
943        text.relocs[0].at = 4;
944        let bytes =
945            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
946        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
947        let section = file.section_by_name(PATCHABLE).expect("a record of the room");
948        assert_eq!(section.size(), 8, "one address, and this file defines one function");
949        assert_eq!(section.align(), 8);
950        let header = section.elf_section_header();
951        assert_eq!(
952            header.sh_flags.get(Endianness::Little),
953            elf::SHF_ALLOC | elf::SHF_WRITE | elf::SHF_LINK_ORDER
954        );
955        // Which is the whole point of the fixup: the index has to be the text section's own, and
956        // the writer underneath had written a zero there.
957        let index = file.section_by_name(".text").expect("a text section").index().0;
958        assert_eq!(header.sh_link.get(Endianness::Little) as usize, index);
959        assert_ne!(index, 0);
960
961        // And the address, which is the front of the room rather than the function's own symbol.
962        let [(at, reloc)] = &section.relocations().collect::<Vec<_>>()[..] else {
963            panic!("one address in the record")
964        };
965        assert_eq!(*at, 0);
966        assert_eq!(reloc.addend(), 0);
967        assert_eq!(reloc.flags(), RelocationFlags::Elf { r_type: elf::R_X86_64_64 });
968    }
969
970    /// And a file that asked for none has no such section, which is nearly every file.
971    #[test]
972    fn a_file_that_promised_a_patcher_nothing_records_nothing() {
973        let text = calling("puts");
974        let bytes =
975            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
976        let file = object::File::parse(&bytes[..]).expect("a readable object");
977        assert!(file.section_by_name(PATCHABLE).is_none());
978    }
979
980    /// The same when each function is a section of its own, which is what a kernel builds with.
981    ///
982    /// Each record then points at a different section, which is what makes the pairing worth
983    /// asserting: getting it backwards would still produce a file every tool reads and every
984    /// address in it would be about the wrong function.
985    #[test]
986    fn each_record_is_tied_to_its_own_function_when_they_are_split_up() {
987        let mut text = calling("puts");
988        text.funcs[0].patch = Some(Patch { at: 0, before: 0 });
989        text.funcs.push(extent("g".to_owned(), 16, 1, Binding::Global));
990        text.funcs[1].patch = Some(Patch { at: 16, before: 0 });
991        text.bytes.resize(17, 0x90);
992        let output =
993            Output { sections: Sections { functions: true, data: false }, ..Output::default() };
994        let bytes = write(&text, &Data::default(), &[], &target(), output).expect("an object");
995        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
996        let links: Vec<usize> = file
997            .sections()
998            .filter(|section| section.name() == Ok(PATCHABLE))
999            .map(|section| section.elf_section_header().sh_link.get(Endianness::Little) as usize)
1000            .collect();
1001        let index = |name: &str| file.section_by_name(name).expect("a text section").index().0;
1002        assert_eq!(links, [index(".text.f"), index(".text.g")]);
1003    }
1004
1005    #[test]
1006    fn a_global_is_visible_to_the_dynamic_linker_and_a_static_one_is_not_a_symbol_at_all() {
1007        let mut text = calling("puts");
1008        text.funcs.push(extent("g".to_owned(), 16, 1, Binding::Global));
1009        text.funcs.push(extent("w".to_owned(), 32, 1, Binding::Weak));
1010        text.funcs.push(extent("s".to_owned(), 48, 1, Binding::Local));
1011        text.bytes.resize(49, 0x90);
1012        let bytes =
1013            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
1014        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
1015        let visibility = |name: &str| {
1016            file.symbols()
1017                .find(|s| s.name() == Ok(name))
1018                .expect("the function")
1019                .elf_symbol()
1020                .st_visibility()
1021        };
1022        // Nothing said hidden about either of these, so neither is.
1023        assert_eq!(visibility("g"), elf::STV_DEFAULT);
1024        assert_eq!(visibility("w"), elf::STV_DEFAULT, "a weak one is still a name others may use");
1025        // The `static` one is local, and a local symbol's visibility means nothing either way,
1026        // which is why the binding is what this asks about.
1027        assert_eq!(visibility("s"), elf::STV_DEFAULT);
1028    }
1029
1030    /// And the other direction: a name that did ask to be hidden is hidden, and a protected one is
1031    /// protected.
1032    ///
1033    /// The half of tamnd/rucc#733 that the fix above left open. Saying `STV_DEFAULT` for everything
1034    /// is right for everything nobody marked and wrong the moment something is marked, so the two
1035    /// tests together are what says the field carries an answer rather than a constant.
1036    ///
1037    /// Both are asked of a function and of a variable, because they are added by two different
1038    /// loops in `write` and a field one of them fills in is not a field the other one does.
1039    #[test]
1040    fn a_name_that_asked_to_be_hidden_is_hidden_and_a_protected_one_is_protected() {
1041        let mut text = calling("puts");
1042        for (index, (name, seen)) in
1043            [("h", Visibility::Hidden), ("p", Visibility::Protected)].into_iter().enumerate()
1044        {
1045            let mut func = extent(name.to_owned(), 16 + index * 16, 1, Binding::Global);
1046            func.visibility = seen;
1047            text.funcs.push(func);
1048        }
1049        text.bytes.resize(49, 0x90);
1050        let mut data = Data::default();
1051        for (name, seen) in [("vh", Visibility::Hidden), ("vp", Visibility::Protected)] {
1052            let mut object = variable(name, Place::Written);
1053            object.visibility = seen;
1054            data.objects.push(object);
1055        }
1056        let bytes = write(&text, &data, &[], &target(), Output::default()).expect("an object");
1057        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
1058        let visibility = |name: &str| {
1059            file.symbols()
1060                .find(|s| s.name() == Ok(name))
1061                .expect("the symbol")
1062                .elf_symbol()
1063                .st_visibility()
1064        };
1065        assert_eq!(visibility("h"), elf::STV_HIDDEN);
1066        assert_eq!(visibility("p"), elf::STV_PROTECTED);
1067        assert_eq!(visibility("vh"), elf::STV_HIDDEN, "a variable goes through a second loop");
1068        assert_eq!(visibility("vp"), elf::STV_PROTECTED);
1069        // The one thing a visibility must not disturb, since `st_info` and `st_other` are written
1070        // in one go and the second was set after the first.
1071        let h = file.symbols().find(|s| s.name() == Ok("h")).expect("the function");
1072        assert!(h.is_global(), "hidden is about the dynamic linker and not about the binding");
1073        assert_eq!(h.size(), 1, "and it is still a function of the length it was");
1074    }
1075
1076    #[test]
1077    fn a_name_this_file_does_not_define_is_left_for_the_linker_to_find() {
1078        let bytes = write(&calling("puts"), &Data::default(), &[], &target(), Output::default())
1079            .expect("an object");
1080        let file = object::File::parse(&bytes[..]).expect("a readable object");
1081        let puts = file.symbols().find(|s| s.name() == Ok("puts")).expect("the callee");
1082        assert!(puts.is_undefined(), "the file does not define it and must not claim to");
1083    }
1084
1085    #[test]
1086    fn a_call_asks_for_the_relocation_a_stub_may_answer_and_a_load_asks_for_the_one_that_may_not() {
1087        for (reference, wanted) in [
1088            (Reference::Call, elf::R_X86_64_PLT32),
1089            (Reference::Data, elf::R_X86_64_PC32),
1090            (Reference::Got, elf::R_X86_64_REX_GOTPCRELX),
1091            (Reference::Thread, elf::R_X86_64_GOTTPOFF),
1092        ] {
1093            let mut text = calling("puts");
1094            text.relocs[0].kind = reference;
1095            let bytes = write(&text, &Data::default(), &[], &target(), Output::default())
1096                .expect("an object");
1097            let file = object::File::parse(&bytes[..]).expect("a readable object");
1098            let section = file.section_by_name(".text").expect("a text section");
1099            let (offset, reloc) = section.relocations().next().expect("one relocation");
1100            assert_eq!(offset, 1);
1101            assert_eq!(reloc.addend(), -4);
1102            assert_eq!(reloc.flags(), RelocationFlags::Elf { r_type: wanted });
1103        }
1104    }
1105
1106    #[test]
1107    fn a_name_wanted_twice_is_one_symbol_rather_than_two() {
1108        let mut text = calling("puts");
1109        text.relocs.push(Reloc {
1110            at: 1,
1111            symbol: "puts".to_owned(),
1112            kind: Reference::Call,
1113            addend: -4,
1114            after: 0,
1115        });
1116        let bytes =
1117            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
1118        let file = object::File::parse(&bytes[..]).expect("a readable object");
1119        assert_eq!(file.symbols().filter(|s| s.name() == Ok("puts")).count(), 1);
1120    }
1121
1122    #[test]
1123    fn a_function_that_is_also_called_is_not_a_second_symbol() {
1124        let text = calling("f");
1125        let bytes =
1126            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
1127        let file = object::File::parse(&bytes[..]).expect("a readable object");
1128        let mut found = file.symbols().filter(|s| s.name() == Ok("f"));
1129        let f = found.next().expect("the function");
1130        assert!(!f.is_undefined(), "the file defines it");
1131        assert!(found.next().is_none(), "and defines it once");
1132    }
1133
1134    #[test]
1135    fn the_marker_that_says_the_stack_is_not_executable_is_written() {
1136        let bytes = write(&calling("puts"), &Data::default(), &[], &target(), Output::default())
1137            .expect("an object");
1138        let file = object::File::parse(&bytes[..]).expect("a readable object");
1139        let note = file.section_by_name(".note.GNU-stack").expect("the marker");
1140        assert!(note.data().expect("no bytes").is_empty());
1141    }
1142
1143    /// What the file says it was built to have checked, byte for byte.
1144    ///
1145    /// Written against the bytes rather than against a reader, because the two lengths in the
1146    /// header count the padding after what they measure and a note whose lengths are one word out
1147    /// is one a linker drops without saying anything. What comes of that is a program the loader
1148    /// leaves the check turned off for, which is a build that looks like it worked.
1149    #[test]
1150    fn the_note_that_says_what_the_file_was_built_to_have_checked_is_written() {
1151        let property = Property { features: Property::IBT | Property::SHSTK };
1152        let output = Output { property, ..Output::default() };
1153        let bytes =
1154            write(&calling("puts"), &Data::default(), &[], &target(), output).expect("an object");
1155        let file = object::File::parse(&bytes[..]).expect("a readable object");
1156        let note = file.section_by_name(".note.gnu.property").expect("the note");
1157        assert_eq!(note.align(), 8, "a note in a sixty four bit object is read a word at a time");
1158        let want: Vec<u8> = [
1159            4u32,
1160            16,
1161            5,
1162            u32::from_le_bytes(*b"GNU\0"),
1163            Property::X86_FEATURES,
1164            4,
1165            Property::IBT | Property::SHSTK,
1166            0,
1167        ]
1168        .iter()
1169        .flat_map(|word| word.to_le_bytes())
1170        .collect();
1171        assert_eq!(note.data().expect("the bytes"), &want[..]);
1172    }
1173
1174    /// And nothing at all when the file was built to have nothing checked.
1175    ///
1176    /// A note with an empty feature word and no note are the same thing to a linker, which drops
1177    /// the whole property when any input lacks it. gcc writes nothing, so a section header that
1178    /// describes nothing would be the one difference between the two compilers' objects.
1179    #[test]
1180    fn a_file_built_to_have_nothing_checked_says_nothing() {
1181        let bytes = write(&calling("puts"), &Data::default(), &[], &target(), Output::default())
1182            .expect("an object");
1183        let file = object::File::parse(&bytes[..]).expect("a readable object");
1184        assert!(file.section_by_name(".note.gnu.property").is_none());
1185    }
1186
1187    /// Every unwind record names the function it is about, and each name goes where it is in the
1188    /// table rather than at the start of it.
1189    ///
1190    /// Written because working the offset out is the caller's job here, which is what the two text
1191    /// paths differ about, and a third caller that let it default to nothing would put every record
1192    /// in the table on the same function. Nothing else would notice: the section is the right
1193    /// length, the symbols are right, the link succeeds, and what comes of it is an unwinder that
1194    /// walks out of the wrong frame the first time something throws or a backtrace is taken.
1195    #[test]
1196    fn an_unwind_record_names_the_function_it_is_about_and_not_the_first_one() {
1197        let mut text = calling("puts");
1198        text.funcs.push(extent("g".to_owned(), 16, 1, Binding::Global));
1199        text.bytes.resize(17, 0x90);
1200        // A shared header and two records, whose contents nothing here reads: what is being asked
1201        // is where in them each name landed.
1202        text.unwind.bytes = vec![0; 64];
1203        for (at, name) in [(32usize, "f"), (48usize, "g")] {
1204            text.unwind.relocs.push(Reloc {
1205                at,
1206                symbol: name.to_owned(),
1207                kind: Reference::Address { bytes: 8 },
1208                addend: 0,
1209                after: 0,
1210            });
1211        }
1212        let bytes =
1213            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
1214        let file = object::File::parse(&bytes[..]).expect("a readable object");
1215        let mut found = points_at(&file);
1216        found.sort_unstable();
1217        assert_eq!(found, [(32, ".text".to_owned(), 0), (48, ".text".to_owned(), 16)]);
1218    }
1219
1220    /// What each record in the unwind table points at: where it is, the section it reaches, and
1221    /// how far into that section the function it is about begins.
1222    fn points_at(file: &object::File<'_>) -> Vec<(u64, String, i64)> {
1223        let frames = file.section_by_name(".eh_frame").expect("the table");
1224        frames
1225            .relocations()
1226            .map(|(offset, reloc)| {
1227                let object::RelocationTarget::Symbol(index) = reloc.target() else {
1228                    panic!("a record points at something that is not a symbol");
1229                };
1230                let symbol = file.symbol_by_index(index).expect("a symbol that is in the table");
1231                assert_eq!(symbol.kind(), SymbolKind::Section, "a record names a section");
1232                let section = symbol.section_index().expect("a section symbol is in one");
1233                let name = file.section_by_index(section).expect("a readable section");
1234                (offset, name.name().expect("a named section").to_owned(), reloc.addend())
1235            })
1236            .collect()
1237    }
1238
1239    /// A record points at the section its function is in rather than at the function's name.
1240    ///
1241    /// Written for tamnd/rucc#1004, which was that nothing this compiler wrote could go into a
1242    /// shared library. A global name is answered at load time by whichever object defines it
1243    /// first, so the distance from a record to one of them is not a distance a static linker can
1244    /// work out, and `ld` says so and stops with advice to recompile with the flag that was
1245    /// already on the command line. A section is settled by then, which is why gcc measures to a
1246    /// local label and why this measures to the section.
1247    ///
1248    /// Both ways of splitting the text, because the offset is the part that differs: one section
1249    /// holding everything makes it the function's place in the whole text, and a section per
1250    /// function makes it whatever room a patcher was promised in front of the label.
1251    #[test]
1252    fn a_record_reaches_its_function_through_the_section_it_is_in() {
1253        let mut text = two();
1254        text.unwind.bytes = vec![0; 64];
1255        for (at, name) in [(32usize, "f"), (48usize, "g")] {
1256            text.unwind.relocs.push(Reloc {
1257                at,
1258                symbol: name.to_owned(),
1259                kind: Reference::Data,
1260                addend: 0,
1261                after: 0,
1262            });
1263        }
1264        let bytes =
1265            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
1266        let file = object::File::parse(&bytes[..]).expect("a readable object");
1267        let mut whole = points_at(&file);
1268        whole.sort_unstable();
1269        assert_eq!(whole, [(32, ".text".to_owned(), 0), (48, ".text".to_owned(), 16)]);
1270
1271        let sections =
1272            Output { sections: Sections { functions: true, data: false }, ..Output::default() };
1273        let bytes = write(&text, &Data::default(), &[], &target(), sections).expect("an object");
1274        let file = object::File::parse(&bytes[..]).expect("a readable object");
1275        let mut split = points_at(&file);
1276        split.sort_unstable();
1277        assert_eq!(split, [(32, ".text.f".to_owned(), 0), (48, ".text.g".to_owned(), 0)]);
1278    }
1279
1280    /// A record about a name this file does not define is refused rather than written.
1281    ///
1282    /// There is no such file today: the table is built beside the text out of the functions that
1283    /// were just compiled. It is refused rather than left to the linker because the alternative is
1284    /// the shape that was just fixed, a record measured to a name, and the writer saying what it
1285    /// was given is how that stays fixed.
1286    #[test]
1287    fn a_record_about_something_this_file_does_not_define_is_refused() {
1288        let mut text = calling("puts");
1289        text.unwind.bytes = vec![0; 64];
1290        text.unwind.relocs.push(Reloc {
1291            at: 32,
1292            symbol: "puts".to_owned(),
1293            kind: Reference::Data,
1294            addend: 0,
1295            after: 0,
1296        });
1297        let why = write(&text, &Data::default(), &[], &target(), Output::default())
1298            .expect_err("a record about a name from somewhere else");
1299        assert!(why.to_string().contains("puts"), "{why}");
1300    }
1301
1302    /// The name of the section that symbol is defined in.
1303    fn lives_in<'a>(file: &'a object::File<'a>, name: &str) -> String {
1304        let symbol = file.symbols().find(|s| s.name() == Ok(name)).expect("the symbol");
1305        let index = symbol.section_index().expect("a section to be defined in");
1306        let section = file.section_by_index(index).expect("a readable section");
1307        section.name().expect("a named section").to_owned()
1308    }
1309
1310    /// Two functions, the second of them sixteen bytes in and calling something outside the file.
1311    fn two() -> Text {
1312        let mut text = calling("puts");
1313        // Padded to where the second one is aligned to, with the instruction that does nothing,
1314        // because the space in front of a function is reached by falling off the end of one.
1315        text.bytes.resize(16, 0x90);
1316        text.bytes.extend_from_slice(&[0xe8, 0, 0, 0, 0, 0xc3]);
1317        text.funcs.push(extent("g".to_owned(), 16, 6, Binding::Global));
1318        text.relocs.push(Reloc {
1319            at: 17,
1320            symbol: "puts".to_owned(),
1321            kind: Reference::Call,
1322            addend: -4,
1323            after: 0,
1324        });
1325        text
1326    }
1327
1328    /// What `-ffunction-sections` comes down to in an object file, which is the flag that makes
1329    /// `--gc-sections` able to drop anything: a linker can leave out a section nothing reaches and
1330    /// cannot leave out half of one.
1331    ///
1332    /// The empty `.text` stays, because it is the section the writer underneath opens a file with
1333    /// and gcc 16 leaves an empty one behind under the flag too.
1334    #[test]
1335    fn every_function_gets_a_section_of_its_own_when_that_is_what_was_asked_for() {
1336        let sections =
1337            Output { sections: Sections { functions: true, data: false }, ..Output::default() };
1338        let bytes = write(&two(), &Data::default(), &[], &target(), sections).expect("an object");
1339        let file = object::File::parse(&bytes[..]).expect("a readable object");
1340        assert_eq!(lives_in(&file, "f"), ".text.f");
1341        assert_eq!(lives_in(&file, "g"), ".text.g");
1342        assert!(file.section_by_name(".text").expect("the empty one").size() == 0);
1343        // Each one at nothing into its own section, and as long as it was: a function alone in a
1344        // section starts where the section does, whatever it started at when they shared one.
1345        for name in ["f", "g"] {
1346            let symbol = file.symbols().find(|s| s.name() == Ok(name)).expect("the function");
1347            assert_eq!(symbol.address(), 0, "{name}");
1348            assert_eq!(symbol.size(), 6, "{name}");
1349        }
1350        let section = file.section_by_name(".text.g").expect("the second function");
1351        assert_eq!(section.data().expect("the bytes"), &[0xe8, 0, 0, 0, 0, 0xc3]);
1352        // The padding between the two is gone with them, since it was there to align the second
1353        // one inside a section they shared and each section is aligned by the linker now.
1354        assert_eq!(section.align(), u64::from(crate::FUNC_ALIGN));
1355    }
1356
1357    /// A relocation counts from the start of whichever section its function ended up in, which is
1358    /// the arithmetic the split path has to do and the unsplit one never does.
1359    ///
1360    /// Getting it wrong is a call patched over the wrong bytes, which assembles, links, and jumps
1361    /// into the middle of an instruction at run time.
1362    #[test]
1363    fn a_relocation_moves_with_the_function_whose_bytes_it_is_in() {
1364        let sections =
1365            Output { sections: Sections { functions: true, data: false }, ..Output::default() };
1366        let bytes = write(&two(), &Data::default(), &[], &target(), sections).expect("an object");
1367        let file = object::File::parse(&bytes[..]).expect("a readable object");
1368        for name in [".text.f", ".text.g"] {
1369            let section = file.section_by_name(name).expect("a function");
1370            let (offset, _) = section.relocations().next().expect("the call in it");
1371            // One byte in either way, because the call is the first instruction of both and the
1372            // opcode is one byte in front of the address the linker fills in.
1373            assert_eq!(offset, 1, "{name}");
1374            assert_eq!(section.relocations().count(), 1, "{name}");
1375        }
1376    }
1377
1378    /// One variable of four bytes, in whichever section its own answer puts it.
1379    fn variable(name: &str, place: Place) -> Object {
1380        Object {
1381            name: name.to_owned(),
1382            bytes: if carries_no_bytes(&place) { Vec::new() } else { vec![1, 0, 0, 0] },
1383            size: 4,
1384            align: 4,
1385            place,
1386            binding: Binding::Global,
1387            visibility: Visibility::Default,
1388            relocs: Vec::new(),
1389        }
1390    }
1391
1392    /// A file of that one variable and nothing else.
1393    fn holding(object: Object) -> Vec<u8> {
1394        let data = Data { weak: Vec::new(), objects: vec![object] };
1395        write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object")
1396    }
1397
1398    #[test]
1399    fn what_a_variable_is_decides_which_section_it_goes_in() {
1400        for (place, wanted) in [
1401            (Place::Written, ".data"),
1402            (Place::ReadOnly, ".rodata"),
1403            (Place::RelocReadOnly { local: false }, ".data.rel.ro"),
1404            (Place::RelocReadOnly { local: true }, ".data.rel.ro.local"),
1405            (Place::Zero, ".bss"),
1406            (Place::Thread { zero: false }, ".tdata"),
1407            (Place::Thread { zero: true }, ".tbss"),
1408            (Place::Named(".init_array".to_owned()), ".init_array"),
1409        ] {
1410            let bytes = holding(variable("x", place.clone()));
1411            let file = object::File::parse(&bytes[..]).expect("a readable object");
1412            let section = file.section_by_name(wanted).unwrap_or_else(|| panic!("{place:?}"));
1413            assert_eq!(section.size(), 4, "{place:?}");
1414            // The zero filled one is as long as it says and carries none of it, which is the
1415            // whole reason the section exists.
1416            let carried = section.data().expect("the bytes").len();
1417            assert_eq!(carried, if carries_no_bytes(&place) { 0 } else { 4 }, "{place:?}");
1418        }
1419    }
1420
1421    /// The section is half of it and the symbol is the other half.
1422    ///
1423    /// A linker checks a relocation against the kind of the symbol it names, so a variable that is
1424    /// in `.tdata` and is an ordinary data symbol is one an ordinary reference resolves to an
1425    /// address that belongs to no thread. `STT_TLS` is what makes that reference an error instead.
1426    #[test]
1427    fn a_thread_local_variable_is_a_thread_local_symbol_and_not_only_a_thread_local_section() {
1428        for place in [Place::Thread { zero: false }, Place::Thread { zero: true }] {
1429            let bytes = holding(variable("counter", place.clone()));
1430            let file = object::File::parse(&bytes[..]).expect("a readable object");
1431            let symbol = file
1432                .symbols()
1433                .find(|symbol| symbol.name() == Ok("counter"))
1434                .unwrap_or_else(|| panic!("{place:?}"));
1435            assert_eq!(symbol.kind(), SymbolKind::Tls, "{place:?}");
1436        }
1437    }
1438
1439    /// The section type a startup list carries, which is what makes the CRT call what is in it.
1440    ///
1441    /// A section of the ordinary type with the right name is gathered by the linker in the same run
1442    /// and called by nobody, so the type is the whole of what this is about. The numbered name is
1443    /// the same kind of section as the plain one: the number is there so that the linker sorts it.
1444    #[test]
1445    fn a_section_of_function_addresses_carries_the_type_the_runtime_looks_for() {
1446        for (name, wanted) in [
1447            (".init_array", elf::SHT_INIT_ARRAY),
1448            (".init_array.00101", elf::SHT_INIT_ARRAY),
1449            (".fini_array", elf::SHT_FINI_ARRAY),
1450            (".preinit_array", elf::SHT_PREINIT_ARRAY),
1451            (".init_arrays", elf::SHT_PROGBITS),
1452        ] {
1453            let bytes = holding(variable("x", Place::Named(name.to_owned())));
1454            let file = object::File::parse(&bytes[..]).expect("a readable object");
1455            let section = file.section_by_name(name).unwrap_or_else(|| panic!("{name}"));
1456            let SectionFlags::Elf { sh_type, sh_flags } = section.flags() else {
1457                panic!("{name} is not an elf section");
1458            };
1459            assert_eq!(sh_type, wanted, "{name}");
1460            assert!(sh_flags.contains(elf::SHF_ALLOC | elf::SHF_WRITE), "{name}");
1461        }
1462    }
1463
1464    /// Two variables the program put one section name on, which belong in one section.
1465    ///
1466    /// A file with ten constructors in it would otherwise carry ten section headers describing eight
1467    /// bytes each, and the order the entries run in would be the order the linker happened to put
1468    /// the headers in rather than the order they were written.
1469    #[test]
1470    fn two_variables_in_one_named_section_share_it() {
1471        let objects = vec![
1472            variable("x", Place::Named(".init_array".to_owned())),
1473            variable("y", Place::Named(".init_array".to_owned())),
1474        ];
1475        let data = Data { weak: Vec::new(), objects };
1476        let bytes =
1477            write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object");
1478        let file = object::File::parse(&bytes[..]).expect("a readable object");
1479        let named: Vec<_> =
1480            file.sections().filter(|section| section.name() == Ok(".init_array")).collect();
1481        assert_eq!(named.len(), 1);
1482        assert_eq!(named[0].size(), 8);
1483    }
1484
1485    /// What `-fdata-sections` comes down to in an object file: the section a variable would have
1486    /// shared, with its own name after it. The names are gcc 16's, checked against it on a Linux
1487    /// host, and the part in front of the dot is what a linker script and `--gc-sections` match on.
1488    #[test]
1489    fn every_variable_gets_a_section_of_its_own_when_that_is_what_was_asked_for() {
1490        let sections =
1491            Output { sections: Sections { functions: false, data: true }, ..Output::default() };
1492        for (place, wanted) in [
1493            (Place::Written, ".data.x"),
1494            (Place::ReadOnly, ".rodata.x"),
1495            (Place::RelocReadOnly { local: false }, ".data.rel.ro.x"),
1496            (Place::RelocReadOnly { local: true }, ".data.rel.ro.local.x"),
1497            (Place::Zero, ".bss.x"),
1498            (Place::Thread { zero: false }, ".tdata.x"),
1499            (Place::Thread { zero: true }, ".tbss.x"),
1500        ] {
1501            let data = Data { weak: Vec::new(), objects: vec![variable("x", place.clone())] };
1502            let bytes = write(&Text::default(), &data, &[], &target(), sections).expect("object");
1503            let file = object::File::parse(&bytes[..]).expect("a readable object");
1504            assert_eq!(lives_in(&file, "x"), wanted, "{place:?}");
1505            let section = file.section_by_name(wanted).expect("the section it named");
1506            assert_eq!(section.size(), 4, "{place:?}");
1507            // Which page it lands in is what the section it came out of decided, and splitting
1508            // must not quietly change it: the zero filled one still carries none of its bytes.
1509            let carried = section.data().expect("the bytes").len();
1510            assert_eq!(carried, if carries_no_bytes(&place) { 0 } else { 4 }, "{place:?}");
1511        }
1512    }
1513
1514    /// The two kinds of variable the flag leaves alone. A tentative definition is a request to the
1515    /// linker for that much zeroed space rather than an image, so there is no section to split off,
1516    /// and one the program named has the answer the source gave, which a flag must not overrule.
1517    #[test]
1518    fn a_variable_that_has_no_section_of_its_own_to_be_given_is_left_where_it_was() {
1519        let sections =
1520            Output { sections: Sections { functions: false, data: true }, ..Output::default() };
1521        let named = Place::Named(".init_array".to_owned());
1522        let objects = vec![variable("m", Place::Merged), variable("n", named)];
1523        let bytes =
1524            write(&Text::default(), &Data { weak: Vec::new(), objects }, &[], &target(), sections)
1525                .expect("object");
1526        let file = object::File::parse(&bytes[..]).expect("a readable object");
1527        let m = file.symbols().find(|s| s.name() == Ok("m")).expect("the tentative one");
1528        assert!(m.is_common(), "still the linker's to merge and not in a section at all");
1529        assert_eq!(lives_in(&file, "n"), ".init_array");
1530        assert!(file.section_by_name(".init_array.n").is_none(), "the source already answered");
1531    }
1532
1533    /// A relocation in a variable's image counts from the start of the section it ended up in, the
1534    /// same question the split text has to answer and a shorter answer: a variable alone in a
1535    /// section starts where the section does.
1536    #[test]
1537    fn a_relocation_in_an_image_moves_with_the_variable_whose_image_it_is_in() {
1538        let sections =
1539            Output { sections: Sections { functions: false, data: true }, ..Output::default() };
1540        let pointer = Object {
1541            bytes: vec![0; 8],
1542            size: 8,
1543            align: 8,
1544            relocs: vec![Reloc {
1545                at: 0,
1546                symbol: "y".to_owned(),
1547                kind: Reference::Address { bytes: 8 },
1548                addend: 0,
1549                after: 0,
1550            }],
1551            ..variable("p", Place::Written)
1552        };
1553        let objects = vec![variable("first", Place::Written), pointer];
1554        let bytes =
1555            write(&Text::default(), &Data { weak: Vec::new(), objects }, &[], &target(), sections)
1556                .expect("object");
1557        let file = object::File::parse(&bytes[..]).expect("a readable object");
1558        let section = file.section_by_name(".data.p").expect("the pointer's own section");
1559        let (offset, reloc) = section.relocations().next().expect("one relocation");
1560        // Nothing rather than the eight it would be if the variable in front of it were still
1561        // counted, which is what a section of its own means.
1562        assert_eq!(offset, 0);
1563        assert_eq!(reloc.flags(), RelocationFlags::Elf { r_type: elf::R_X86_64_64 });
1564    }
1565
1566    /// Two variables that want `.data.rel.ro.local` end up in one section, not two of one name.
1567    ///
1568    /// The writer has no name of its own for that section, so it is added by hand, and asking for
1569    /// it again makes a second section rather than handing back the first. SQLite has enough const
1570    /// tables of function pointers in it to turn that into eighty odd sections in one object, each
1571    /// with its own relocation section beside it, which is a pile of section headers describing
1572    /// eight bytes apiece.
1573    #[test]
1574    fn every_variable_that_wants_the_local_relocated_section_shares_one() {
1575        let place = Place::RelocReadOnly { local: true };
1576        let data = Data {
1577            weak: Vec::new(),
1578            objects: vec![variable("first", place.clone()), variable("second", place)],
1579        };
1580        let bytes =
1581            write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object");
1582        let file = object::File::parse(&bytes[..]).expect("a readable object");
1583        let named = file.sections().filter(|s| s.name() == Ok(".data.rel.ro.local")).count();
1584        assert_eq!(named, 1, "one section holding both, not one each");
1585    }
1586
1587    #[test]
1588    fn a_variable_is_a_symbol_that_says_where_it_is_and_how_long_it_is() {
1589        let mut data = Data { weak: Vec::new(), objects: vec![variable("first", Place::Written)] };
1590        data.objects.push(Object { align: 16, ..variable("second", Place::Written) });
1591        let bytes =
1592            write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object");
1593        let file = object::File::parse(&bytes[..]).expect("a readable object");
1594        let second = file.symbols().find(|s| s.name() == Ok("second")).expect("the second one");
1595        assert_eq!(second.kind(), SymbolKind::Data);
1596        assert_eq!(second.size(), 4);
1597        // Sixteen rather than four, because the second one asked for sixteen and the first one
1598        // had already used four. Getting this wrong is a variable at an address it said it would
1599        // never be at, which nothing downstream would notice until an aligned load faulted.
1600        assert_eq!(second.address(), 16);
1601    }
1602
1603    #[test]
1604    fn the_linkage_a_variable_had_is_the_binding_the_symbol_gets() {
1605        for (binding, global, weak) in [
1606            (Binding::Global, true, false),
1607            (Binding::Local, false, false),
1608            (Binding::Weak, true, true),
1609        ] {
1610            let bytes = holding(Object { binding, ..variable("x", Place::Written) });
1611            let file = object::File::parse(&bytes[..]).expect("a readable object");
1612            let x = file.symbols().find(|s| s.name() == Ok("x")).expect("the variable");
1613            assert_eq!(x.is_global(), global, "{binding:?}");
1614            assert_eq!(x.is_weak(), weak, "{binding:?}");
1615        }
1616    }
1617
1618    #[test]
1619    fn a_tentative_definition_asks_the_linker_for_space_rather_than_naming_any() {
1620        let bytes = holding(Object { align: 8, ..variable("x", Place::Merged) });
1621        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
1622        let x = file.symbols().find(|s| s.name() == Ok("x")).expect("the variable");
1623        assert!(x.is_common(), "the linker merges every definition of this name into one");
1624        assert_eq!(x.size(), 4);
1625        // What a common symbol records where an ordinary one records its address is what it wants
1626        // to be aligned to, because it has no address yet. The reader deliberately answers nothing
1627        // when asked for the address of one, so this is the field itself.
1628        assert_eq!(x.address(), 0);
1629        assert_eq!(x.elf_symbol().st_value(Endianness::Little), 8);
1630    }
1631
1632    #[test]
1633    fn an_address_in_an_image_is_the_address_and_not_a_distance_to_it() {
1634        let object = Object {
1635            bytes: vec![0; 8],
1636            size: 8,
1637            align: 8,
1638            relocs: vec![Reloc {
1639                at: 0,
1640                symbol: "y".to_owned(),
1641                kind: Reference::Address { bytes: 8 },
1642                addend: 16,
1643                after: 0,
1644            }],
1645            ..variable("p", Place::Written)
1646        };
1647        let bytes = holding(object);
1648        let file = object::File::parse(&bytes[..]).expect("a readable object");
1649        let section = file.section_by_name(".data").expect("a data section");
1650        let (offset, reloc) = section.relocations().next().expect("one relocation");
1651        assert_eq!(offset, 0);
1652        assert_eq!(reloc.addend(), 16);
1653        assert_eq!(reloc.flags(), RelocationFlags::Elf { r_type: elf::R_X86_64_64 });
1654        let y = file.symbols().find(|s| s.name() == Ok("y")).expect("what it points at");
1655        assert!(y.is_undefined(), "nothing here defines it and the linker is being asked for it");
1656    }
1657
1658    /// A name a declaration wrote `weak` on is undefined and may stay that way.
1659    ///
1660    /// The difference between this and the case above is one bit and the whole of what a link does
1661    /// about it: an ordinary undefined symbol is a name the linker has to find, and a weak one is a
1662    /// name it may fail to find, in which case every reference reads a zero address. That is what
1663    /// lets a library offer a hook a profiler may fill in, which is tamnd/rucc#1414.
1664    #[test]
1665    fn a_weak_undefined_name_is_one_the_link_may_leave_unfound() {
1666        let mut text = Text::default();
1667        text.funcs.push(extent("caller".to_owned(), 0, 8, Binding::Global));
1668        text.bytes.resize(8, 0x90);
1669        text.relocs.push(Reloc {
1670            at: 1,
1671            symbol: "hook".to_owned(),
1672            kind: Reference::Call,
1673            addend: -4,
1674            after: 0,
1675        });
1676        let data =
1677            Data { weak: vec!["hook".to_owned(), "never_called".to_owned()], objects: vec![] };
1678        let bytes = write(&text, &data, &[], &target(), Output::default()).expect("an object");
1679        let file = object::File::parse(&bytes[..]).expect("a readable object");
1680
1681        let hook = file.symbols().find(|s| s.name() == Ok("hook")).expect("the one called");
1682        assert!(hook.is_undefined(), "nothing here defines it");
1683        assert!(hook.is_weak(), "so the link may leave it alone rather than fail");
1684
1685        // And one nothing refers to is still written down, because the listing writes a directive
1686        // for it and the two paths have to put the same entries in. A linker has nothing to do
1687        // about an undefined weak symbol no relocation names.
1688        let quiet = file.symbols().find(|s| s.name() == Ok("never_called")).expect("the other");
1689        assert!(quiet.is_undefined() && quiet.is_weak(), "{:?}", quiet.flags());
1690    }
1691
1692    /// A name this file reads through the thread pointer is undefined and is still known to be
1693    /// thread-local.
1694    ///
1695    /// The other undefined names here are written with no type at all, because a name this file does
1696    /// not define is a name this file has nothing to say about. A thread-local one is different in
1697    /// the one way that counts: a reference to it is satisfied by an offset into a block rather than
1698    /// by an address, so the linker has to know which of the two is wanted before it has found the
1699    /// definition, and rather than guess it refuses a link where one file says `STT_TLS` about a name
1700    /// and another does not. Writing the type is not extra information, it is the same information
1701    /// the relocation already carried, said where the linker looks for it.
1702    ///
1703    /// That is tamnd/rucc#1461. libmpfr defines `__gmpfr_flags` in `exceptions.c` and reads it in a
1704    /// hundred other files, and the link stopped at the first reader with `TLS definition in
1705    /// exceptions.o section .tdata mismatches non-TLS reference in add.o`.
1706    #[test]
1707    fn a_thread_local_name_this_file_only_reads_is_still_written_down_as_thread_local() {
1708        let mut text = Text::default();
1709        text.funcs.push(extent("reader".to_owned(), 0, 16, Binding::Global));
1710        text.bytes.resize(16, 0x90);
1711        text.relocs.push(Reloc {
1712            at: 3,
1713            symbol: "flags".to_owned(),
1714            kind: Reference::Thread,
1715            addend: -4,
1716            after: 0,
1717        });
1718        // One of them reached the ordinary way, so that what the type says is the relocation's doing
1719        // and not something every undefined name here would have got.
1720        text.relocs.push(Reloc {
1721            at: 10,
1722            symbol: "shared".to_owned(),
1723            kind: Reference::Got,
1724            addend: -4,
1725            after: 0,
1726        });
1727        let data = Data { weak: Vec::new(), objects: vec![] };
1728        let bytes = write(&text, &data, &[], &target(), Output::default()).expect("an object");
1729        let file = object::File::parse(&bytes[..]).expect("a readable object");
1730
1731        let flags = file.symbols().find(|s| s.name() == Ok("flags")).expect("the thread-local one");
1732        assert!(flags.is_undefined(), "nothing here defines it");
1733        assert_eq!(flags.kind(), SymbolKind::Tls, "which is what the linker refuses to guess");
1734
1735        let shared = file.symbols().find(|s| s.name() == Ok("shared")).expect("the ordinary one");
1736        assert!(shared.is_undefined(), "nothing here defines this one either");
1737        assert_eq!(shared.kind(), SymbolKind::Unknown, "and there is nothing to say about it");
1738    }
1739
1740    /// Not a rewording of the case above: what is checked is the arithmetic between the two.
1741    #[test]
1742    fn a_relocation_counts_from_the_start_of_the_section_and_not_of_the_image_it_is_in() {
1743        let mut data = Data { weak: Vec::new(), objects: vec![variable("first", Place::Written)] };
1744        data.objects.push(Object {
1745            bytes: vec![0; 16],
1746            size: 16,
1747            align: 8,
1748            relocs: vec![Reloc {
1749                at: 8,
1750                symbol: "y".to_owned(),
1751                kind: Reference::Address { bytes: 8 },
1752                addend: 0,
1753                after: 0,
1754            }],
1755            ..variable("second", Place::Written)
1756        });
1757        let bytes =
1758            write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object");
1759        let file = object::File::parse(&bytes[..]).expect("a readable object");
1760        let section = file.section_by_name(".data").expect("a data section");
1761        let (offset, _) = section.relocations().next().expect("one relocation");
1762        // Eight into the second image, which starts eight in because the first one is four long
1763        // and the second is eight aligned.
1764        assert_eq!(offset, 16);
1765    }
1766
1767    #[test]
1768    fn a_second_name_is_a_second_symbol_at_the_first_one_s_address_and_no_second_image() {
1769        let data = Data {
1770            weak: Vec::new(),
1771            objects: vec![Object { binding: Binding::Local, ..variable("a", Place::Written) }],
1772        };
1773        let aliases = [Alias {
1774            name: "b".to_owned(),
1775            target: "a".to_owned(),
1776            binding: Binding::Global,
1777            visibility: Visibility::Default,
1778        }];
1779        let bytes = write(&Text::default(), &data, &aliases, &target(), Output::default())
1780            .expect("an object");
1781        let file = object::File::parse(&bytes[..]).expect("a readable object");
1782        let a = file.symbols().find(|s| s.name() == Ok("a")).expect("the variable");
1783        let b = file.symbols().find(|s| s.name() == Ok("b")).expect("the second name");
1784        assert_eq!(b.address(), a.address(), "the same place");
1785        assert_eq!(b.size(), a.size());
1786        assert_eq!(b.section_index(), a.section_index());
1787        // The binding is the one thing the second name does not take from the first, which is
1788        // what `extern int b __attribute__((alias("a")))` on a `static a` asks for.
1789        assert!(a.is_local(), "the target was written `static`");
1790        assert!(b.is_global(), "and the name given to it was not");
1791        // Four bytes of image and not eight, since an alias is a name and not a copy.
1792        assert_eq!(file.section_by_name(".data").expect("a data section").size(), 4);
1793    }
1794
1795    #[test]
1796    fn a_function_can_be_given_a_second_name_the_same_way_a_variable_can() {
1797        let text = calling("puts");
1798        let aliases = [Alias {
1799            name: "g".to_owned(),
1800            target: "f".to_owned(),
1801            binding: Binding::Weak,
1802            visibility: Visibility::Default,
1803        }];
1804        let bytes = write(&text, &Data::default(), &aliases, &target(), Output::default())
1805            .expect("an object");
1806        let file = object::File::parse(&bytes[..]).expect("a readable object");
1807        let f = file.symbols().find(|s| s.name() == Ok("f")).expect("the function");
1808        let g = file.symbols().find(|s| s.name() == Ok("g")).expect("the second name");
1809        assert_eq!(g.address(), f.address());
1810        assert_eq!(g.size(), f.size());
1811        assert_eq!(g.kind(), f.kind(), "a second name for a function is a function");
1812        assert!(g.is_weak(), "so that a program may define the name itself instead");
1813    }
1814
1815    /// The front end is what reports this as a program's mistake, so one arriving here is a bug
1816    /// in this compiler and is said so rather than written as an undefined symbol.
1817    #[test]
1818    fn a_second_name_for_something_this_file_does_not_define_is_refused() {
1819        let aliases = [Alias {
1820            name: "b".to_owned(),
1821            target: "a".to_owned(),
1822            binding: Binding::Global,
1823            visibility: Visibility::Default,
1824        }];
1825        let error =
1826            write(&Text::default(), &Data::default(), &aliases, &target(), Output::default())
1827                .expect_err("nothing to point at");
1828        assert!(matches!(error, Error::Refused { .. }), "{error:?}");
1829    }
1830
1831    #[test]
1832    fn a_platform_this_does_not_write_is_said_so_rather_than_written_as_elf() {
1833        let text = calling("puts");
1834        for triple in [
1835            Triple::new(Arch::Aarch64, Os::Linux, Env::Gnu),
1836            Triple::new(Arch::X86_64, Os::Darwin, Env::Gnu),
1837        ] {
1838            let error =
1839                write(&text, &Data::default(), &[], &TargetInfo::new(triple), Output::default())
1840                    .expect_err("no writer");
1841            assert!(matches!(error, Error::Format { .. }), "{error:?}");
1842        }
1843    }
1844
1845    /// What the archive's symbol index is built from is what the linker can find in the member.
1846    ///
1847    /// Written against the object rather than against the list, because the two agreeing is the
1848    /// whole point: a list that says more than the file does is an archive that promises a
1849    /// definition it does not have, and a list that says less is a member nothing pulls out.
1850    #[test]
1851    fn the_names_a_linker_can_find_are_the_names_the_list_gives() {
1852        let mut text = calling("puts");
1853        text.funcs.push(extent("hidden".to_owned(), 16, 1, Binding::Local));
1854        text.funcs.push(extent("shared".to_owned(), 32, 1, Binding::Weak));
1855        text.bytes.resize(33, 0x90);
1856        let data = Data {
1857            weak: Vec::new(),
1858            objects: vec![variable("seen", Place::Written), {
1859                let mut quiet = variable("quiet", Place::Zero);
1860                quiet.binding = Binding::Local;
1861                quiet
1862            }],
1863        };
1864        let aliases = [Alias {
1865            name: "second".to_owned(),
1866            target: "f".to_owned(),
1867            binding: Binding::Global,
1868            visibility: Visibility::Default,
1869        }];
1870
1871        let names = defines(&text, &data, &aliases, &target()).expect("a list");
1872        assert_eq!(names, ["f", "shared", "seen", "second"]);
1873
1874        let bytes = write(&text, &data, &aliases, &target(), Output::default()).expect("an object");
1875        let file = object::File::parse(&bytes[..]).expect("a readable object");
1876        let found: Vec<String> = file
1877            .symbols()
1878            .filter(|symbol| symbol.is_global() && symbol.is_definition())
1879            .map(|symbol| symbol.name().unwrap_or_default().to_owned())
1880            .collect();
1881        let mut sorted = names.clone();
1882        sorted.sort();
1883        let mut theirs = found;
1884        theirs.sort();
1885        assert_eq!(sorted, theirs, "the list and the file have to say the same thing");
1886    }
1887
1888    /// A windows x86-64 target, which is the other format this writes.
1889    fn windows() -> TargetInfo {
1890        TargetInfo::new(Triple::new(Arch::X86_64, Os::Windows, Env::Gnu))
1891    }
1892
1893    /// What the four bytes a relocation covers hold, which is where COFF keeps its addend.
1894    fn inline(bytes: &[u8], section: &str, at: usize) -> i32 {
1895        let file = object::File::parse(bytes).expect("a readable object");
1896        let found = file.section_by_name(section).expect("the section").data().expect("the bytes");
1897        i32::from_le_bytes(found[at..at + 4].try_into().expect("four bytes"))
1898    }
1899
1900    #[test]
1901    fn a_windows_target_is_written_rather_than_refused() {
1902        let text = calling("puts");
1903        let bytes =
1904            write(&text, &Data::default(), &[], &windows(), Output::default()).expect("an object");
1905        let file = object::File::parse(&bytes[..]).expect("a readable object");
1906        assert_eq!(file.format(), BinaryFormat::Coff);
1907        let section = file.section_by_name(".text").expect("a text section");
1908        assert_eq!(section.data().expect("the bytes"), &text.bytes[..]);
1909        let names: Vec<&str> = file.symbols().filter_map(|symbol| symbol.name().ok()).collect();
1910        assert!(names.contains(&"f"), "{names:?}");
1911        assert!(names.contains(&"puts"), "{names:?}");
1912    }
1913
1914    /// The whole reason a relocation carries where the instruction ended as well as the addend.
1915    ///
1916    /// A call ends at the four bytes the linker writes over, and a store of a constant through an
1917    /// address counted from the instruction pointer has the constant after them, and ELF tells the
1918    /// two apart by the addend alone. COFF cannot: it says how far the end is in the relocation type
1919    /// and works the addend out from that, so the same four bytes come out of two different types
1920    /// and both have to end up meaning the same distance.
1921    #[test]
1922    fn how_far_the_instruction_runs_past_the_hole_is_in_the_relocation_type() {
1923        for (after, typ) in [
1924            (0, pe::IMAGE_REL_AMD64_REL32),
1925            (1, pe::IMAGE_REL_AMD64_REL32_1),
1926            (4, pe::IMAGE_REL_AMD64_REL32_4),
1927            (5, pe::IMAGE_REL_AMD64_REL32_5),
1928        ] {
1929            let mut text = calling("puts");
1930            // The same distance every time, said the way ELF says it: from where the four bytes
1931            // start, with everything else folded in.
1932            text.relocs[0].addend = -4 - i64::from(after);
1933            text.relocs[0].after = after;
1934            text.bytes.resize(6 + after as usize, 0x90);
1935            text.funcs[0].len = text.bytes.len();
1936            let bytes = write(&text, &Data::default(), &[], &windows(), Output::default())
1937                .expect("an object");
1938            let file = object::File::parse(&bytes[..]).expect("a readable object");
1939            let section = file.section_by_name(".text").expect("a text section");
1940            let (_, reloc) = section.relocations().next().expect("the relocation");
1941            assert_eq!(reloc.flags(), RelocationFlags::Coff { typ }, "{after}");
1942            // And the bytes come out holding nothing, because the distance the instruction wants
1943            // and the distance the type already says are the same one.
1944            assert_eq!(inline(&bytes, ".text", 1), 0, "{after}");
1945        }
1946    }
1947
1948    /// The addend a COFF object keeps is in the bytes rather than in the relocation, so the number
1949    /// the caller handed over has to survive the trip through the type.
1950    #[test]
1951    fn a_distance_the_instruction_did_not_ask_for_stays_in_the_bytes() {
1952        let mut text = calling("puts");
1953        text.relocs[0].addend = 12;
1954        let bytes =
1955            write(&text, &Data::default(), &[], &windows(), Output::default()).expect("an object");
1956        assert_eq!(inline(&bytes, ".text", 1), 16, "twelve past the end, which is four past here");
1957    }
1958
1959    #[test]
1960    fn an_address_written_into_an_image_is_the_wide_relocation_here_too() {
1961        let object = Object {
1962            bytes: vec![0; 8],
1963            size: 8,
1964            align: 8,
1965            relocs: vec![Reloc {
1966                at: 0,
1967                symbol: "y".to_owned(),
1968                kind: Reference::Address { bytes: 8 },
1969                addend: 0,
1970                after: 0,
1971            }],
1972            ..variable("p", Place::Written)
1973        };
1974        let data = Data { weak: Vec::new(), objects: vec![object] };
1975        let bytes =
1976            write(&Text::default(), &data, &[], &windows(), Output::default()).expect("an object");
1977        let file = object::File::parse(&bytes[..]).expect("a readable object");
1978        let section = file.section_by_name(".data").expect("a data section");
1979        let (_, reloc) = section.relocations().next().expect("the relocation");
1980        let typ = pe::IMAGE_REL_AMD64_ADDR64;
1981        assert_eq!(reloc.flags(), RelocationFlags::Coff { typ });
1982    }
1983
1984    /// `.data.rel.ro` is an ELF answer to a problem this format solves elsewhere, so both halves of
1985    /// it land in ordinary read only data, which is where the platform's own linker puts them.
1986    #[test]
1987    fn a_variable_the_loader_writes_into_is_read_only_data_here() {
1988        for local in [false, true] {
1989            let data = Data {
1990                weak: Vec::new(),
1991                objects: vec![variable("p", Place::RelocReadOnly { local })],
1992            };
1993            let bytes = write(&Text::default(), &data, &[], &windows(), Output::default())
1994                .expect("an object");
1995            let file = object::File::parse(&bytes[..]).expect("a readable object");
1996            assert!(file.section_by_name(".rdata").is_some(), "{local}");
1997            assert!(file.section_by_name(".data.rel.ro.local").is_none(), "{local}");
1998        }
1999    }
2000
2001    /// No marker and no note, because a PE image says both of those things in the header of the
2002    /// finished image rather than in each of its inputs.
2003    #[test]
2004    fn the_sections_only_elf_reads_are_left_out_rather_than_written_empty() {
2005        let text = calling("puts");
2006        let output = Output { property: Property { features: 3 }, ..Output::default() };
2007        let bytes = write(&text, &Data::default(), &[], &windows(), output).expect("an object");
2008        let file = object::File::parse(&bytes[..]).expect("a readable object");
2009        assert!(file.section_by_name(".note.GNU-stack").is_none());
2010        assert!(file.section_by_name(".note.gnu.property").is_none());
2011    }
2012
2013    /// Each of these is something this format has no way to write, and writing the nearest thing
2014    /// would be worse than refusing: a thread-local variable written as an ordinary one is one copy
2015    /// where the program asked for one per thread, and a constructor list under a name nothing
2016    /// gathers is a program whose constructors never run.
2017    #[test]
2018    fn what_this_format_cannot_say_is_refused_by_name() {
2019        let ordinary = Text::default();
2020        let empty = Data::default();
2021
2022        let mut thread = Data::default();
2023        thread.objects.push(variable("t", Place::Thread { zero: false }));
2024
2025        let mut gathered = Data::default();
2026        gathered.objects.push(variable("c", Place::Named(".init_array".to_owned())));
2027
2028        let mut table = calling("puts");
2029        table.relocs[0].kind = Reference::Got;
2030
2031        let mut room = calling("puts");
2032        room.funcs[0].patch = Some(Patch { at: 0, before: 0 });
2033
2034        let cases: [(&str, &Text, &Data); 4] = [
2035            ("thread-local", &ordinary, &thread),
2036            ("startup", &ordinary, &gathered),
2037            ("table", &table, &empty),
2038            ("patcher", &room, &empty),
2039        ];
2040        for (what, text, data) in cases {
2041            let error = write(text, data, &[], &windows(), Output::default())
2042                .expect_err("something this format cannot write");
2043            assert!(matches!(error, Error::Refused { .. }), "{what}: {error:?}");
2044        }
2045    }
2046
2047    /// A visibility is not refused, because there is nothing to refuse: it is a fact about a dynamic
2048    /// symbol table and a COFF symbol has nowhere to keep one, which is what gcc does on the
2049    /// platform as well.
2050    #[test]
2051    fn a_visibility_this_format_cannot_keep_changes_nothing_rather_than_failing() {
2052        let mut text = calling("puts");
2053        text.funcs[0].visibility = Visibility::Hidden;
2054        let bytes =
2055            write(&text, &Data::default(), &[], &windows(), Output::default()).expect("an object");
2056        let file = object::File::parse(&bytes[..]).expect("a readable object");
2057        let symbol = file.symbols().find(|symbol| symbol.name() == Ok("f")).expect("the function");
2058        assert!(symbol.is_global(), "a name others may use either way");
2059    }
2060
2061    #[test]
2062    fn the_names_a_linker_can_find_are_the_same_list_on_either_format() {
2063        let text = calling("puts");
2064        let data = Data { weak: Vec::new(), objects: vec![variable("shared", Place::Written)] };
2065        let theirs = defines(&text, &data, &[], &windows()).expect("a list");
2066        assert_eq!(theirs, defines(&text, &data, &[], &target()).expect("a list"));
2067    }
2068
2069    /// The same refusal the writer gives, for the reason the function says: an undecorated name is
2070    /// the wrong answer for a format whose symbols carry an underscore, and a wrong index entry is
2071    /// worse than no archive.
2072    #[test]
2073    fn a_platform_this_does_not_write_has_no_list_of_names_either() {
2074        let text = calling("puts");
2075        for triple in [
2076            Triple::new(Arch::Aarch64, Os::Linux, Env::Gnu),
2077            Triple::new(Arch::X86_64, Os::Darwin, Env::Gnu),
2078        ] {
2079            let error = defines(&text, &Data::default(), &[], &TargetInfo::new(triple))
2080                .expect_err("no writer");
2081            assert!(matches!(error, Error::Format { .. }), "{error:?}");
2082        }
2083    }
2084}