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