qld 0.1.1

A fast, parallel linker compatible with GNU ld, gold, lld and mold
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Module-definition (`.def`) files.
//!
//! The grammar accepted is the union of what GNU `dlltool`/ld
//! (`deffilep.y`) and LLVM (`COFFModuleDefinition.cpp`, used by lld and
//! `llvm-dlltool`) accept in their MinGW modes:
//!
//! ```text
//! LIBRARY [name] [BASE=address]          ; or NAME for an executable
//! DESCRIPTION "text"
//! STACKSIZE reserve[,commit]
//! HEAPSIZE reserve[,commit]
//! VERSION major[.minor]
//! SECTIONS
//!     name [READ] [WRITE] [EXECUTE] [SHARED]
//! EXPORTS
//!     name[=internal] [@ordinal [NONAME]] [DATA] [PRIVATE] [CONSTANT]
//!          [==importname] [EXPORTAS name]
//! IMPORTS
//!     [internal=]module.name|module.ordinal [==name]
//! ```
//!
//! Lexing follows LLVM: `;` starts a comment that runs to the end of the
//! line; `=`, `==` and `,` are tokens; a word runs to the next `=`, `,`,
//! `;` or whitespace (so `kernel32.Sleep` and `foo@4` are single words);
//! `"..."` (and, as in GNU, `'...'`) quotes a word. Keywords are
//! upper-case. Where the two implementations differ, qld accepts both:
//!
//! - `NONAME`, `DATA`, `PRIVATE` and `CONSTANT` are also accepted in lower
//!   case, as in GNU (lld reads a lower-case `data` as the next export's
//!   name);
//! - flags may be separated by commas, as in GNU;
//! - numbers may be hexadecimal with `0x`, as in GNU (lld accepts only
//!   decimal); a leading `0` does not make a number octal, unlike GNU;
//! - `DESCRIPTION`, `SECTIONS`, `IMPORTS` and `NONAME` without an ordinal
//!   are GNU extensions that lld rejects.
//!
//! Names are kept exactly as written; adding the i386 `_` prefix is the
//! linker's decision.

use std::borrow::Cow;

use super::export::ExportSpec;
use super::source::{Source, to_u64};
use crate::error::{Error, Result};

/// Whether the file declared a DLL or an executable.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ModuleKind {
    /// `LIBRARY`.
    Library,
    /// `NAME`.
    Executable,
}

/// A `SECTIONS` entry.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DefSection<'a> {
    /// Section name.
    pub name: &'a [u8],
    /// `READ`.
    pub read: bool,
    /// `WRITE`.
    pub write: bool,
    /// `EXECUTE`.
    pub execute: bool,
    /// `SHARED`.
    pub shared: bool,
}

/// An `IMPORTS` entry (GNU only).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DefImport<'a> {
    /// `internal=`: the local name, if different.
    pub internal_name: Option<&'a [u8]>,
    /// The module (DLL name without extension, or with it).
    pub module: &'a [u8],
    /// The imported name, when importing by name.
    pub name: Option<&'a [u8]>,
    /// The imported ordinal, when importing by ordinal.
    pub ordinal: Option<u16>,
    /// `==name`: the name the import is known by.
    pub import_name: Option<&'a [u8]>,
}

/// A parsed module-definition file.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ModuleDefinition<'a> {
    /// `LIBRARY` or `NAME`, whichever came last.
    pub kind: Option<ModuleKind>,
    /// The name after `LIBRARY` or `NAME`.
    pub name: Option<&'a [u8]>,
    /// `BASE=`.
    pub image_base: Option<u64>,
    /// `DESCRIPTION`.
    pub description: Option<&'a [u8]>,
    /// `STACKSIZE` reserve.
    pub stack_reserve: Option<u64>,
    /// `STACKSIZE` commit.
    pub stack_commit: Option<u64>,
    /// `HEAPSIZE` reserve.
    pub heap_reserve: Option<u64>,
    /// `HEAPSIZE` commit.
    pub heap_commit: Option<u64>,
    /// `VERSION` major.
    pub major_image_version: Option<u32>,
    /// `VERSION` minor.
    pub minor_image_version: Option<u32>,
    /// `EXPORTS` entries, in file order.
    pub exports: Vec<ExportSpec<'a>>,
    /// `SECTIONS` entries, in file order.
    pub sections: Vec<DefSection<'a>>,
    /// `IMPORTS` entries, in file order.
    pub imports: Vec<DefImport<'a>>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kw {
    Base,
    Code,
    Constant,
    Data,
    Description,
    Exports,
    ExportAs,
    Heapsize,
    Imports,
    Library,
    Name,
    Noname,
    Private,
    Sections,
    Stacksize,
    Version,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Tok<'a> {
    Eof,
    Ident(&'a [u8]),
    Comma,
    Equal,
    EqualEqual,
    Kw(Kw, &'a [u8]),
}

fn keyword(word: &[u8]) -> Option<Kw> {
    Some(match word {
        b"BASE" => Kw::Base,
        b"CODE" => Kw::Code,
        b"CONSTANT" | b"constant" => Kw::Constant,
        b"DATA" | b"data" => Kw::Data,
        b"DESCRIPTION" => Kw::Description,
        b"EXPORTS" => Kw::Exports,
        b"EXPORTAS" => Kw::ExportAs,
        b"HEAPSIZE" => Kw::Heapsize,
        b"IMPORTS" => Kw::Imports,
        b"LIBRARY" => Kw::Library,
        b"NAME" => Kw::Name,
        b"NONAME" | b"noname" => Kw::Noname,
        b"PRIVATE" | b"private" => Kw::Private,
        b"SECTIONS" => Kw::Sections,
        b"STACKSIZE" => Kw::Stacksize,
        b"VERSION" => Kw::Version,
        _ => return None,
    })
}

struct Parser<'a> {
    data: &'a [u8],
    pos: usize,
    /// Start of the last token read.
    start: usize,
    /// A token pushed back by `unget`, with its start.
    pending: Option<(Tok<'a>, usize)>,
    last: Option<(Tok<'a>, usize)>,
    source: Source<'a>,
}

impl<'a> Parser<'a> {
    fn lex(&mut self) -> Result<Tok<'a>> {
        loop {
            let rest = self.data.get(self.pos..).unwrap_or_default();
            let skip = rest
                .iter()
                .position(|c| !c.is_ascii_whitespace() && *c != 0x0b)
                .unwrap_or(rest.len());
            self.pos = self.pos.saturating_add(skip);
            self.start = self.pos;
            let Some(&c) = self.data.get(self.pos) else {
                return Ok(Tok::Eof);
            };
            let rest = self.data.get(self.pos..).unwrap_or_default();
            match c {
                0 => return Ok(Tok::Eof),
                b';' => {
                    let line = rest.iter().position(|&c| c == b'\n').unwrap_or(rest.len());
                    self.pos = self.pos.saturating_add(line);
                }
                b'=' => {
                    if rest.starts_with(b"==") {
                        self.pos = self.pos.saturating_add(2);
                        return Ok(Tok::EqualEqual);
                    }
                    self.pos = self.pos.saturating_add(1);
                    return Ok(Tok::Equal);
                }
                b',' => {
                    self.pos = self.pos.saturating_add(1);
                    return Ok(Tok::Comma);
                }
                b'"' | b'\'' => {
                    let body = rest.get(1..).unwrap_or_default();
                    let len = body
                        .iter()
                        .position(|&q| q == c)
                        .ok_or_else(|| self.error("unterminated quoted name"))?;
                    self.pos = self.pos.saturating_add(len).saturating_add(2);
                    return Ok(Tok::Ident(body.get(..len).unwrap_or_default()));
                }
                _ => {
                    let len = rest
                        .iter()
                        .position(|&c| {
                            matches!(c, b'=' | b',' | b';' | b'\r' | b'\n' | b' ' | b'\t' | 0x0b)
                        })
                        .unwrap_or(rest.len());
                    let word = rest.get(..len).unwrap_or_default();
                    self.pos = self.pos.saturating_add(len);
                    return Ok(match keyword(word) {
                        Some(kw) => Tok::Kw(kw, word),
                        None => Tok::Ident(word),
                    });
                }
            }
        }
    }

    fn read(&mut self) -> Result<Tok<'a>> {
        let tok = match self.pending.take() {
            Some((tok, start)) => {
                self.start = start;
                tok
            }
            None => self.lex()?,
        };
        self.last = Some((tok, self.start));
        Ok(tok)
    }

    fn unget(&mut self) {
        self.pending = self.last.take();
    }

    #[cold]
    fn error(&self, what: &str) -> Error {
        let before = self.data.get(..self.start).unwrap_or_default();
        let line = before
            .iter()
            .filter(|&&c| c == b'\n')
            .count()
            .saturating_add(1);
        self.source.malformed(
            to_u64(self.start),
            format!("module-definition file (line {line}: {what})"),
        )
    }

    fn identifier(&mut self, what: &str) -> Result<&'a [u8]> {
        match self.read()? {
            Tok::Ident(word) => Ok(word),
            _ => Err(self.error(what)),
        }
    }

    fn number(&mut self) -> Result<u64> {
        let word = self.identifier("expected a number")?;
        parse_def_number(word).ok_or_else(|| self.error("invalid number"))
    }

    fn parse(mut self) -> Result<ModuleDefinition<'a>> {
        let mut def = ModuleDefinition::default();
        loop {
            match self.read()? {
                Tok::Eof => return Ok(def),
                Tok::Kw(Kw::Exports, _) => loop {
                    match self.read()? {
                        Tok::Ident(name) => {
                            let export = self.export(name)?;
                            def.exports.push(export);
                        }
                        _ => {
                            self.unget();
                            break;
                        }
                    }
                },
                Tok::Kw(kind @ (Kw::Library | Kw::Name), _) => {
                    def.kind = Some(if kind == Kw::Library {
                        ModuleKind::Library
                    } else {
                        ModuleKind::Executable
                    });
                    def.name = match self.read()? {
                        Tok::Ident(name) => Some(name),
                        _ => {
                            self.unget();
                            None
                        }
                    };
                    if let Tok::Kw(Kw::Base, _) = self.read()? {
                        if self.read()? != Tok::Equal {
                            return Err(self.error("expected `=` after BASE"));
                        }
                        def.image_base = Some(self.number()?);
                    } else {
                        self.unget();
                    }
                }
                Tok::Kw(Kw::Description, _) => {
                    def.description = Some(self.identifier("expected a description")?);
                }
                Tok::Kw(kw @ (Kw::Stacksize | Kw::Heapsize), _) => {
                    let reserve = self.number()?;
                    let commit = if self.read()? == Tok::Comma {
                        Some(self.number()?)
                    } else {
                        self.unget();
                        None
                    };
                    if kw == Kw::Stacksize {
                        def.stack_reserve = Some(reserve);
                        def.stack_commit = commit;
                    } else {
                        def.heap_reserve = Some(reserve);
                        def.heap_commit = commit;
                    }
                }
                Tok::Kw(Kw::Version, _) => {
                    let word = self.identifier("expected a version")?;
                    let (major, minor) = match word.iter().position(|&c| c == b'.') {
                        Some(dot) => (
                            word.get(..dot).unwrap_or_default(),
                            word.get(dot.saturating_add(1)..),
                        ),
                        None => (word, None),
                    };
                    let parse =
                        |part: &[u8]| parse_def_number(part).and_then(|v| u32::try_from(v).ok());
                    def.major_image_version =
                        Some(parse(major).ok_or_else(|| self.error("invalid version"))?);
                    def.minor_image_version = Some(match minor {
                        Some(minor) => parse(minor).ok_or_else(|| self.error("invalid version"))?,
                        None => 0,
                    });
                }
                Tok::Kw(Kw::Sections, _) => self.sections(&mut def)?,
                Tok::Kw(Kw::Imports, _) => self.imports(&mut def)?,
                Tok::Kw(kw @ (Kw::Code | Kw::Data), _) => {
                    // GNU `CODE attrs` / `DATA attrs`: attributes of the
                    // default code or data section.
                    let name: &'a [u8] = if kw == Kw::Code { b"CODE" } else { b"DATA" };
                    let section = self.section_attributes(name)?;
                    def.sections.push(section);
                }
                _ => return Err(self.error("unknown directive")),
            }
        }
    }

    fn export(&mut self, name: &'a [u8]) -> Result<ExportSpec<'a>> {
        let mut export = ExportSpec {
            name: Cow::Borrowed(name),
            ..ExportSpec::default()
        };
        if self.read()? == Tok::Equal {
            export.internal_name =
                Some(Cow::Borrowed(self.identifier("expected a name after `=`")?));
        } else {
            self.unget();
        }
        loop {
            match self.read()? {
                Tok::Ident(word) if word.first() == Some(&b'@') => {
                    let digits = word.get(1..).unwrap_or_default();
                    let ordinal = if digits.is_empty() {
                        // "foo @ 10"
                        let number = self.number()?;
                        Some(u16::try_from(number).map_err(|_| self.error("invalid ordinal"))?)
                    } else {
                        parse_def_number(digits).and_then(|v| u16::try_from(v).ok())
                    };
                    let Some(ordinal) = ordinal else {
                        // "foo \n @bar": the next export, fastcall-decorated.
                        self.unget();
                        return Ok(export);
                    };
                    export.ordinal = Some(ordinal);
                }
                Tok::Kw(Kw::Noname, _) => export.noname = true,
                Tok::Kw(Kw::Data, _) => export.data = true,
                Tok::Kw(Kw::Constant, _) => export.constant = true,
                Tok::Kw(Kw::Private, _) => export.private = true,
                Tok::Comma => {}
                Tok::EqualEqual => {
                    export.import_name = Some(Cow::Borrowed(
                        self.identifier("expected a name after `==`")?,
                    ));
                }
                Tok::Kw(Kw::ExportAs, _) => {
                    export.export_as = Some(Cow::Borrowed(
                        self.identifier("expected a name after EXPORTAS")?,
                    ));
                    return Ok(export);
                }
                _ => {
                    self.unget();
                    return Ok(export);
                }
            }
        }
    }

    fn section_attributes(&mut self, name: &'a [u8]) -> Result<DefSection<'a>> {
        let mut section = DefSection {
            name,
            read: false,
            write: false,
            execute: false,
            shared: false,
        };
        loop {
            match self.read()? {
                Tok::Ident(b"READ") => section.read = true,
                Tok::Ident(b"WRITE") => section.write = true,
                Tok::Ident(b"EXECUTE") => section.execute = true,
                Tok::Ident(b"SHARED") => section.shared = true,
                Tok::Comma => {}
                _ => {
                    self.unget();
                    return Ok(section);
                }
            }
        }
    }

    fn sections(&mut self, def: &mut ModuleDefinition<'a>) -> Result<()> {
        loop {
            match self.read()? {
                Tok::Ident(name) => {
                    let section = self.section_attributes(name)?;
                    def.sections.push(section);
                }
                _ => {
                    self.unget();
                    return Ok(());
                }
            }
        }
    }

    fn imports(&mut self, def: &mut ModuleDefinition<'a>) -> Result<()> {
        loop {
            let first = match self.read()? {
                Tok::Ident(word) => word,
                _ => {
                    self.unget();
                    return Ok(());
                }
            };
            let (internal_name, target) = if self.read()? == Tok::Equal {
                (Some(first), self.identifier("expected module.name")?)
            } else {
                self.unget();
                (None, first)
            };
            let dot = target
                .iter()
                .rposition(|&c| c == b'.')
                .ok_or_else(|| self.error("expected module.name"))?;
            let module = target.get(..dot).unwrap_or_default();
            let entry = target.get(dot.saturating_add(1)..).unwrap_or_default();
            let (name, ordinal) = if entry.first().is_some_and(u8::is_ascii_digit) {
                let ordinal = parse_def_number(entry)
                    .and_then(|v| u16::try_from(v).ok())
                    .ok_or_else(|| self.error("invalid ordinal"))?;
                (None, Some(ordinal))
            } else {
                (Some(entry), None)
            };
            let import_name = if self.read()? == Tok::EqualEqual {
                Some(self.identifier("expected a name after `==`")?)
            } else {
                self.unget();
                None
            };
            def.imports.push(DefImport {
                internal_name,
                module,
                name,
                ordinal,
                import_name,
            });
        }
    }
}

/// Parses a `.def` number: decimal, or hexadecimal with `0x`.
fn parse_def_number(word: &[u8]) -> Option<u64> {
    match word
        .strip_prefix(b"0x")
        .or_else(|| word.strip_prefix(b"0X"))
    {
        Some(hex) => super::directives::parse_radix(hex, 16),
        None => super::directives::parse_radix(word, 10),
    }
}

/// Parses a module-definition file.
///
/// # Errors
///
/// Returns `Error::Malformed` naming the line of the first syntax error.
pub fn parse_module_definition<'a>(
    data: &'a [u8],
    source: Source<'a>,
) -> Result<ModuleDefinition<'a>> {
    Parser {
        data,
        pos: 0,
        start: 0,
        pending: None,
        last: None,
        source,
    }
    .parse()
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::*;

    fn parse(text: &'static str) -> Result<ModuleDefinition<'static>> {
        parse_module_definition(text.as_bytes(), Source::new(Path::new("x.def")))
    }

    #[test]
    fn full_file() {
        let def = parse(
            "; comment\n\
             LIBRARY \"foo.dll\" BASE=0x10000000\n\
             DESCRIPTION 'a library'\n\
             STACKSIZE 0x100000, 4096\n\
             HEAPSIZE 1048576\n\
             VERSION 1.2\n\
             EXPORTS\n\
             \x20 plain\n\
             \x20 ext=internal @3 NONAME\n\
             \x20 var DATA ; trailing\n\
             \x20 fwd = kernel32.Sleep\n\
             \x20 _fast@8 @ 7, PRIVATE\n\
             \x20 @fastcall@4\n\
             \x20 imp==realname CONSTANT\n\
             \x20 alias EXPORTAS real\n\
             \x20 lower data private\n\
             SECTIONS\n\
             \x20 .shared READ WRITE SHARED\n\
             IMPORTS\n\
             \x20 local=user32.MessageBoxA\n\
             \x20 gdi32.12\n",
        )
        .unwrap();
        assert_eq!(def.kind, Some(ModuleKind::Library));
        assert_eq!(def.name, Some(&b"foo.dll"[..]));
        assert_eq!(def.image_base, Some(0x1000_0000));
        assert_eq!(def.description, Some(&b"a library"[..]));
        assert_eq!(
            (def.stack_reserve, def.stack_commit),
            (Some(0x10_0000), Some(4096))
        );
        assert_eq!((def.heap_reserve, def.heap_commit), (Some(1_048_576), None));
        assert_eq!(
            (def.major_image_version, def.minor_image_version),
            (Some(1), Some(2))
        );
        let names: Vec<_> = def.exports.iter().map(|e| &*e.name).collect();
        assert_eq!(
            names,
            [
                &b"plain"[..],
                b"ext",
                b"var",
                b"fwd",
                b"_fast@8",
                b"@fastcall@4",
                b"imp",
                b"alias",
                b"lower"
            ]
        );
        let e = &def.exports[1];
        assert_eq!(e.internal_name.as_deref(), Some(&b"internal"[..]));
        assert_eq!(e.ordinal, Some(3));
        assert!(e.noname);
        assert!(def.exports[2].data);
        assert_eq!(def.exports[3].forwarder(), Some(&b"kernel32.Sleep"[..]));
        assert_eq!(def.exports[4].ordinal, Some(7));
        assert!(def.exports[4].private);
        assert_eq!(def.exports[5].ordinal, None);
        assert_eq!(
            def.exports[6].import_name.as_deref(),
            Some(&b"realname"[..])
        );
        assert!(def.exports[6].constant);
        assert_eq!(def.exports[7].export_as.as_deref(), Some(&b"real"[..]));
        assert!(def.exports[8].data && def.exports[8].private);
        assert_eq!(
            def.sections,
            [DefSection {
                name: b".shared",
                read: true,
                write: true,
                execute: false,
                shared: true
            }]
        );
        assert_eq!(def.imports.len(), 2);
        assert_eq!(def.imports[0].internal_name, Some(&b"local"[..]));
        assert_eq!(def.imports[0].module, b"user32");
        assert_eq!(def.imports[0].name, Some(&b"MessageBoxA"[..]));
        assert_eq!(def.imports[1].ordinal, Some(12));
    }

    #[test]
    fn name_and_errors() {
        let def = parse("NAME app.exe\nEXPORTS\n").unwrap();
        assert_eq!(def.kind, Some(ModuleKind::Executable));
        let def = parse("LIBRARY\nEXPORTS foo").unwrap();
        assert_eq!(def.name, None);
        assert_eq!(def.exports.len(), 1);
        assert!(parse("").unwrap().exports.is_empty());
        for bad in [
            "BOGUS",
            "LIBRARY foo BASE 5",
            "LIBRARY foo BASE=zz",
            "EXPORTS foo=",
            "EXPORTS foo @ x",
            "EXPORTS foo EXPORTAS",
            "EXPORTS \"foo",
            "VERSION a.b",
            "STACKSIZE",
            "IMPORTS nodot",
        ] {
            let error = parse(bad).unwrap_err().to_string();
            assert!(error.contains("line 1"), "{bad}: {error}");
        }
        let error = parse("EXPORTS\n  foo\n  BASE\n").unwrap_err().to_string();
        assert!(error.contains("line 3"), "{error}");
    }
}