Skip to main content

rucc_asm/
data.rs

1//! Global variables as the image a file carries and the facts a linker needs about it.
2//!
3//! Design: `spec/11-asm-objects-debug.md` section 11.1, which asks that the text path and the
4//! binary path share one description so they cannot disagree. That is what this is for data: the
5//! walk over a module's globals happens once, here, and what it produces is a list of pieces that
6//! [`crate::att`] writes down as directives and [`Globals::image`] writes down as bytes. A `.long`
7//! in a listing and the four bytes in the object beside it come from the same piece.
8//!
9//! # What a piece is
10//!
11//! As much of an image as one directive says. The four kinds are the four things C can put in an
12//! initializer: a run of zeros, a run of literal bytes, one scalar, and the address of a symbol.
13//! The first three are bytes the compiler knows and the fourth is a hole the linker fills, which
14//! is the only reason data has relocations at all.
15//!
16//! Where a variable goes is worked out here too, from what the variable is rather than from
17//! anything the object format says: a variable nothing writes through goes in a page the loader
18//! can map read only, one whose image is all zeros goes in the section that carries no image, and
19//! one the program named a section for goes where the program said. What those sections are
20//! called is the format's business and is in [`crate::format`].
21//!
22//! # The second names
23//!
24//! [`aliases`] is the same idea for what `__attribute__((alias("target")))` asks for, and is here
25//! for the same reason: `.set b, a` in a listing and a second symbol table entry in an object have
26//! to be saying the same thing. There is no image in one, which is what makes an alias free.
27//!
28//! # What is refused
29//!
30//! A thread-local variable on a format that is not ELF. ELF says one with a section flag and a
31//! symbol type, which is what [`place`] and [`crate::format`] write. Windows hands out an index at
32//! load time and reaches a variable through a table the index names, and Mach-O puts a descriptor
33//! in front of every one and reaches it by calling through the descriptor, so neither is this
34//! written a different way and both are refused by name rather than written out as an ordinary
35//! variable that every thread would share.
36//!
37//! An ifunc, which is the other thing an alias in the IR can be. It is resolved once at program
38//! start by calling a function in the same object, which wants a symbol type and a relocation
39//! neither half of this writes yet.
40
41use rucc_base::{Interner, Symbol};
42use rucc_ir as ir;
43use rucc_ir::{AliasKind, Datum, GlobalId, Linkage, Module, SymbolRef};
44use rucc_object::{Alias, Apart, Binding, Data, Object, Place, Reference, Reloc, Visibility};
45use rucc_target::ObjectFormat;
46
47use crate::Error;
48
49/// Every variable a module defines, laid out.
50#[derive(Debug, Clone, Default, PartialEq, Eq)]
51pub struct Globals {
52    /// One entry per definition, in the order the module held them. A declaration is not here,
53    /// because a file says nothing about a variable another file defines beyond the references
54    /// that name it, and those are already in the text.
55    pub vars: Vec<Variable>,
56    /// Every name a declaration wrote `weak` on and this file does not define, in the order the
57    /// module held them.
58    ///
59    /// Not a definition and not bytes of anything, which is why it is a list of names beside the
60    /// variables rather than an entry among them. What it asks for is that the link be allowed to
61    /// leave the name undefined and hand every reference a zero address, which is how a library
62    /// offers a hook a profiler may fill in: the calls are written under `if (hook)` and the test
63    /// is false when nobody filled it in. Without it the link of a file that declares one fails
64    /// on an undefined symbol, which is what zstd's four tracing hooks do.
65    ///
66    /// Every one of them is here whether or not anything in the file refers to it, which is what
67    /// keeps the listing and the object saying the same thing: both read this list and neither
68    /// works out the answer for itself. gcc writes the directive only for the ones something
69    /// refers to, so a file that declares a hook and never calls it gets one undefined weak symbol
70    /// here that gcc does not put in. A linker has nothing to do about an undefined weak symbol
71    /// nothing refers to, which is why that difference is a difference and not a bug.
72    pub weak: Vec<String>,
73    /// The text of each `asm` at file scope with an instruction in it, in the order they were
74    /// written, which goes into the listing as it is. See `rucc_ir::Module::add_file_asm`.
75    ///
76    /// A unit with one is assembled from its listing, since only the assembler knows what the
77    /// template defines, which is why the driver asks [`Globals::kept`] beside
78    /// [`crate::kept`].
79    pub file_asm: Vec<String>,
80}
81
82impl Globals {
83    /// Whether anything here has to be read by the assembler, which today is an `asm` at file
84    /// scope with an instruction in it.
85    #[must_use]
86    pub fn kept(&self) -> bool {
87        !self.file_asm.is_empty()
88    }
89}
90
91/// One global variable, as the pieces of its image and what the linker is told about it.
92#[derive(Debug, Clone, PartialEq, Eq)]
93pub struct Variable {
94    /// Its name, as the C program spelled it. The underscore an Apple symbol carries is added
95    /// when it is written down, because it is a fact about the object format and not about the
96    /// variable.
97    pub name: String,
98    /// How many bytes it occupies, which the pieces add up to.
99    pub size: u64,
100    /// What it has to be aligned to, always a power of two.
101    pub align: u64,
102    /// Which section it goes in.
103    pub place: Place,
104    /// How the linker sees the name.
105    pub binding: Binding,
106    /// How far outside a shared library holding it the name reaches.
107    pub visibility: Visibility,
108    /// Its image, in order.
109    pub pieces: Vec<Piece>,
110}
111
112/// As much of an image as one directive says.
113#[derive(Debug, Clone, PartialEq, Eq)]
114pub enum Piece {
115    /// That many zero bytes, which is the tail of a partly initialized array and the whole of a
116    /// variable with no initializer.
117    Zero(u64),
118    /// Those literal bytes, which is what a string literal and anything already laid out is.
119    Bytes(Vec<u8>),
120    /// One number, in the byte order the module was built for, as many bytes wide as its type.
121    Scalar(Vec<u8>),
122    /// The address of a symbol, which is a hole this compiler leaves and the linker fills.
123    Addr {
124        /// Whose address it is, as the C program spelled it.
125        symbol: String,
126        /// What to add to that address. `&array[2]` is the address of `array` plus eight.
127        addend: i64,
128        /// How many bytes it occupies.
129        bytes: u8,
130    },
131    /// How far a symbol is from these four bytes, which is the same hole with a different
132    /// question in it. `.long target - .` in an `asm` at file scope and nothing else.
133    Away {
134        /// Whose distance it is, as the template spelled it.
135        symbol: String,
136        /// What to add to that address before the distance is taken, which is how far into the
137        /// symbol the place being measured to sits.
138        addend: i64,
139    },
140    /// How far one label is from another, which is a number the writer works out once the code is
141    /// laid out and not a hole for the linker. `.long .L1-.L0` for GNU C's `&&l1 - &&l0`.
142    Apart {
143        /// The label measured to, as the module named it.
144        to: String,
145        /// The label measured from.
146        from: String,
147        /// What to add to the distance.
148        addend: i64,
149        /// How many bytes it occupies.
150        bytes: u8,
151    },
152}
153
154impl Piece {
155    /// How many bytes it contributes to the image.
156    #[must_use]
157    pub fn size(&self) -> u64 {
158        match self {
159            Piece::Zero(bytes) => *bytes,
160            Piece::Bytes(bytes) | Piece::Scalar(bytes) => bytes.len() as u64,
161            Piece::Addr { bytes, .. } | Piece::Apart { bytes, .. } => u64::from(*bytes),
162            Piece::Away { .. } => 4,
163        }
164    }
165}
166
167impl Globals {
168    /// The image of every variable, and where in each one the linker has to write an address.
169    ///
170    /// A variable in a section that carries no image contributes its size and none of its bytes,
171    /// which is what makes a program with a large zeroed array a small file.
172    #[must_use]
173    pub fn image(&self) -> Data {
174        let mut data = Data { weak: self.weak.clone(), ..Data::default() };
175        for var in &self.vars {
176            let mut object = Object {
177                name: var.name.clone(),
178                bytes: Vec::new(),
179                size: var.size,
180                align: var.align,
181                place: var.place.clone(),
182                binding: var.binding,
183                visibility: var.visibility,
184                relocs: Vec::new(),
185            };
186            if matches!(var.place, Place::Zero | Place::Merged | Place::Thread { zero: true }) {
187                data.objects.push(object);
188                continue;
189            }
190            for piece in &var.pieces {
191                match piece {
192                    Piece::Zero(bytes) => {
193                        object.bytes.resize(object.bytes.len() + *bytes as usize, 0);
194                    }
195                    Piece::Bytes(bytes) | Piece::Scalar(bytes) => {
196                        object.bytes.extend_from_slice(bytes);
197                    }
198                    Piece::Addr { symbol, addend, bytes } => {
199                        // The bytes are left zero rather than holding anything, because a linker
200                        // writes the whole hole from the addend and never reads what was there.
201                        object.relocs.push(Reloc {
202                            at: object.bytes.len(),
203                            symbol: symbol.clone(),
204                            kind: Reference::Address { bytes: *bytes },
205                            addend: *addend,
206                            // An image rather than an instruction, so there is nothing after the
207                            // hole for the question to be about.
208                            after: 0,
209                        });
210                        object.bytes.resize(object.bytes.len() + usize::from(*bytes), 0);
211                    }
212                    Piece::Away { symbol, addend } => {
213                        object.relocs.push(Reloc {
214                            at: object.bytes.len(),
215                            symbol: symbol.clone(),
216                            kind: Reference::Away,
217                            addend: *addend,
218                            after: 0,
219                        });
220                        object.bytes.resize(object.bytes.len() + 4, 0);
221                    }
222                    Piece::Apart { to, from, addend, bytes } => {
223                        data.apart.push(Apart {
224                            object: data.objects.len(),
225                            at: object.bytes.len(),
226                            to: to.clone(),
227                            from: from.clone(),
228                            addend: *addend,
229                            bytes: *bytes,
230                        });
231                        object.bytes.resize(object.bytes.len() + usize::from(*bytes), 0);
232                    }
233                }
234            }
235            data.objects.push(object);
236        }
237        data
238    }
239}
240
241/// Every variable a module defines, laid out.
242///
243/// The format is an argument because one question here is the format's rather than the module's:
244/// a thread-local variable is a section flag and a symbol type on ELF and an image and a descriptor
245/// on Mach-O, and is neither on COFF, so which of them is being written decides whether there is
246/// anything to write.
247///
248/// # Errors
249///
250/// [`Error::Thread`] for a thread-local variable on a format that does not spell one this way,
251/// which is a program this compiler is behind on rather than a mistake, and [`Error::Image`] for a
252/// piece of an initializer nothing here can write down. See [`Error`].
253pub fn globals(module: &Module, names: &Interner, format: ObjectFormat) -> Result<Globals, Error> {
254    let mut out = Globals::default();
255    for id in module.globals() {
256        if module[id].is_declaration() {
257            continue;
258        }
259        out.vars.push(variable(module, names, id, format)?);
260    }
261    // The other half, which is names and no bytes. A declaration is not a variable and has no
262    // image, so it is skipped above and picked up here, and only the weak ones are: an ordinary
263    // undefined name needs nothing said about it, since a reference to one is already an
264    // undefined symbol and a link that cannot resolve it is a link that should fail.
265    //
266    // The functions as well as the objects, and the functions are the ones a program actually
267    // writes: a hook a library offers is a function, and `if (hook)` around the call is the test
268    // that reads the zero address a weak reference gets. Both walks are here rather than one in
269    // each caller, for the reason the walk over the definitions above is one walk.
270    for id in module.funcs() {
271        let func = &module[id];
272        if func.is_declaration() && func.linkage == Linkage::Weak {
273            out.weak.push(names.resolve(func.name).to_owned());
274        }
275    }
276    for id in module.globals() {
277        let global = &module[id];
278        if global.is_declaration() && global.linkage == Linkage::Weak {
279            out.weak.push(names.resolve(global.name).to_owned());
280        }
281    }
282    out.file_asm = module.file_asms().to_vec();
283    Ok(out)
284}
285
286/// Every second name a module gives something, in the order it gave them.
287///
288/// One walk for the same reason the one over the globals above is one: `.set b, a` in a listing
289/// and a second symbol table entry in an object have to be saying the same thing, and the way to
290/// be sure of that is for both of them to be reading the same list.
291///
292/// # Errors
293///
294/// [`Error::IFunc`] for an ifunc, which is the other thing this shape of the IR carries and is a
295/// program this compiler is behind on rather than a mistake. See [`Error`].
296pub fn aliases(module: &Module, names: &Interner) -> Result<Vec<Alias>, Error> {
297    let mut out = Vec::new();
298    for id in module.aliases() {
299        let alias = &module[id];
300        let name = names.resolve(alias.name).to_owned();
301        if alias.kind != AliasKind::Alias {
302            return Err(Error::IFunc { name });
303        }
304        out.push(Alias {
305            name,
306            target: names.resolve(alias.target).to_owned(),
307            binding: binding(alias.linkage),
308            visibility: visibility(alias.visibility),
309        });
310    }
311    Ok(out)
312}
313
314/// One variable, laid out.
315fn variable(
316    module: &Module,
317    names: &Interner,
318    id: GlobalId,
319    format: ObjectFormat,
320) -> Result<Variable, Error> {
321    let global = &module[id];
322    let name = names.resolve(global.name).to_owned();
323    if global.tls.is_some() && !matches!(format, ObjectFormat::Elf | ObjectFormat::MachO) {
324        return Err(Error::Thread { name, format: format.as_str() });
325    }
326    let init = global.init.expect("a definition has an image");
327
328    let mut pieces = Vec::new();
329    // The names the image holds the addresses of, kept as symbols rather than read back off the
330    // pieces, because whether one of them is defined here is a question about this module and the
331    // pieces carry the spelling rather than the name.
332    let mut addrs = Vec::new();
333    let mut written = 0;
334    for datum in &module[init] {
335        let piece = match *datum {
336            Datum::Zero(bytes) => Piece::Zero(bytes),
337            Datum::Bytes(range) => Piece::Bytes(module[range].to_vec()),
338            Datum::Scalar { ty, value } => {
339                if ty.lanes() != 1 {
340                    let why = format!("a {ty} in an initializer");
341                    return Err(Error::Image { name, why });
342                }
343                let bytes = usize::try_from(ty.bits().div_ceil(8)).expect("a scalar this wide");
344                let mut image = module[value].bits().to_le_bytes()[..bytes].to_vec();
345                if !module.datalayout.little_endian {
346                    image.reverse();
347                }
348                Piece::Scalar(image)
349            }
350            // A distance rather than an address, which is the same symbol and addend read a
351            // different way. It is not in `addrs` below, because what that list is for is whether
352            // a read only image needs relocating when it is loaded, and a distance between two
353            // places in the same file is the same number wherever the file is loaded.
354            Datum::Away(idx) => {
355                let reloc = module[idx];
356                if reloc.size != 4 {
357                    let why = format!("a distance {} bytes wide", reloc.size);
358                    return Err(Error::Image { name, why });
359                }
360                let symbol = names.resolve(reloc.symbol).to_owned();
361                Piece::Away { symbol, addend: reloc.addend }
362            }
363            // Both ends are labels of a function in this file, so neither goes in `addrs`: the
364            // distance is the same number wherever the file is loaded.
365            Datum::Apart { to, from } => {
366                let reloc = module[to];
367                let bytes = match reloc.size {
368                    1 | 2 | 4 | 8 => reloc.size as u8,
369                    size => {
370                        let why = format!("a distance {size} bytes wide");
371                        return Err(Error::Image { name, why });
372                    }
373                };
374                let to = names.resolve(reloc.symbol).to_owned();
375                let from = names.resolve(from).to_owned();
376                Piece::Apart { to, from, addend: reloc.addend, bytes }
377            }
378            Datum::Addr(idx) => {
379                let reloc = module[idx];
380                // Four and eight are the widths a machine has a relocation for and a directive
381                // for. Anything else is a module nothing here produced and neither half of the
382                // description could write down, so it is refused rather than rounded to one.
383                let bytes = match reloc.size {
384                    4 | 8 => reloc.size as u8,
385                    size => {
386                        let why = format!("an address {size} bytes wide");
387                        return Err(Error::Image { name, why });
388                    }
389                };
390                let symbol = names.resolve(reloc.symbol).to_owned();
391                addrs.push(reloc.symbol);
392                Piece::Addr { symbol, addend: reloc.addend, bytes }
393            }
394        };
395        written += piece.size();
396        pieces.push(piece);
397    }
398    // An image shorter than the variable is the rest of an array nothing initialized, which the
399    // front end may leave off the end rather than write out as zeros it already said were there.
400    if written < global.size {
401        pieces.push(Piece::Zero(global.size - written));
402    }
403
404    let place = place(module, names, id, &pieces, &addrs);
405    let size = global.size.max(written);
406    let binding = binding(global.linkage);
407    let visibility = visibility(global.visibility);
408    Ok(Variable { name, size, align: u64::from(global.align), place, binding, visibility, pieces })
409}
410
411/// What the linker is told about a name, from the linkage the module gave it.
412///
413/// Three of the five, because that is how many an object file can say. Which of the two weak ones
414/// a symbol had is a fact the optimizer needs and the linker does not, and a common one is a
415/// definition every other file may also make, which is a section rather than a binding.
416const fn binding(linkage: Linkage) -> Binding {
417    match linkage {
418        Linkage::Internal => Binding::Local,
419        Linkage::Weak | Linkage::LinkOnce => Binding::Weak,
420        Linkage::External | Linkage::Common => Binding::Global,
421    }
422}
423
424/// What the dynamic linker is told about a name, from the visibility the module gave it.
425///
426/// All three, because ELF says all three, and the two enumerations are the same three answers
427/// written once in a crate that is not allowed to know what an object file is and once in one
428/// that is.
429const fn visibility(visibility: ir::Visibility) -> Visibility {
430    match visibility {
431        ir::Visibility::Default => Visibility::Default,
432        ir::Visibility::Hidden => Visibility::Hidden,
433        ir::Visibility::Protected => Visibility::Protected,
434    }
435}
436
437/// Which section a variable goes in.
438///
439/// The program's answer when it gave one, and otherwise worked out from what the variable is. A
440/// tentative definition is asked of the linker rather than put anywhere, since the whole of what
441/// it says is that the variable exists and that some other file may say so too.
442///
443/// Being constant is not on its own enough to put a variable in a section nothing may ever write.
444/// An image holding the address of something is an image the loader has to write, because an
445/// address is not a number a link knows when everything it links may be moved. So the question
446/// asked of a constant variable is whether its image holds an address, and one that does goes in
447/// the section that is writable for exactly as long as the loader needs it to be.
448///
449/// A variable with no image at all is not zero filled, it is empty, and the two differ. An `asm`
450/// at file scope writes one whenever it puts a label at the end of what it just wrote, and what
451/// that label means is the address after those bytes, so it belongs in the section those bytes
452/// went into. Sending it to the section of zeros instead would move it away from the run it was
453/// written to mark the end of, and the distance a program reads off it would be a different
454/// distance.
455fn place(
456    module: &Module,
457    names: &Interner,
458    id: GlobalId,
459    pieces: &[Piece],
460    addrs: &[Symbol],
461) -> Place {
462    let global = &module[id];
463    // First, because a thread-local variable has to be in one of the two sections a thread gets a
464    // copy of whatever else is true of it. It is never merged, since what `.comm` asks the linker
465    // for is one piece of zeroed space and this wants one per thread, and it is never read only,
466    // since the copy is made by writing it.
467    if global.tls.is_some() {
468        let zero = !pieces.is_empty() && pieces.iter().all(|piece| matches!(piece, Piece::Zero(_)));
469        return Place::Thread { zero };
470    }
471    if let Some(section) = global.section {
472        return Place::Named(names.resolve(section).to_owned());
473    }
474    if global.linkage == Linkage::Common {
475        return Place::Merged;
476    }
477    if !pieces.is_empty() && pieces.iter().all(|piece| matches!(piece, Piece::Zero(_))) {
478        return Place::Zero;
479    }
480    if global.constant {
481        return match addrs {
482            [] => Place::ReadOnly,
483            _ => Place::RelocReadOnly {
484                local: addrs.iter().all(|&symbol| resolved_here(module, symbol)),
485            },
486        };
487    }
488    Place::Written
489}
490
491/// Whether that name is one this file both defines and keeps to itself.
492///
493/// Both halves matter. A name this file does not define is one the link resolves from somewhere
494/// else, and a name this file exports is one another object may define instead, so neither is an
495/// address the first pages of the relocated segment can be laid out around.
496fn resolved_here(module: &Module, symbol: Symbol) -> bool {
497    match module.lookup(symbol) {
498        Some(SymbolRef::Func(id)) => {
499            module[id].linkage == Linkage::Internal && !module[id].is_declaration()
500        }
501        Some(SymbolRef::Global(id)) => {
502            module[id].linkage == Linkage::Internal && !module[id].is_declaration()
503        }
504        Some(SymbolRef::Alias(id)) => module[id].linkage == Linkage::Internal,
505        None => false,
506    }
507}
508
509#[cfg(test)]
510mod tests {
511    use super::*;
512
513    use rucc_ir::{Alias as IrAlias, Global, Imm, Reloc as IrReloc, TlsModel, Type};
514    use rucc_target::{Arch, Env, Os, TargetInfo, Triple};
515
516    /// A module for the one target every case here is written for.
517    fn module(names: &mut Interner) -> Module {
518        let target = TargetInfo::new(Triple::new(Arch::X86_64, Os::Linux, Env::Gnu));
519        Module::new(names.intern("t.c"), &target)
520    }
521
522    /// A four byte variable with that image.
523    fn defined(module: &mut Module, names: &mut Interner, name: &str, data: &[Datum]) -> GlobalId {
524        let list = module.push_data(data);
525        let mut global = Global::new(names.intern(name), 4, 4);
526        global.init = Some(list);
527        module.add_global(global)
528    }
529
530    #[test]
531    fn a_declaration_is_not_a_variable_this_file_defines() {
532        let mut names = Interner::new();
533        let mut module = module(&mut names);
534        module.add_global(Global::new(names.intern("x"), 4, 4));
535        defined(&mut module, &mut names, "y", &[Datum::Zero(4)]);
536        let vars =
537            globals(&module, &names, ObjectFormat::Elf).expect("a module of two globals").vars;
538        assert_eq!(vars.iter().map(|var| var.name.as_str()).collect::<Vec<_>>(), ["y"]);
539    }
540
541    /// A variable's visibility comes through the layout and out the other side of the image.
542    ///
543    /// Two hops rather than one, because a variable is laid out here and then turned into an
544    /// object for the writer a hundred lines further up, and a field that survives the first and
545    /// not the second is a field the object file never hears about.
546    #[test]
547    fn the_visibility_a_variable_asked_for_reaches_the_image() {
548        for (asked, wanted) in [
549            (ir::Visibility::Default, Visibility::Default),
550            (ir::Visibility::Hidden, Visibility::Hidden),
551            (ir::Visibility::Protected, Visibility::Protected),
552        ] {
553            let mut names = Interner::new();
554            let mut module = module(&mut names);
555            let id = defined(&mut module, &mut names, "x", &[Datum::Zero(4)]);
556            module[id].visibility = asked;
557            let out = globals(&module, &names, ObjectFormat::Elf).expect("a module of one global");
558            assert_eq!(out.vars[0].visibility, wanted, "{asked:?}");
559            assert_eq!(out.image().objects[0].visibility, wanted, "{asked:?} through the image");
560        }
561    }
562
563    #[test]
564    fn a_number_in_an_image_is_the_bytes_the_machine_reads_it_as() {
565        let mut names = Interner::new();
566        let mut module = module(&mut names);
567        let value = module.add_imm(Imm::int(258, Type::int(32)));
568        defined(&mut module, &mut names, "x", &[Datum::Scalar { ty: Type::int(32), value }]);
569        let vars =
570            globals(&module, &names, ObjectFormat::Elf).expect("a module of one global").vars;
571        assert_eq!(vars[0].pieces, [Piece::Scalar(vec![2, 1, 0, 0])]);
572        // The low byte first, which is what this machine reads and is a fact about the module
573        // rather than about the variable.
574        assert_eq!(vars[0].pieces[0].size(), 4);
575    }
576
577    #[test]
578    fn what_a_variable_is_decides_which_section_it_goes_in() {
579        let mut names = Interner::new();
580        let mut module = module(&mut names);
581        let value = module.add_imm(Imm::int(1, Type::int(32)));
582        let scalar = Datum::Scalar { ty: Type::int(32), value };
583
584        let zeroed = defined(&mut module, &mut names, "zeroed", &[Datum::Zero(4)]);
585        let written = defined(&mut module, &mut names, "written", &[scalar]);
586        let read_only = defined(&mut module, &mut names, "read_only", &[scalar]);
587        module[read_only].constant = true;
588        let named = defined(&mut module, &mut names, "named", &[scalar]);
589        module[named].section = Some(names.intern(".init_array"));
590        let merged = defined(&mut module, &mut names, "merged", &[Datum::Zero(4)]);
591        module[merged].linkage = Linkage::Common;
592
593        let vars =
594            globals(&module, &names, ObjectFormat::Elf).expect("a module of five globals").vars;
595        let places: Vec<&Place> = vars.iter().map(|var| &var.place).collect();
596        assert_eq!(
597            places,
598            [
599                &Place::Zero,
600                &Place::Written,
601                &Place::ReadOnly,
602                &Place::Named(".init_array".to_owned()),
603                &Place::Merged,
604            ]
605        );
606        let _ = (zeroed, written);
607    }
608
609    /// A constant holding an address goes where the loader may write it once, not in `.rodata`.
610    ///
611    /// Three of them, because the question has three answers. One whose address is of something
612    /// this file defines and keeps to itself is local, one whose address is of a name this file
613    /// only declares is not, and one that mixes the two is not either, since it takes only one
614    /// name the link resolves from elsewhere to spoil it. The fourth is the constant with no
615    /// address in it at all, which is the case that has to keep going where it went before.
616    #[test]
617    fn a_constant_holding_an_address_goes_where_the_loader_may_write_it_once() {
618        let mut names = Interner::new();
619        let mut module = module(&mut names);
620        let value = module.add_imm(Imm::int(1, Type::int(32)));
621
622        let mine = defined(&mut module, &mut names, "mine", &[Datum::Zero(4)]);
623        module[mine].linkage = Linkage::Internal;
624        let theirs = module.add_global(Global::new(names.intern("theirs"), 4, 4));
625
626        let to_mine = module.add_reloc(IrReloc { symbol: module[mine].name, addend: 0, size: 8 });
627        let to_theirs =
628            module.add_reloc(IrReloc { symbol: module[theirs].name, addend: 0, size: 8 });
629
630        let plain = defined(
631            &mut module,
632            &mut names,
633            "plain",
634            &[Datum::Scalar { ty: Type::int(32), value }],
635        );
636        module[plain].constant = true;
637        let local = defined(&mut module, &mut names, "local", &[Datum::Addr(to_mine)]);
638        module[local].constant = true;
639        module[local].size = 8;
640        let far = defined(&mut module, &mut names, "far", &[Datum::Addr(to_theirs)]);
641        module[far].constant = true;
642        module[far].size = 8;
643        let both = defined(
644            &mut module,
645            &mut names,
646            "both",
647            &[Datum::Addr(to_mine), Datum::Addr(to_theirs)],
648        );
649        module[both].constant = true;
650        module[both].size = 16;
651
652        let vars =
653            globals(&module, &names, ObjectFormat::Elf).expect("a module of five globals").vars;
654        let places: Vec<(&str, &Place)> =
655            vars.iter().map(|var| (var.name.as_str(), &var.place)).collect();
656        assert_eq!(
657            places,
658            [
659                ("mine", &Place::Zero),
660                ("plain", &Place::ReadOnly),
661                ("local", &Place::RelocReadOnly { local: true }),
662                ("far", &Place::RelocReadOnly { local: false }),
663                ("both", &Place::RelocReadOnly { local: false }),
664            ]
665        );
666    }
667
668    #[test]
669    fn the_rest_of_an_image_the_front_end_left_off_is_zeros() {
670        let mut names = Interner::new();
671        let mut module = module(&mut names);
672        let value = module.add_imm(Imm::int(7, Type::int(8)));
673        let id =
674            defined(&mut module, &mut names, "x", &[Datum::Scalar { ty: Type::int(8), value }]);
675        module[id].size = 4;
676        let vars =
677            globals(&module, &names, ObjectFormat::Elf).expect("a module of one global").vars;
678        assert_eq!(vars[0].pieces, [Piece::Scalar(vec![7]), Piece::Zero(3)]);
679        assert_eq!(vars[0].size, 4);
680    }
681
682    #[test]
683    fn a_variable_holding_an_address_is_a_hole_and_a_name_for_the_linker() {
684        let mut names = Interner::new();
685        let mut module = module(&mut names);
686        let reloc = module.add_reloc(IrReloc { symbol: names.intern("y"), addend: 16, size: 8 });
687        let id = defined(&mut module, &mut names, "p", &[Datum::Addr(reloc)]);
688        module[id].size = 8;
689        let vars =
690            globals(&module, &names, ObjectFormat::Elf).expect("a module of one global").vars;
691        assert_eq!(vars[0].pieces, [Piece::Addr { symbol: "y".to_owned(), addend: 16, bytes: 8 }]);
692
693        let data = Globals { vars, ..Globals::default() }.image();
694        assert_eq!(data.objects[0].bytes, vec![0; 8]);
695        assert_eq!(
696            data.objects[0].relocs,
697            [Reloc {
698                at: 0,
699                symbol: "y".to_owned(),
700                kind: Reference::Address { bytes: 8 },
701                addend: 16,
702                after: 0,
703            }]
704        );
705    }
706
707    #[test]
708    fn a_variable_holding_a_distance_is_a_hole_the_linker_measures_from_where_it_is() {
709        let mut names = Interner::new();
710        let mut module = module(&mut names);
711        let reloc = module.add_reloc(IrReloc { symbol: names.intern("y"), addend: 1, size: 4 });
712        let id = defined(&mut module, &mut names, "d", &[Datum::Away(reloc)]);
713        module[id].size = 4;
714        // Read only rather than relocated at load time, which is the point of writing a table of
715        // distances: what is in the four bytes is the same number wherever the file is loaded.
716        module[id].constant = true;
717        let vars =
718            globals(&module, &names, ObjectFormat::Elf).expect("a module of one global").vars;
719        assert_eq!(vars[0].pieces, [Piece::Away { symbol: "y".to_owned(), addend: 1 }]);
720        assert_eq!(vars[0].place, Place::ReadOnly);
721
722        let data = Globals { vars, ..Globals::default() }.image();
723        assert_eq!(data.objects[0].bytes, vec![0; 4]);
724        assert_eq!(
725            data.objects[0].relocs,
726            [Reloc { at: 0, symbol: "y".to_owned(), kind: Reference::Away, addend: 1, after: 0 }]
727        );
728    }
729
730    #[test]
731    fn a_distance_of_a_width_no_relocation_writes_is_refused_by_the_width_it_asked_for() {
732        let mut names = Interner::new();
733        let mut module = module(&mut names);
734        let reloc = module.add_reloc(IrReloc { symbol: names.intern("y"), addend: 0, size: 8 });
735        let id = defined(&mut module, &mut names, "d", &[Datum::Away(reloc)]);
736        module[id].size = 8;
737        let failed = globals(&module, &names, ObjectFormat::Elf).expect_err("a distance that wide");
738        assert_eq!(
739            failed,
740            Error::Image { name: "d".to_owned(), why: "a distance 8 bytes wide".to_owned() }
741        );
742    }
743
744    #[test]
745    fn a_variable_in_a_section_that_carries_no_image_carries_its_size_and_nothing_else() {
746        let mut names = Interner::new();
747        let mut module = module(&mut names);
748        let id = defined(&mut module, &mut names, "x", &[Datum::Zero(4096)]);
749        module[id].size = 4096;
750        let data =
751            globals(&module, &names, ObjectFormat::Elf).expect("a module of one global").image();
752        assert_eq!(data.objects[0].place, Place::Zero);
753        assert_eq!(data.objects[0].size, 4096);
754        // The point of the section: a program with a large zeroed array is a small file.
755        assert!(data.objects[0].bytes.is_empty());
756    }
757
758    #[test]
759    fn the_linkage_a_variable_had_decides_how_the_linker_sees_the_name() {
760        let mut names = Interner::new();
761        let mut module = module(&mut names);
762        for (index, (linkage, binding)) in [
763            (Linkage::External, Binding::Global),
764            (Linkage::Internal, Binding::Local),
765            (Linkage::Weak, Binding::Weak),
766            (Linkage::LinkOnce, Binding::Weak),
767        ]
768        .into_iter()
769        .enumerate()
770        {
771            let name = format!("x{index}");
772            let id = defined(&mut module, &mut names, &name, &[Datum::Zero(4)]);
773            module[id].linkage = linkage;
774            let vars =
775                globals(&module, &names, ObjectFormat::Elf).expect("a module of globals").vars;
776            assert_eq!(vars[index].binding, binding, "{linkage:?}");
777        }
778    }
779
780    #[test]
781    fn the_linkage_an_alias_had_decides_how_the_linker_sees_the_second_name() {
782        let mut names = Interner::new();
783        let mut module = module(&mut names);
784        let target = names.intern("a");
785        for (index, (linkage, binding)) in [
786            (Linkage::External, Binding::Global),
787            (Linkage::Internal, Binding::Local),
788            (Linkage::Weak, Binding::Weak),
789        ]
790        .into_iter()
791        .enumerate()
792        {
793            let mut alias = IrAlias::new(names.intern(&format!("b{index}")), target);
794            alias.linkage = linkage;
795            module.add_alias(alias);
796            let written = aliases(&module, &names).expect("a module of aliases");
797            assert_eq!(written[index].binding, binding, "{linkage:?}");
798            assert_eq!(written[index].target, "a", "{linkage:?}");
799        }
800    }
801
802    /// A different job from a second name for something, and the wrong answer would be an alias
803    /// pointing at the resolver rather than at what the resolver picks.
804    #[test]
805    fn an_ifunc_is_refused_rather_than_written_as_an_ordinary_second_name() {
806        let mut names = Interner::new();
807        let mut module = module(&mut names);
808        let mut memcpy = IrAlias::new(names.intern("memcpy"), names.intern("pick_memcpy"));
809        memcpy.kind = AliasKind::IFunc;
810        module.add_alias(memcpy);
811        let error = aliases(&module, &names).expect_err("an ifunc");
812        assert_eq!(error, Error::IFunc { name: "memcpy".to_owned() });
813    }
814
815    /// The two sections a thread gets a copy of, told apart the way `.data` and `.bss` are.
816    #[test]
817    fn a_thread_local_variable_goes_in_the_section_a_thread_gets_a_copy_of() {
818        let mut names = Interner::new();
819        let mut module = module(&mut names);
820        let value = module.add_imm(Imm::int(1, Type::int(32)));
821        let written = defined(
822            &mut module,
823            &mut names,
824            "counted",
825            &[Datum::Scalar { ty: Type::int(32), value }],
826        );
827        module[written].tls = Some(TlsModel::GlobalDynamic);
828        let zeroed = defined(&mut module, &mut names, "empty", &[Datum::Zero(4)]);
829        module[zeroed].tls = Some(TlsModel::GlobalDynamic);
830
831        let vars = globals(&module, &names, ObjectFormat::Elf).expect("two thread-locals").vars;
832        assert_eq!(vars[0].place, Place::Thread { zero: false }, ".tdata");
833        assert_eq!(vars[1].place, Place::Thread { zero: true }, ".tbss");
834    }
835
836    /// Being read only loses to being thread-local, because the copy is made by writing it.
837    #[test]
838    fn a_constant_thread_local_is_still_in_the_section_a_thread_gets_a_copy_of() {
839        let mut names = Interner::new();
840        let mut module = module(&mut names);
841        let value = module.add_imm(Imm::int(1, Type::int(32)));
842        let id =
843            defined(&mut module, &mut names, "x", &[Datum::Scalar { ty: Type::int(32), value }]);
844        module[id].tls = Some(TlsModel::GlobalDynamic);
845        module[id].constant = true;
846
847        let vars = globals(&module, &names, ObjectFormat::Elf).expect("a thread-local").vars;
848        assert_eq!(vars[0].place, Place::Thread { zero: false });
849    }
850
851    /// The image of a thread-local whose image is all zeros costs the file nothing, the same as
852    /// `.bss` does, and the one that is not all zeros carries its bytes.
853    #[test]
854    fn the_image_of_a_thread_local_is_carried_only_when_it_is_not_all_zeros() {
855        let mut names = Interner::new();
856        let mut module = module(&mut names);
857        let value = module.add_imm(Imm::int(258, Type::int(32)));
858        let written = defined(
859            &mut module,
860            &mut names,
861            "counted",
862            &[Datum::Scalar { ty: Type::int(32), value }],
863        );
864        module[written].tls = Some(TlsModel::GlobalDynamic);
865        let zeroed = defined(&mut module, &mut names, "empty", &[Datum::Zero(4)]);
866        module[zeroed].tls = Some(TlsModel::GlobalDynamic);
867
868        let data = globals(&module, &names, ObjectFormat::Elf).expect("two thread-locals").image();
869        assert_eq!(data.objects[0].bytes, [2, 1, 0, 0]);
870        assert_eq!(data.objects[0].size, 4);
871        assert!(data.objects[1].bytes.is_empty(), "a zeroed one carries its size and no bytes");
872        assert_eq!(data.objects[1].size, 4);
873    }
874
875    /// Windows reaches a thread-local through a table of its own, which is not a section with a
876    /// flag on it, so the variable is refused by name there.
877    #[test]
878    fn a_thread_local_variable_is_refused_on_a_format_that_does_not_spell_one_this_way() {
879        let mut names = Interner::new();
880        let mut module = module(&mut names);
881        let id = defined(&mut module, &mut names, "x", &[Datum::Zero(4)]);
882        module[id].tls = Some(TlsModel::GlobalDynamic);
883        let format = ObjectFormat::Coff;
884        let error = globals(&module, &names, format).expect_err("a thread-local variable");
885        assert_eq!(error, Error::Thread { name: "x".to_owned(), format: format.as_str() });
886    }
887}