Skip to main content

rucc_object/
elf.rs

1//! Relocatable ELF objects.
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 for ELF, 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//! # The marker that has to be there
10//!
11//! `.note.GNU-stack`. A linker that does not find it in every input marks the stack executable,
12//! which section 11.3 calls out as a real and recurring security bug rather than a missing
13//! nicety. It is an empty section and nothing reads its contents, and leaving it out is the kind
14//! of mistake that produces a working program with a weakness in it, so it is written here and a
15//! test says so.
16//!
17//! # What is not here
18//!
19//! Mach-O and COFF. The formats disagree about more than their headers: an Apple symbol carries
20//! an underscore in front of the C name, Mach-O has no way to say how long a function is and
21//! wants `.subsections_via_symbols` instead, and COFF wants storage classes and `.pdata`. Each is
22//! its own piece of work and each is written when the target that needs it is.
23//!
24//! Thread-local storage. Reaching a thread-local variable is a different instruction sequence per
25//! model and the back end writes none of them, so a module carrying one is refused before it
26//! reaches here rather than written as an ordinary variable in the wrong section.
27
28use object::write::{
29    Object as Writer, Relocation, StandardSection, Symbol, SymbolId, SymbolSection,
30};
31use object::{
32    Architecture, BinaryFormat, Endianness, RelocationFlags, SectionFlags, SectionKind,
33    SymbolFlags, SymbolKind, SymbolScope, elf,
34};
35use rucc_target::{ObjectFormat, TargetInfo};
36use rucc_tuple::Arch;
37
38use crate::section::{
39    Alias, Binding, Data, Object, Output, Place, Property, Reference, Reloc, Sections, Text,
40    Visibility,
41};
42
43/// Why an object file could not be written.
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum Error {
46    /// A machine or a platform this does not write objects for.
47    Format {
48        /// The triple that was asked for.
49        triple: String,
50    },
51    /// The writer refused something it was given, which is a bug here rather than in a program.
52    Refused {
53        /// What it said, already formatted.
54        why: String,
55    },
56}
57
58impl std::fmt::Display for Error {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            Error::Format { triple } => {
62                write!(f, "there is no object writer for {triple} in this compiler yet")
63            }
64            Error::Refused { why } => {
65                write!(f, "the object writer refused what it was given: {why}")
66            }
67        }
68    }
69}
70
71impl std::error::Error for Error {}
72
73/// One text section and the variables beside it, as a relocatable ELF object.
74///
75/// # Errors
76///
77/// [`Error::Format`] for a machine or a platform this does not write, and [`Error::Refused`] for
78/// anything the writer underneath objected to, which would be a bug here. An alias whose target
79/// this file does not define is refused the same way, since the front end is what reports that as
80/// a program's mistake and one reaching here means it did not. See [`Error`].
81pub fn write(
82    text: &Text,
83    data: &Data,
84    aliases: &[Alias],
85    target: &TargetInfo,
86    output: Output,
87) -> Result<Vec<u8>, Error> {
88    let Output { sections, property } = output;
89    if target.tuple.arch() != Arch::X86_64 || target.object_format != ObjectFormat::Elf {
90        return Err(Error::Format { triple: target.tuple.to_string() });
91    }
92    let mut obj = Writer::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
93    // The one that holds every function when they are not being split up. Asked for even when it
94    // will stay empty, because it is the section the writer underneath starts a file with anyway
95    // and gcc writes an empty `.text` under `-ffunction-sections` too.
96    let whole = obj.section_id(StandardSection::Text);
97    if !sections.functions {
98        obj.append_section_data(whole, &text.bytes, u64::from(text.align));
99    }
100
101    // Every function defined here, then every variable, then every name either of them wanted that
102    // is not. A name is looked up rather than added twice, because two symbols with one name is
103    // not a file a linker accepts.
104    let mut symbols = std::collections::BTreeMap::new();
105    // Where each function ended up, in the order they were written, so that a relocation inside
106    // one goes into the section that one is in. The same list as `text.funcs` and in the same
107    // order, so the two are walked together below.
108    let mut split = Vec::with_capacity(text.funcs.len());
109    // Which text section each record of where a patcher's room is belongs to, in the order the
110    // records were added, which is the order their headers come out in. See `link`.
111    let mut ordered: Vec<String> = Vec::new();
112    for func in &text.funcs {
113        // A section of its own, holding this function's bytes and nothing else, so the linker can
114        // drop it when nothing reaches it. The name is what gcc writes, and the leading `.text.`
115        // is not decoration: `--gc-sections` and the linker scripts that place code both match on
116        // it, and a section called something else would be placed by the catch all rule.
117        //
118        // The room a patcher was promised in front of the label goes in it too. Those bytes are
119        // the function's, they are just not under its name: the symbol is where the label was and
120        // the room is what came before, so a section holding one without the other would be a
121        // section a linker could place with the room missing.
122        let ahead = func.patch.map_or(0, |patch| patch.before);
123        let (section, at) = if sections.functions {
124            let name = format!(".text.{}", func.name).into_bytes();
125            let id = obj.add_section(Vec::new(), name, SectionKind::Text);
126            let bytes = &text.bytes[func.start - ahead..func.start + func.len];
127            obj.append_section_data(id, bytes, u64::from(func.align.max(1)));
128            (id, ahead as u64)
129        } else {
130            (whole, func.start as u64)
131        };
132        // Where the room is, in a section of its own that says nothing else. What reads it is a
133        // tracer patching every function in an image at once, and what it needs is every address
134        // in one place: a stripped kernel has no symbol table to walk instead, which is the whole
135        // reason the list is written rather than worked out later.
136        //
137        // The address is a relocation rather than a number, because a function is at a fixed
138        // offset in its own section and where that section lands is the linker's answer. It is
139        // written against the section rather than against the function's own name so that it still
140        // points at the room when the room is in front of the name.
141        //
142        // One section per function even when they all point at the same text, which is what gas
143        // produces and what lets a linker throw the record away with the function. `SHF_LINK_ORDER`
144        // is what ties the two together and it needs a section index the writer underneath does not
145        // set, so `link` fills it in afterwards. See `link`.
146        if let Some(patch) = func.patch {
147            let base = if sections.functions { func.start - ahead } else { 0 };
148            let name = PATCHABLE.as_bytes().to_vec();
149            let id = obj.add_section(Vec::new(), name, SectionKind::Data);
150            obj.section_mut(id).flags = SectionFlags::Elf {
151                sh_type: elf::SHT_PROGBITS,
152                sh_flags: elf::SHF_ALLOC | elf::SHF_WRITE | elf::SHF_LINK_ORDER,
153            };
154            obj.append_section_data(id, &[0; 8], 8);
155            let symbol = obj.section_symbol(section);
156            obj.add_relocation(
157                id,
158                Relocation {
159                    offset: 0,
160                    symbol,
161                    addend: (patch.at - base) as i64,
162                    flags: RelocationFlags::Elf { r_type: elf::R_X86_64_64 },
163                },
164            )
165            .map_err(|why| Error::Refused { why: why.to_string() })?;
166            ordered.push(if sections.functions {
167                format!(".text.{}", func.name)
168            } else {
169                ".text".to_owned()
170            });
171        }
172        let id = obj.add_symbol(Symbol {
173            name: func.name.clone().into_bytes(),
174            value: at,
175            size: func.len as u64,
176            kind: SymbolKind::Text,
177            scope: scope_of(func.binding),
178            weak: func.binding == Binding::Weak,
179            section: SymbolSection::Section(section),
180            flags: SymbolFlags::None,
181        });
182        see(&mut obj, id, func.binding, func.visibility);
183        symbols.insert(func.name.clone(), id);
184        split.push(section);
185    }
186
187    // Where each variable's image landed in the section it went into, kept because a relocation in
188    // an image counts from the start of the image and one in a file counts from the start of the
189    // section. A variable that is not in a section has no entry, since nothing in a merged one can
190    // hold a relocation: the linker is being asked for zeroed space rather than for an image.
191    let mut placed = Vec::with_capacity(data.objects.len());
192    // The one section the writer has no name of its own for, remembered so that every variable that
193    // wants it lands in the same one. The rest come back from `section_id`, which already answers
194    // with the section it made the first time it was asked.
195    let mut local = None;
196    for object in &data.objects {
197        let (section, offset) = put(&mut obj, object, &mut local, sections);
198        let id = obj.add_symbol(Symbol {
199            name: object.name.clone().into_bytes(),
200            // A common symbol says what it wants rather than where it is, and what it wants is
201            // recorded where an ordinary symbol records its address.
202            value: if object.place == Place::Merged { object.align } else { offset },
203            size: object.size,
204            kind: SymbolKind::Data,
205            scope: scope_of(object.binding),
206            weak: object.binding == Binding::Weak,
207            section,
208            flags: SymbolFlags::None,
209        });
210        see(&mut obj, id, object.binding, object.visibility);
211        symbols.insert(object.name.clone(), id);
212        placed.push((section.id(), offset));
213    }
214
215    // A second name for something already added, which is where the alias's own binding is the
216    // only thing it does not take from what it points at: the target of one may be a `static` and
217    // the alias of it may not be. Before the loop below rather than after it, because a reference
218    // to the new name is a reference to something this file defines and would otherwise be added
219    // as a name this file wants from somewhere else.
220    for alias in aliases {
221        let Some(&id) = symbols.get(&alias.target) else {
222            let why =
223                format!("'{}' is aliased to '{}', which is not here", alias.name, alias.target);
224            return Err(Error::Refused { why });
225        };
226        let (value, size) = (obj.symbol(id).value, obj.symbol(id).size);
227        let (kind, section) = (obj.symbol(id).kind, obj.symbol(id).section);
228        let id = obj.add_symbol(Symbol {
229            name: alias.name.clone().into_bytes(),
230            value,
231            size,
232            kind,
233            scope: scope_of(alias.binding),
234            weak: alias.binding == Binding::Weak,
235            section,
236            flags: SymbolFlags::None,
237        });
238        see(&mut obj, id, alias.binding, alias.visibility);
239        symbols.insert(alias.name.clone(), id);
240    }
241
242    let wanted = text
243        .relocs
244        .iter()
245        .chain(text.unwind.relocs.iter())
246        .chain(data.objects.iter().flat_map(|object| &object.relocs));
247    for reloc in wanted {
248        if symbols.contains_key(&reloc.symbol) {
249            continue;
250        }
251        let id = obj.add_symbol(Symbol {
252            name: reloc.symbol.clone().into_bytes(),
253            value: 0,
254            size: 0,
255            // What kind of thing an undefined name is is not known here and does not have to be:
256            // a linker resolves an undefined symbol by its name, and the type of one that is not
257            // defined anywhere in this file is nothing this file can say.
258            kind: SymbolKind::Unknown,
259            scope: SymbolScope::Dynamic,
260            weak: false,
261            section: SymbolSection::Undefined,
262            flags: SymbolFlags::None,
263        });
264        symbols.insert(reloc.symbol.clone(), id);
265    }
266
267    for reloc in &text.relocs {
268        // Which function's bytes this one is in, which is the question only the split path has to
269        // ask: when there is one text section every offset in it is already the offset in it.
270        // Every relocation is inside some function, since the padding between two of them is
271        // instructions that do nothing and holds nothing a linker fills in.
272        let (section, at) = if sections.functions {
273            let after = text.funcs.partition_point(|func| func.start <= reloc.at);
274            let Some(func) = after.checked_sub(1).map(|i| &text.funcs[i]) else {
275                let why = format!("a relocation at {} is in front of every function", reloc.at);
276                return Err(Error::Refused { why });
277            };
278            // From the start of the section rather than from the symbol, and the two are not the
279            // same byte in a function with room in front of its label.
280            let base = func.start - func.patch.map_or(0, |patch| patch.before);
281            (split[after - 1], (reloc.at - base) as u64)
282        } else {
283            (whole, reloc.at as u64)
284        };
285        add(&mut obj, section, at, reloc, &symbols)?;
286    }
287
288    // The unwind table, if there is one. Its own section rather than part of the text, because it
289    // is read rather than run: the loader maps it and the linker gathers every input's into one
290    // table and builds the index the unwinder binary searches. Eight, because a record is looked
291    // up by address at a point where the program is usually already crashing and an unaligned read
292    // there is a second fault on top of the first.
293    if !text.unwind.bytes.is_empty() {
294        let frames = obj.add_section(Vec::new(), b".eh_frame".to_vec(), SectionKind::ReadOnlyData);
295        obj.append_section_data(frames, &text.unwind.bytes, 8);
296        for reloc in &text.unwind.relocs {
297            add(&mut obj, frames, reloc.at as u64, reloc, &symbols)?;
298        }
299    }
300    for (object, &(section, offset)) in data.objects.iter().zip(&placed) {
301        let Some(section) = section else { continue };
302        for reloc in &object.relocs {
303            add(&mut obj, section, offset + reloc.at as u64, reloc, &symbols)?;
304        }
305    }
306
307    // What the file was built to have checked, when it was built to have anything checked. Left
308    // out otherwise rather than written as a zero, because a linker treats a missing note and a
309    // note with no bits in it the same way and gcc writes nothing.
310    if property.any() {
311        let note = obj.section_id(StandardSection::GnuProperty);
312        obj.append_section_data(note, &record(property), 8);
313    }
314
315    // Written as an empty note rather than left out, because a linker that does not find it in
316    // every input marks the stack executable.
317    obj.add_section(Vec::new(), b".note.GNU-stack".to_vec(), SectionKind::Metadata);
318
319    let mut bytes = obj.write().map_err(|why| Error::Refused { why: why.to_string() })?;
320    link(&mut bytes, &ordered);
321    Ok(bytes)
322}
323
324/// What a record of where a patcher's room is is called.
325const PATCHABLE: &str = "__patchable_function_entries";
326
327/// Ties each record of where a patcher's room is to the text it is a record of.
328///
329/// `SHF_LINK_ORDER` says a section belongs to another one, and which one is `sh_link`, a section
330/// index. The writer underneath has no way to say it: it writes a zero into every ordinary
331/// section's `sh_link` and offers nothing that would change one. A zero there is not harmless,
332/// since a linker reads a section that claims to be ordered after nothing as an error, so the
333/// number is written into the finished bytes here.
334///
335/// Ordinary sections come out in the order they were added, so the records are found by name in
336/// header order and paired with the text sections they were added beside, in the same order.
337/// `ordered` is that list, and the target is looked up by name because a text section's name is
338/// unique in a file even though a record's is not.
339///
340/// A file with no records is left alone, which is nearly every file.
341fn link(bytes: &mut [u8], ordered: &[String]) {
342    if ordered.is_empty() {
343        return;
344    }
345    let word = |bytes: &[u8], at: usize| u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap());
346    let short = |bytes: &[u8], at: usize| u16::from_le_bytes(bytes[at..at + 2].try_into().unwrap());
347    let long = |bytes: &[u8], at: usize| u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap());
348    // Where the section headers are, how far apart they are and how many of them there are. A
349    // file with more than there is room to say puts the count in the first header instead, which
350    // this never writes: it would take sixty five thousand sections, and a section here is a
351    // function.
352    let headers = word(bytes, 0x28) as usize;
353    let step = short(bytes, 0x3a) as usize;
354    let count = short(bytes, 0x3c) as usize;
355    let strings = word(bytes, headers + short(bytes, 0x3e) as usize * step + 24) as usize;
356    let name = |bytes: &[u8], header: usize| {
357        let at = strings + long(bytes, header) as usize;
358        let end = bytes[at..].iter().position(|byte| *byte == 0).map_or(at, |len| at + len);
359        String::from_utf8_lossy(&bytes[at..end]).into_owned()
360    };
361    let names: Vec<String> = (0..count).map(|i| name(bytes, headers + i * step)).collect();
362    let mut wanted = ordered.iter();
363    for (i, section) in names.iter().enumerate() {
364        if section != PATCHABLE {
365            continue;
366        }
367        let Some(target) = wanted.next() else { break };
368        let Some(at) = names.iter().position(|name| name == target) else { continue };
369        let at = u32::try_from(at).expect("a file with this many sections in it");
370        let sh_link = headers + i * step + 40;
371        bytes[sh_link..sh_link + 4].copy_from_slice(&at.to_le_bytes());
372    }
373    debug_assert!(wanted.next().is_none(), "a record whose header nothing found");
374}
375
376/// The note that says what the file was built to have checked.
377///
378/// A note is a name, a description and a number saying what kind it is, and this kind is the one
379/// whose description is a list of properties. Each property is a key, a length and that many bytes,
380/// and the one written here is the feature word.
381///
382/// Everything is padded to eight rather than to four, which is what a note in a sixty four bit
383/// object is aligned to and what makes the reader's walk over the list a walk over aligned words.
384/// The two lengths in the header count the padding after what they measure, which is why the
385/// description is sixteen bytes for a property of twelve.
386fn record(property: Property) -> Vec<u8> {
387    // How long the name is, how long the description is, and which kind of note this is. Then the
388    // name, and then the description, which is the one property and the four bytes that pad it.
389    let head = [4, 16, elf::NT_GNU_PROPERTY_TYPE_0.0];
390    let desc = [Property::X86_FEATURES, 4, property.features, 0];
391    let mut out = Vec::with_capacity(32);
392    for word in head {
393        out.extend_from_slice(&word.to_le_bytes());
394    }
395    // Twelve bytes in and already a multiple of eight, so the description begins straight after the
396    // name with no padding between them.
397    out.extend_from_slice(b"GNU\0");
398    for word in desc {
399        out.extend_from_slice(&word.to_le_bytes());
400    }
401    out
402}
403
404/// One variable's image into the section it belongs in, and where in that section it landed.
405///
406/// A zero filled variable takes as many bytes of the file as it is long on the way in and none on
407/// the way out, which is the whole point of the section it goes in. A merged one goes in no section
408/// at all: the linker is being asked for that much zeroed space under that name, and where it ends
409/// up is the linker's answer rather than this file's.
410fn put(
411    obj: &mut Writer<'_>,
412    object: &Object,
413    local: &mut Option<object::write::SectionId>,
414    sections: Sections,
415) -> (SymbolSection, u64) {
416    // A section of its own, named after the variable and after the section it would have gone in,
417    // which is what `-fdata-sections` asks for. A merged variable has no section to split and a
418    // named one was named by the program, so both are left where they are: the first is a request
419    // to the linker rather than an image, and the second would otherwise have the flag silently
420    // overrule what the source said.
421    if sections.data {
422        if let Some(name) = object.place.split(&object.name) {
423            let section = obj.add_section(Vec::new(), name.into_bytes(), kind_of(&object.place));
424            let offset = if object.place == Place::Zero {
425                obj.append_section_bss(section, object.size, object.align)
426            } else {
427                obj.append_section_data(section, &object.bytes, object.align)
428            };
429            return (SymbolSection::Section(section), offset);
430        }
431    }
432    let section = match &object.place {
433        Place::Written => obj.section_id(StandardSection::Data),
434        Place::ReadOnly => obj.section_id(StandardSection::ReadOnlyData),
435        // Read only after the loader has written it, which the writer knows as the relocatable
436        // read only data section and which is `.data.rel.ro` on ELF. The `.local` half is a layout
437        // hint the writer has no name for, so it is added by hand and remembered: asking again
438        // would make a second section with the same name, and a file with one of those per variable
439        // is a file whose section headers outweigh what they describe.
440        Place::RelocReadOnly { local: false } => {
441            obj.section_id(StandardSection::ReadOnlyDataWithRel)
442        }
443        Place::RelocReadOnly { local: true } => *local.get_or_insert_with(|| {
444            obj.add_section(
445                Vec::new(),
446                b".data.rel.ro.local".to_vec(),
447                SectionKind::ReadOnlyDataWithRel,
448            )
449        }),
450        Place::Zero => obj.section_id(StandardSection::UninitializedData),
451        Place::Merged => return (SymbolSection::Common, 0),
452        // A named section is the program's word for where this goes, and a program that names one
453        // wants what it named rather than what would have been chosen. It is written as ordinary
454        // data because nothing in the IR says otherwise.
455        Place::Named(name) => {
456            obj.add_section(Vec::new(), name.clone().into_bytes(), SectionKind::Data)
457        }
458    };
459    let offset = if object.place == Place::Zero {
460        obj.append_section_bss(section, object.size, object.align)
461    } else {
462        obj.append_section_data(section, &object.bytes, object.align)
463    };
464    (SymbolSection::Section(section), offset)
465}
466
467/// What a section split off for one variable is, which is what the section it was split off from
468/// was.
469///
470/// Splitting changes the name and nothing else. A variable that was going to be in a page the
471/// loader maps read only is still in one, and a zero filled variable still costs the file nothing,
472/// so the flags a linker reads off the section header have to come out the same as they would
473/// have. The two kinds with no section of their own never reach here, and `Data` for them is a
474/// value that is never used rather than a claim about either.
475fn kind_of(place: &Place) -> SectionKind {
476    match place {
477        Place::ReadOnly => SectionKind::ReadOnlyData,
478        Place::RelocReadOnly { .. } => SectionKind::ReadOnlyDataWithRel,
479        Place::Zero => SectionKind::UninitializedData,
480        Place::Written | Place::Merged | Place::Named(_) => SectionKind::Data,
481    }
482}
483
484/// One relocation, `at` bytes into the section it ended up in.
485///
486/// The offset is worked out by the caller rather than here, because the two callers count from
487/// different places: a relocation in an image counts from the start of that image and a relocation
488/// in a function counts from the start of that function, and neither of those is where the section
489/// begins once something else is in front of it.
490fn add(
491    obj: &mut Writer<'_>,
492    section: object::write::SectionId,
493    at: u64,
494    reloc: &Reloc,
495    symbols: &std::collections::BTreeMap<String, SymbolId>,
496) -> Result<(), Error> {
497    let r_type = r_type(reloc.kind)
498        .ok_or_else(|| Error::Refused { why: format!("no relocation is {:?}", reloc.kind) })?;
499    obj.add_relocation(
500        section,
501        Relocation {
502            offset: at,
503            symbol: symbols[&reloc.symbol],
504            addend: reloc.addend,
505            flags: RelocationFlags::Elf { r_type },
506        },
507    )
508    .map_err(|why| Error::Refused { why: why.to_string() })
509}
510
511/// How far a name reaches, which is the one thing about a symbol ELF calls its binding.
512///
513/// `SymbolScope` is two facts in one word, and the trap is that the middle one is not the neutral
514/// answer it reads as. The writer turns `Compilation` into a local symbol, and it turns the choice
515/// between `Linkage` and `Dynamic` into `st_other`: `Linkage` is `STV_HIDDEN` and `Dynamic` is
516/// `STV_DEFAULT`. So there is no way to say global and decline to say anything about visibility,
517/// and picking the one whose name sounds like the smaller claim is picking hidden. That is what
518/// tamnd/rucc#733 was.
519///
520/// `Dynamic` is what every global asks for here, and the visibility is said afterwards by
521/// [`see`] rather than through this, so that nothing about `st_other` depends on reading one of
522/// these four names the way its author meant it.
523fn scope_of(binding: Binding) -> SymbolScope {
524    match binding {
525        Binding::Local => SymbolScope::Compilation,
526        Binding::Global | Binding::Weak => SymbolScope::Dynamic,
527    }
528}
529
530/// Say what `st_other` is for a symbol that has just been added, rather than leave it to be
531/// inferred from the scope.
532///
533/// The writer underneath fills `st_info` in from the kind, the binding and whether the symbol is
534/// defined, and there is nothing to add to that. `st_other` is the field this compiler has an
535/// opinion about and the field the `SymbolScope` mapping got wrong, so it is written here in the
536/// two bits ELF puts the visibility in and the rest of the byte is left as it was found.
537///
538/// A local symbol is left alone. Its visibility means nothing, since a name the static link has
539/// already finished with cannot be in a dynamic symbol table whatever `st_other` says, and gcc
540/// writes `STV_DEFAULT` for one, which is what the writer underneath produces on its own.
541fn see(obj: &mut Writer<'_>, id: SymbolId, binding: Binding, visibility: Visibility) {
542    if binding == Binding::Local {
543        return;
544    }
545    let wanted = match visibility {
546        Visibility::Default => elf::STV_DEFAULT,
547        Visibility::Hidden => elf::STV_HIDDEN,
548        Visibility::Protected => elf::STV_PROTECTED,
549    };
550    if let SymbolFlags::Elf { st_other, .. } = obj.symbol_flags_mut(id) {
551        *st_other = st_other.with_visibility(wanted);
552    }
553}
554
555/// Which relocation of this machine one reference is, and nothing for one this machine has none of.
556///
557/// The first three are the distance from the end of an instruction to something, and they differ in
558/// what the linker is allowed to do about it. A call may go through a stub, which is what lets a
559/// call reach a symbol further away than four bytes can say and what makes a call to a shared
560/// library work at all. A load may not, because there is nowhere to put a stub that a load would
561/// read, so a load of something another object may define reads a table slot the linker fills in
562/// instead, and the relaxing form of the relocation lets the linker undo that when it turns out
563/// nobody else defines it. The fourth is the address itself, at the two widths this machine writes
564/// one at.
565fn r_type(reference: Reference) -> Option<elf::RelocationType> {
566    Some(match reference {
567        Reference::Call => elf::R_X86_64_PLT32,
568        Reference::Data => elf::R_X86_64_PC32,
569        Reference::Got => elf::R_X86_64_REX_GOTPCRELX,
570        Reference::Address { bytes: 8 } => elf::R_X86_64_64,
571        Reference::Address { bytes: 4 } => elf::R_X86_64_32,
572        Reference::Address { .. } => return None,
573    })
574}
575
576#[cfg(test)]
577mod tests {
578    use super::*;
579
580    use object::read::elf::Sym as _;
581    use object::read::{Object as _, ObjectSection as _, ObjectSymbol as _};
582    use rucc_target::{Arch, Env, Os, Triple};
583
584    use crate::section::{Extent, Patch, Reloc};
585
586    /// A linux x86-64 target, which is the only one this writes.
587    fn target() -> TargetInfo {
588        TargetInfo::new(Triple::new(Arch::X86_64, Os::Linux, Env::Gnu))
589    }
590
591    /// One function of that name, at that offset, that many bytes long, and visible that far.
592    ///
593    /// Visibility is the field these cases mostly have no opinion about, so it is the one the
594    /// helper fills in and the two that do have an opinion write for themselves.
595    fn extent(name: String, start: usize, len: usize, binding: Binding) -> Extent {
596        Extent {
597            name,
598            start,
599            len,
600            align: crate::FUNC_ALIGN,
601            binding,
602            visibility: Visibility::Default,
603            patch: None,
604        }
605    }
606
607    /// A call to something outside the file, which is the shape every case here starts from.
608    fn calling(name: &str) -> Text {
609        Text {
610            bytes: vec![0xe8, 0, 0, 0, 0, 0xc3],
611            funcs: vec![extent("f".to_owned(), 0, 6, Binding::Global)],
612            relocs: vec![Reloc {
613                at: 1,
614                symbol: name.to_owned(),
615                kind: Reference::Call,
616                addend: -4,
617            }],
618            ..Text::default()
619        }
620    }
621
622    #[test]
623    fn the_bytes_come_back_out_of_the_section_they_went_into() {
624        let text = calling("puts");
625        let bytes =
626            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
627        let file = object::File::parse(&bytes[..]).expect("a readable object");
628        let section = file.section_by_name(".text").expect("a text section");
629        assert_eq!(section.data().expect("the bytes"), &text.bytes[..]);
630    }
631
632    #[test]
633    fn a_function_is_a_symbol_that_says_where_it_is_and_how_long_it_is() {
634        let mut text = calling("puts");
635        text.funcs.push(extent("g".to_owned(), 16, 1, Binding::Global));
636        text.bytes.resize(17, 0x90);
637        let bytes =
638            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
639        let file = object::File::parse(&bytes[..]).expect("a readable object");
640        let g = file.symbols().find(|s| s.name() == Ok("g")).expect("the second function");
641        assert_eq!(g.address(), 16);
642        assert_eq!(g.size(), 1);
643        assert_eq!(g.kind(), SymbolKind::Text);
644        assert!(g.is_global(), "nothing said otherwise about this one");
645    }
646
647    #[test]
648    fn a_function_no_other_file_can_see_is_a_local_symbol() {
649        let mut text = calling("puts");
650        text.funcs.push(extent("hidden".to_owned(), 16, 1, Binding::Local));
651        text.funcs.push(extent("shared".to_owned(), 32, 1, Binding::Weak));
652        text.bytes.resize(33, 0x90);
653        let bytes =
654            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
655        let file = object::File::parse(&bytes[..]).expect("a readable object");
656        let hidden = file.symbols().find(|s| s.name() == Ok("hidden")).expect("the static one");
657        // A symbol the linker keeps and does not let another file reach, which is the whole of
658        // what `static` on a function means and what two files each defining their own need.
659        assert!(hidden.is_local(), "a static function must not be offered to the linker");
660        assert!(!hidden.is_weak());
661        let shared = file.symbols().find(|s| s.name() == Ok("shared")).expect("the weak one");
662        assert!(shared.is_weak(), "a weak function has to be able to lose");
663        assert!(shared.is_global());
664    }
665
666    /// A global is `STV_DEFAULT`, so a shared library built from these objects exports something.
667    ///
668    /// The bug in tamnd/rucc#733. Every global came out `STV_HIDDEN`, which a static link does not
669    /// look at, so nothing here noticed and SQLite linked and ran and the whole test suite passed.
670    /// What it costs is the dynamic symbol table: `gcc -shared` over one of these objects produced
671    /// a library with an empty one, and `dlsym` could not find a function the file plainly defines.
672    ///
673    /// Written against `st_other` itself rather than against the reader's `scope`, because `scope`
674    /// is the word that was misread in the first place and a test that asks it the same question
675    /// would agree with whatever the writer did.
676    /// The record of where a patcher's room is, and what it says about it.
677    ///
678    /// Four things have to be right at once for a linker to take it: the flags, the alignment, the
679    /// relocation and the section it says it is ordered after. The last of those is the one the
680    /// writer underneath cannot say, so a zero there would be a file `ld` refuses and a test that
681    /// only looked at the bytes would not see it.
682    #[test]
683    fn where_a_patcher_may_write_is_recorded_in_a_section_tied_to_the_code_it_is_about() {
684        let mut text = calling("puts");
685        text.bytes.splice(0..0, [0x90, 0x90, 0x90]);
686        text.funcs[0].start = 3;
687        text.funcs[0].patch = Some(Patch { at: 0, before: 3 });
688        text.relocs[0].at = 4;
689        let bytes =
690            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
691        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
692        let section = file.section_by_name(PATCHABLE).expect("a record of the room");
693        assert_eq!(section.size(), 8, "one address, and this file defines one function");
694        assert_eq!(section.align(), 8);
695        let header = section.elf_section_header();
696        assert_eq!(
697            header.sh_flags.get(Endianness::Little),
698            elf::SHF_ALLOC | elf::SHF_WRITE | elf::SHF_LINK_ORDER
699        );
700        // Which is the whole point of the fixup: the index has to be the text section's own, and
701        // the writer underneath had written a zero there.
702        let index = file.section_by_name(".text").expect("a text section").index().0;
703        assert_eq!(header.sh_link.get(Endianness::Little) as usize, index);
704        assert_ne!(index, 0);
705
706        // And the address, which is the front of the room rather than the function's own symbol.
707        let [(at, reloc)] = &section.relocations().collect::<Vec<_>>()[..] else {
708            panic!("one address in the record")
709        };
710        assert_eq!(*at, 0);
711        assert_eq!(reloc.addend(), 0);
712        assert_eq!(reloc.flags(), RelocationFlags::Elf { r_type: elf::R_X86_64_64 });
713    }
714
715    /// And a file that asked for none has no such section, which is nearly every file.
716    #[test]
717    fn a_file_that_promised_a_patcher_nothing_records_nothing() {
718        let text = calling("puts");
719        let bytes =
720            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
721        let file = object::File::parse(&bytes[..]).expect("a readable object");
722        assert!(file.section_by_name(PATCHABLE).is_none());
723    }
724
725    /// The same when each function is a section of its own, which is what a kernel builds with.
726    ///
727    /// Each record then points at a different section, which is what makes the pairing worth
728    /// asserting: getting it backwards would still produce a file every tool reads and every
729    /// address in it would be about the wrong function.
730    #[test]
731    fn each_record_is_tied_to_its_own_function_when_they_are_split_up() {
732        let mut text = calling("puts");
733        text.funcs[0].patch = Some(Patch { at: 0, before: 0 });
734        text.funcs.push(extent("g".to_owned(), 16, 1, Binding::Global));
735        text.funcs[1].patch = Some(Patch { at: 16, before: 0 });
736        text.bytes.resize(17, 0x90);
737        let output =
738            Output { sections: Sections { functions: true, data: false }, ..Output::default() };
739        let bytes = write(&text, &Data::default(), &[], &target(), output).expect("an object");
740        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
741        let links: Vec<usize> = file
742            .sections()
743            .filter(|section| section.name() == Ok(PATCHABLE))
744            .map(|section| section.elf_section_header().sh_link.get(Endianness::Little) as usize)
745            .collect();
746        let index = |name: &str| file.section_by_name(name).expect("a text section").index().0;
747        assert_eq!(links, [index(".text.f"), index(".text.g")]);
748    }
749
750    #[test]
751    fn a_global_is_visible_to_the_dynamic_linker_and_a_static_one_is_not_a_symbol_at_all() {
752        let mut text = calling("puts");
753        text.funcs.push(extent("g".to_owned(), 16, 1, Binding::Global));
754        text.funcs.push(extent("w".to_owned(), 32, 1, Binding::Weak));
755        text.funcs.push(extent("s".to_owned(), 48, 1, Binding::Local));
756        text.bytes.resize(49, 0x90);
757        let bytes =
758            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
759        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
760        let visibility = |name: &str| {
761            file.symbols()
762                .find(|s| s.name() == Ok(name))
763                .expect("the function")
764                .elf_symbol()
765                .st_visibility()
766        };
767        // Nothing said hidden about either of these, so neither is.
768        assert_eq!(visibility("g"), elf::STV_DEFAULT);
769        assert_eq!(visibility("w"), elf::STV_DEFAULT, "a weak one is still a name others may use");
770        // The `static` one is local, and a local symbol's visibility means nothing either way,
771        // which is why the binding is what this asks about.
772        assert_eq!(visibility("s"), elf::STV_DEFAULT);
773    }
774
775    /// And the other direction: a name that did ask to be hidden is hidden, and a protected one is
776    /// protected.
777    ///
778    /// The half of tamnd/rucc#733 that the fix above left open. Saying `STV_DEFAULT` for everything
779    /// is right for everything nobody marked and wrong the moment something is marked, so the two
780    /// tests together are what says the field carries an answer rather than a constant.
781    ///
782    /// Both are asked of a function and of a variable, because they are added by two different
783    /// loops in `write` and a field one of them fills in is not a field the other one does.
784    #[test]
785    fn a_name_that_asked_to_be_hidden_is_hidden_and_a_protected_one_is_protected() {
786        let mut text = calling("puts");
787        for (index, (name, seen)) in
788            [("h", Visibility::Hidden), ("p", Visibility::Protected)].into_iter().enumerate()
789        {
790            let mut func = extent(name.to_owned(), 16 + index * 16, 1, Binding::Global);
791            func.visibility = seen;
792            text.funcs.push(func);
793        }
794        text.bytes.resize(49, 0x90);
795        let mut data = Data::default();
796        for (name, seen) in [("vh", Visibility::Hidden), ("vp", Visibility::Protected)] {
797            let mut object = variable(name, Place::Written);
798            object.visibility = seen;
799            data.objects.push(object);
800        }
801        let bytes = write(&text, &data, &[], &target(), Output::default()).expect("an object");
802        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
803        let visibility = |name: &str| {
804            file.symbols()
805                .find(|s| s.name() == Ok(name))
806                .expect("the symbol")
807                .elf_symbol()
808                .st_visibility()
809        };
810        assert_eq!(visibility("h"), elf::STV_HIDDEN);
811        assert_eq!(visibility("p"), elf::STV_PROTECTED);
812        assert_eq!(visibility("vh"), elf::STV_HIDDEN, "a variable goes through a second loop");
813        assert_eq!(visibility("vp"), elf::STV_PROTECTED);
814        // The one thing a visibility must not disturb, since `st_info` and `st_other` are written
815        // in one go and the second was set after the first.
816        let h = file.symbols().find(|s| s.name() == Ok("h")).expect("the function");
817        assert!(h.is_global(), "hidden is about the dynamic linker and not about the binding");
818        assert_eq!(h.size(), 1, "and it is still a function of the length it was");
819    }
820
821    #[test]
822    fn a_name_this_file_does_not_define_is_left_for_the_linker_to_find() {
823        let bytes = write(&calling("puts"), &Data::default(), &[], &target(), Output::default())
824            .expect("an object");
825        let file = object::File::parse(&bytes[..]).expect("a readable object");
826        let puts = file.symbols().find(|s| s.name() == Ok("puts")).expect("the callee");
827        assert!(puts.is_undefined(), "the file does not define it and must not claim to");
828    }
829
830    #[test]
831    fn a_call_asks_for_the_relocation_a_stub_may_answer_and_a_load_asks_for_the_one_that_may_not() {
832        for (reference, wanted) in [
833            (Reference::Call, elf::R_X86_64_PLT32),
834            (Reference::Data, elf::R_X86_64_PC32),
835            (Reference::Got, elf::R_X86_64_REX_GOTPCRELX),
836        ] {
837            let mut text = calling("puts");
838            text.relocs[0].kind = reference;
839            let bytes = write(&text, &Data::default(), &[], &target(), Output::default())
840                .expect("an object");
841            let file = object::File::parse(&bytes[..]).expect("a readable object");
842            let section = file.section_by_name(".text").expect("a text section");
843            let (offset, reloc) = section.relocations().next().expect("one relocation");
844            assert_eq!(offset, 1);
845            assert_eq!(reloc.addend(), -4);
846            assert_eq!(reloc.flags(), RelocationFlags::Elf { r_type: wanted });
847        }
848    }
849
850    #[test]
851    fn a_name_wanted_twice_is_one_symbol_rather_than_two() {
852        let mut text = calling("puts");
853        text.relocs.push(Reloc {
854            at: 1,
855            symbol: "puts".to_owned(),
856            kind: Reference::Call,
857            addend: -4,
858        });
859        let bytes =
860            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
861        let file = object::File::parse(&bytes[..]).expect("a readable object");
862        assert_eq!(file.symbols().filter(|s| s.name() == Ok("puts")).count(), 1);
863    }
864
865    #[test]
866    fn a_function_that_is_also_called_is_not_a_second_symbol() {
867        let text = calling("f");
868        let bytes =
869            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
870        let file = object::File::parse(&bytes[..]).expect("a readable object");
871        let mut found = file.symbols().filter(|s| s.name() == Ok("f"));
872        let f = found.next().expect("the function");
873        assert!(!f.is_undefined(), "the file defines it");
874        assert!(found.next().is_none(), "and defines it once");
875    }
876
877    #[test]
878    fn the_marker_that_says_the_stack_is_not_executable_is_written() {
879        let bytes = write(&calling("puts"), &Data::default(), &[], &target(), Output::default())
880            .expect("an object");
881        let file = object::File::parse(&bytes[..]).expect("a readable object");
882        let note = file.section_by_name(".note.GNU-stack").expect("the marker");
883        assert!(note.data().expect("no bytes").is_empty());
884    }
885
886    /// What the file says it was built to have checked, byte for byte.
887    ///
888    /// Written against the bytes rather than against a reader, because the two lengths in the
889    /// header count the padding after what they measure and a note whose lengths are one word out
890    /// is one a linker drops without saying anything. What comes of that is a program the loader
891    /// leaves the check turned off for, which is a build that looks like it worked.
892    #[test]
893    fn the_note_that_says_what_the_file_was_built_to_have_checked_is_written() {
894        let property = Property { features: Property::IBT | Property::SHSTK };
895        let output = Output { property, ..Output::default() };
896        let bytes =
897            write(&calling("puts"), &Data::default(), &[], &target(), output).expect("an object");
898        let file = object::File::parse(&bytes[..]).expect("a readable object");
899        let note = file.section_by_name(".note.gnu.property").expect("the note");
900        assert_eq!(note.align(), 8, "a note in a sixty four bit object is read a word at a time");
901        let want: Vec<u8> = [
902            4u32,
903            16,
904            5,
905            u32::from_le_bytes(*b"GNU\0"),
906            Property::X86_FEATURES,
907            4,
908            Property::IBT | Property::SHSTK,
909            0,
910        ]
911        .iter()
912        .flat_map(|word| word.to_le_bytes())
913        .collect();
914        assert_eq!(note.data().expect("the bytes"), &want[..]);
915    }
916
917    /// And nothing at all when the file was built to have nothing checked.
918    ///
919    /// A note with an empty feature word and no note are the same thing to a linker, which drops
920    /// the whole property when any input lacks it. gcc writes nothing, so a section header that
921    /// describes nothing would be the one difference between the two compilers' objects.
922    #[test]
923    fn a_file_built_to_have_nothing_checked_says_nothing() {
924        let bytes = write(&calling("puts"), &Data::default(), &[], &target(), Output::default())
925            .expect("an object");
926        let file = object::File::parse(&bytes[..]).expect("a readable object");
927        assert!(file.section_by_name(".note.gnu.property").is_none());
928    }
929
930    /// Every unwind record names the function it is about, and each name goes where it is in the
931    /// table rather than at the start of it.
932    ///
933    /// Written because working the offset out is the caller's job here, which is what the two text
934    /// paths differ about, and a third caller that let it default to nothing would put every record
935    /// in the table on the same function. Nothing else would notice: the section is the right
936    /// length, the symbols are right, the link succeeds, and what comes of it is an unwinder that
937    /// walks out of the wrong frame the first time something throws or a backtrace is taken.
938    #[test]
939    fn an_unwind_record_names_the_function_it_is_about_and_not_the_first_one() {
940        let mut text = calling("puts");
941        text.funcs.push(extent("g".to_owned(), 16, 1, Binding::Global));
942        text.bytes.resize(17, 0x90);
943        // A shared header and two records, whose contents nothing here reads: what is being asked
944        // is where in them each name landed.
945        text.unwind.bytes = vec![0; 64];
946        for (at, name) in [(32usize, "f"), (48usize, "g")] {
947            text.unwind.relocs.push(Reloc {
948                at,
949                symbol: name.to_owned(),
950                kind: Reference::Address { bytes: 8 },
951                addend: 0,
952            });
953        }
954        let bytes =
955            write(&text, &Data::default(), &[], &target(), Output::default()).expect("an object");
956        let file = object::File::parse(&bytes[..]).expect("a readable object");
957        let frames = file.section_by_name(".eh_frame").expect("the table");
958        let mut at = frames.relocations().map(|(offset, _)| offset).collect::<Vec<_>>();
959        at.sort_unstable();
960        assert_eq!(at, [32, 48]);
961    }
962
963    /// The name of the section that symbol is defined in.
964    fn lives_in<'a>(file: &'a object::File<'a>, name: &str) -> String {
965        let symbol = file.symbols().find(|s| s.name() == Ok(name)).expect("the symbol");
966        let index = symbol.section_index().expect("a section to be defined in");
967        let section = file.section_by_index(index).expect("a readable section");
968        section.name().expect("a named section").to_owned()
969    }
970
971    /// Two functions, the second of them sixteen bytes in and calling something outside the file.
972    fn two() -> Text {
973        let mut text = calling("puts");
974        // Padded to where the second one is aligned to, with the instruction that does nothing,
975        // because the space in front of a function is reached by falling off the end of one.
976        text.bytes.resize(16, 0x90);
977        text.bytes.extend_from_slice(&[0xe8, 0, 0, 0, 0, 0xc3]);
978        text.funcs.push(extent("g".to_owned(), 16, 6, Binding::Global));
979        text.relocs.push(Reloc {
980            at: 17,
981            symbol: "puts".to_owned(),
982            kind: Reference::Call,
983            addend: -4,
984        });
985        text
986    }
987
988    /// What `-ffunction-sections` comes down to in an object file, which is the flag that makes
989    /// `--gc-sections` able to drop anything: a linker can leave out a section nothing reaches and
990    /// cannot leave out half of one.
991    ///
992    /// The empty `.text` stays, because it is the section the writer underneath opens a file with
993    /// and gcc 16 leaves an empty one behind under the flag too.
994    #[test]
995    fn every_function_gets_a_section_of_its_own_when_that_is_what_was_asked_for() {
996        let sections =
997            Output { sections: Sections { functions: true, data: false }, ..Output::default() };
998        let bytes = write(&two(), &Data::default(), &[], &target(), sections).expect("an object");
999        let file = object::File::parse(&bytes[..]).expect("a readable object");
1000        assert_eq!(lives_in(&file, "f"), ".text.f");
1001        assert_eq!(lives_in(&file, "g"), ".text.g");
1002        assert!(file.section_by_name(".text").expect("the empty one").size() == 0);
1003        // Each one at nothing into its own section, and as long as it was: a function alone in a
1004        // section starts where the section does, whatever it started at when they shared one.
1005        for name in ["f", "g"] {
1006            let symbol = file.symbols().find(|s| s.name() == Ok(name)).expect("the function");
1007            assert_eq!(symbol.address(), 0, "{name}");
1008            assert_eq!(symbol.size(), 6, "{name}");
1009        }
1010        let section = file.section_by_name(".text.g").expect("the second function");
1011        assert_eq!(section.data().expect("the bytes"), &[0xe8, 0, 0, 0, 0, 0xc3]);
1012        // The padding between the two is gone with them, since it was there to align the second
1013        // one inside a section they shared and each section is aligned by the linker now.
1014        assert_eq!(section.align(), u64::from(crate::FUNC_ALIGN));
1015    }
1016
1017    /// A relocation counts from the start of whichever section its function ended up in, which is
1018    /// the arithmetic the split path has to do and the unsplit one never does.
1019    ///
1020    /// Getting it wrong is a call patched over the wrong bytes, which assembles, links, and jumps
1021    /// into the middle of an instruction at run time.
1022    #[test]
1023    fn a_relocation_moves_with_the_function_whose_bytes_it_is_in() {
1024        let sections =
1025            Output { sections: Sections { functions: true, data: false }, ..Output::default() };
1026        let bytes = write(&two(), &Data::default(), &[], &target(), sections).expect("an object");
1027        let file = object::File::parse(&bytes[..]).expect("a readable object");
1028        for name in [".text.f", ".text.g"] {
1029            let section = file.section_by_name(name).expect("a function");
1030            let (offset, _) = section.relocations().next().expect("the call in it");
1031            // One byte in either way, because the call is the first instruction of both and the
1032            // opcode is one byte in front of the address the linker fills in.
1033            assert_eq!(offset, 1, "{name}");
1034            assert_eq!(section.relocations().count(), 1, "{name}");
1035        }
1036    }
1037
1038    /// One variable of four bytes, in whichever section its own answer puts it.
1039    fn variable(name: &str, place: Place) -> Object {
1040        Object {
1041            name: name.to_owned(),
1042            bytes: if place == Place::Zero { Vec::new() } else { vec![1, 0, 0, 0] },
1043            size: 4,
1044            align: 4,
1045            place,
1046            binding: Binding::Global,
1047            visibility: Visibility::Default,
1048            relocs: Vec::new(),
1049        }
1050    }
1051
1052    /// A file of that one variable and nothing else.
1053    fn holding(object: Object) -> Vec<u8> {
1054        let data = Data { objects: vec![object] };
1055        write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object")
1056    }
1057
1058    #[test]
1059    fn what_a_variable_is_decides_which_section_it_goes_in() {
1060        for (place, wanted) in [
1061            (Place::Written, ".data"),
1062            (Place::ReadOnly, ".rodata"),
1063            (Place::RelocReadOnly { local: false }, ".data.rel.ro"),
1064            (Place::RelocReadOnly { local: true }, ".data.rel.ro.local"),
1065            (Place::Zero, ".bss"),
1066            (Place::Named(".init_array".to_owned()), ".init_array"),
1067        ] {
1068            let bytes = holding(variable("x", place.clone()));
1069            let file = object::File::parse(&bytes[..]).expect("a readable object");
1070            let section = file.section_by_name(wanted).unwrap_or_else(|| panic!("{place:?}"));
1071            assert_eq!(section.size(), 4, "{place:?}");
1072            // The zero filled one is as long as it says and carries none of it, which is the
1073            // whole reason the section exists.
1074            let carried = section.data().expect("the bytes").len();
1075            assert_eq!(carried, if place == Place::Zero { 0 } else { 4 }, "{place:?}");
1076        }
1077    }
1078
1079    /// What `-fdata-sections` comes down to in an object file: the section a variable would have
1080    /// shared, with its own name after it. The names are gcc 16's, checked against it on a Linux
1081    /// host, and the part in front of the dot is what a linker script and `--gc-sections` match on.
1082    #[test]
1083    fn every_variable_gets_a_section_of_its_own_when_that_is_what_was_asked_for() {
1084        let sections =
1085            Output { sections: Sections { functions: false, data: true }, ..Output::default() };
1086        for (place, wanted) in [
1087            (Place::Written, ".data.x"),
1088            (Place::ReadOnly, ".rodata.x"),
1089            (Place::RelocReadOnly { local: false }, ".data.rel.ro.x"),
1090            (Place::RelocReadOnly { local: true }, ".data.rel.ro.local.x"),
1091            (Place::Zero, ".bss.x"),
1092        ] {
1093            let data = Data { objects: vec![variable("x", place.clone())] };
1094            let bytes = write(&Text::default(), &data, &[], &target(), sections).expect("object");
1095            let file = object::File::parse(&bytes[..]).expect("a readable object");
1096            assert_eq!(lives_in(&file, "x"), wanted, "{place:?}");
1097            let section = file.section_by_name(wanted).expect("the section it named");
1098            assert_eq!(section.size(), 4, "{place:?}");
1099            // Which page it lands in is what the section it came out of decided, and splitting
1100            // must not quietly change it: the zero filled one still carries none of its bytes.
1101            let carried = section.data().expect("the bytes").len();
1102            assert_eq!(carried, if place == Place::Zero { 0 } else { 4 }, "{place:?}");
1103        }
1104    }
1105
1106    /// The two kinds of variable the flag leaves alone. A tentative definition is a request to the
1107    /// linker for that much zeroed space rather than an image, so there is no section to split off,
1108    /// and one the program named has the answer the source gave, which a flag must not overrule.
1109    #[test]
1110    fn a_variable_that_has_no_section_of_its_own_to_be_given_is_left_where_it_was() {
1111        let sections =
1112            Output { sections: Sections { functions: false, data: true }, ..Output::default() };
1113        let named = Place::Named(".init_array".to_owned());
1114        let objects = vec![variable("m", Place::Merged), variable("n", named)];
1115        let bytes =
1116            write(&Text::default(), &Data { objects }, &[], &target(), sections).expect("object");
1117        let file = object::File::parse(&bytes[..]).expect("a readable object");
1118        let m = file.symbols().find(|s| s.name() == Ok("m")).expect("the tentative one");
1119        assert!(m.is_common(), "still the linker's to merge and not in a section at all");
1120        assert_eq!(lives_in(&file, "n"), ".init_array");
1121        assert!(file.section_by_name(".init_array.n").is_none(), "the source already answered");
1122    }
1123
1124    /// A relocation in a variable's image counts from the start of the section it ended up in, the
1125    /// same question the split text has to answer and a shorter answer: a variable alone in a
1126    /// section starts where the section does.
1127    #[test]
1128    fn a_relocation_in_an_image_moves_with_the_variable_whose_image_it_is_in() {
1129        let sections =
1130            Output { sections: Sections { functions: false, data: true }, ..Output::default() };
1131        let pointer = Object {
1132            bytes: vec![0; 8],
1133            size: 8,
1134            align: 8,
1135            relocs: vec![Reloc {
1136                at: 0,
1137                symbol: "y".to_owned(),
1138                kind: Reference::Address { bytes: 8 },
1139                addend: 0,
1140            }],
1141            ..variable("p", Place::Written)
1142        };
1143        let objects = vec![variable("first", Place::Written), pointer];
1144        let bytes =
1145            write(&Text::default(), &Data { objects }, &[], &target(), sections).expect("object");
1146        let file = object::File::parse(&bytes[..]).expect("a readable object");
1147        let section = file.section_by_name(".data.p").expect("the pointer's own section");
1148        let (offset, reloc) = section.relocations().next().expect("one relocation");
1149        // Nothing rather than the eight it would be if the variable in front of it were still
1150        // counted, which is what a section of its own means.
1151        assert_eq!(offset, 0);
1152        assert_eq!(reloc.flags(), RelocationFlags::Elf { r_type: elf::R_X86_64_64 });
1153    }
1154
1155    /// Two variables that want `.data.rel.ro.local` end up in one section, not two of one name.
1156    ///
1157    /// The writer has no name of its own for that section, so it is added by hand, and asking for
1158    /// it again makes a second section rather than handing back the first. SQLite has enough const
1159    /// tables of function pointers in it to turn that into eighty odd sections in one object, each
1160    /// with its own relocation section beside it, which is a pile of section headers describing
1161    /// eight bytes apiece.
1162    #[test]
1163    fn every_variable_that_wants_the_local_relocated_section_shares_one() {
1164        let place = Place::RelocReadOnly { local: true };
1165        let data =
1166            Data { objects: vec![variable("first", place.clone()), variable("second", place)] };
1167        let bytes =
1168            write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object");
1169        let file = object::File::parse(&bytes[..]).expect("a readable object");
1170        let named = file.sections().filter(|s| s.name() == Ok(".data.rel.ro.local")).count();
1171        assert_eq!(named, 1, "one section holding both, not one each");
1172    }
1173
1174    #[test]
1175    fn a_variable_is_a_symbol_that_says_where_it_is_and_how_long_it_is() {
1176        let mut data = Data { objects: vec![variable("first", Place::Written)] };
1177        data.objects.push(Object { align: 16, ..variable("second", Place::Written) });
1178        let bytes =
1179            write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object");
1180        let file = object::File::parse(&bytes[..]).expect("a readable object");
1181        let second = file.symbols().find(|s| s.name() == Ok("second")).expect("the second one");
1182        assert_eq!(second.kind(), SymbolKind::Data);
1183        assert_eq!(second.size(), 4);
1184        // Sixteen rather than four, because the second one asked for sixteen and the first one
1185        // had already used four. Getting this wrong is a variable at an address it said it would
1186        // never be at, which nothing downstream would notice until an aligned load faulted.
1187        assert_eq!(second.address(), 16);
1188    }
1189
1190    #[test]
1191    fn the_linkage_a_variable_had_is_the_binding_the_symbol_gets() {
1192        for (binding, global, weak) in [
1193            (Binding::Global, true, false),
1194            (Binding::Local, false, false),
1195            (Binding::Weak, true, true),
1196        ] {
1197            let bytes = holding(Object { binding, ..variable("x", Place::Written) });
1198            let file = object::File::parse(&bytes[..]).expect("a readable object");
1199            let x = file.symbols().find(|s| s.name() == Ok("x")).expect("the variable");
1200            assert_eq!(x.is_global(), global, "{binding:?}");
1201            assert_eq!(x.is_weak(), weak, "{binding:?}");
1202        }
1203    }
1204
1205    #[test]
1206    fn a_tentative_definition_asks_the_linker_for_space_rather_than_naming_any() {
1207        let bytes = holding(Object { align: 8, ..variable("x", Place::Merged) });
1208        let file = object::read::elf::ElfFile64::<Endianness>::parse(&bytes[..]).expect("readable");
1209        let x = file.symbols().find(|s| s.name() == Ok("x")).expect("the variable");
1210        assert!(x.is_common(), "the linker merges every definition of this name into one");
1211        assert_eq!(x.size(), 4);
1212        // What a common symbol records where an ordinary one records its address is what it wants
1213        // to be aligned to, because it has no address yet. The reader deliberately answers nothing
1214        // when asked for the address of one, so this is the field itself.
1215        assert_eq!(x.address(), 0);
1216        assert_eq!(x.elf_symbol().st_value(Endianness::Little), 8);
1217    }
1218
1219    #[test]
1220    fn an_address_in_an_image_is_the_address_and_not_a_distance_to_it() {
1221        let object = Object {
1222            bytes: vec![0; 8],
1223            size: 8,
1224            align: 8,
1225            relocs: vec![Reloc {
1226                at: 0,
1227                symbol: "y".to_owned(),
1228                kind: Reference::Address { bytes: 8 },
1229                addend: 16,
1230            }],
1231            ..variable("p", Place::Written)
1232        };
1233        let bytes = holding(object);
1234        let file = object::File::parse(&bytes[..]).expect("a readable object");
1235        let section = file.section_by_name(".data").expect("a data section");
1236        let (offset, reloc) = section.relocations().next().expect("one relocation");
1237        assert_eq!(offset, 0);
1238        assert_eq!(reloc.addend(), 16);
1239        assert_eq!(reloc.flags(), RelocationFlags::Elf { r_type: elf::R_X86_64_64 });
1240        let y = file.symbols().find(|s| s.name() == Ok("y")).expect("what it points at");
1241        assert!(y.is_undefined(), "nothing here defines it and the linker is being asked for it");
1242    }
1243
1244    /// Not a rewording of the case above: what is checked is the arithmetic between the two.
1245    #[test]
1246    fn a_relocation_counts_from_the_start_of_the_section_and_not_of_the_image_it_is_in() {
1247        let mut data = Data { objects: vec![variable("first", Place::Written)] };
1248        data.objects.push(Object {
1249            bytes: vec![0; 16],
1250            size: 16,
1251            align: 8,
1252            relocs: vec![Reloc {
1253                at: 8,
1254                symbol: "y".to_owned(),
1255                kind: Reference::Address { bytes: 8 },
1256                addend: 0,
1257            }],
1258            ..variable("second", Place::Written)
1259        });
1260        let bytes =
1261            write(&Text::default(), &data, &[], &target(), Output::default()).expect("an object");
1262        let file = object::File::parse(&bytes[..]).expect("a readable object");
1263        let section = file.section_by_name(".data").expect("a data section");
1264        let (offset, _) = section.relocations().next().expect("one relocation");
1265        // Eight into the second image, which starts eight in because the first one is four long
1266        // and the second is eight aligned.
1267        assert_eq!(offset, 16);
1268    }
1269
1270    #[test]
1271    fn a_second_name_is_a_second_symbol_at_the_first_one_s_address_and_no_second_image() {
1272        let data = Data {
1273            objects: vec![Object { binding: Binding::Local, ..variable("a", Place::Written) }],
1274        };
1275        let aliases = [Alias {
1276            name: "b".to_owned(),
1277            target: "a".to_owned(),
1278            binding: Binding::Global,
1279            visibility: Visibility::Default,
1280        }];
1281        let bytes = write(&Text::default(), &data, &aliases, &target(), Output::default())
1282            .expect("an object");
1283        let file = object::File::parse(&bytes[..]).expect("a readable object");
1284        let a = file.symbols().find(|s| s.name() == Ok("a")).expect("the variable");
1285        let b = file.symbols().find(|s| s.name() == Ok("b")).expect("the second name");
1286        assert_eq!(b.address(), a.address(), "the same place");
1287        assert_eq!(b.size(), a.size());
1288        assert_eq!(b.section_index(), a.section_index());
1289        // The binding is the one thing the second name does not take from the first, which is
1290        // what `extern int b __attribute__((alias("a")))` on a `static a` asks for.
1291        assert!(a.is_local(), "the target was written `static`");
1292        assert!(b.is_global(), "and the name given to it was not");
1293        // Four bytes of image and not eight, since an alias is a name and not a copy.
1294        assert_eq!(file.section_by_name(".data").expect("a data section").size(), 4);
1295    }
1296
1297    #[test]
1298    fn a_function_can_be_given_a_second_name_the_same_way_a_variable_can() {
1299        let text = calling("puts");
1300        let aliases = [Alias {
1301            name: "g".to_owned(),
1302            target: "f".to_owned(),
1303            binding: Binding::Weak,
1304            visibility: Visibility::Default,
1305        }];
1306        let bytes = write(&text, &Data::default(), &aliases, &target(), Output::default())
1307            .expect("an object");
1308        let file = object::File::parse(&bytes[..]).expect("a readable object");
1309        let f = file.symbols().find(|s| s.name() == Ok("f")).expect("the function");
1310        let g = file.symbols().find(|s| s.name() == Ok("g")).expect("the second name");
1311        assert_eq!(g.address(), f.address());
1312        assert_eq!(g.size(), f.size());
1313        assert_eq!(g.kind(), f.kind(), "a second name for a function is a function");
1314        assert!(g.is_weak(), "so that a program may define the name itself instead");
1315    }
1316
1317    /// The front end is what reports this as a program's mistake, so one arriving here is a bug
1318    /// in this compiler and is said so rather than written as an undefined symbol.
1319    #[test]
1320    fn a_second_name_for_something_this_file_does_not_define_is_refused() {
1321        let aliases = [Alias {
1322            name: "b".to_owned(),
1323            target: "a".to_owned(),
1324            binding: Binding::Global,
1325            visibility: Visibility::Default,
1326        }];
1327        let error =
1328            write(&Text::default(), &Data::default(), &aliases, &target(), Output::default())
1329                .expect_err("nothing to point at");
1330        assert!(matches!(error, Error::Refused { .. }), "{error:?}");
1331    }
1332
1333    #[test]
1334    fn a_platform_this_does_not_write_is_said_so_rather_than_written_as_elf() {
1335        let text = calling("puts");
1336        for triple in [
1337            Triple::new(Arch::Aarch64, Os::Linux, Env::Gnu),
1338            Triple::new(Arch::X86_64, Os::Darwin, Env::Gnu),
1339        ] {
1340            let error =
1341                write(&text, &Data::default(), &[], &TargetInfo::new(triple), Output::default())
1342                    .expect_err("no writer");
1343            assert!(matches!(error, Error::Format { .. }), "{error:?}");
1344        }
1345    }
1346}