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