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