gsym-rs 0.1.6

Pure-Rust reader, writer, and Linux ELF/DWARF converter for LLVM GSYM
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
use std::fmt;

use crate::format::function::{check_inline_depth, check_merged_depth};
use crate::{
    Endian, Error, FileEntry, FileIndex, Function, FunctionSetPolicy, Gsym, GsymBuilder,
    GsymVersion, InlineNode, Result,
};

impl<D: AsRef<[u8]>> Gsym<D> {
    /// Decodes every file and function into an owned semantic model.
    ///
    /// A record type this crate cannot represent is rejected to prevent a
    /// lossy transformation.
    ///
    /// # Errors
    ///
    /// Returns the first structural, reference, or semantic decoding error,
    /// including a file table whose reserved entry zero is not empty.
    pub fn decode_all(&self) -> Result<DecodedGsym> {
        let (report, functions) = self.decode_all_verified()?;
        let header = self.header();
        let mut files = Vec::with_capacity(report.files.max(1));
        if report.files == 0 {
            files.push(FileEntry::default());
        }
        for index in 0..report.files {
            let index = u32::try_from(index).map_err(|_| Error::Overflow("file index"))?;
            let (directory, basename) = self.file(index)?;
            files.push(FileEntry {
                directory: directory.to_vec(),
                basename: basename.to_vec(),
            });
        }
        Ok(DecodedGsym {
            source_version: header.version,
            source_endian: header.endian,
            base_address: header.base_address,
            build_id: header.build_id.to_vec(),
            files,
            functions,
        })
    }

    /// Re-encodes this file with a selected version or byte order.
    ///
    /// # Errors
    ///
    /// Returns an error if the input is malformed or the semantic data cannot
    /// be represented by the requested output version.
    pub fn transcode(&self, options: TranscodeOptions) -> Result<Vec<u8>> {
        self.decode_all()?.transcode(options)
    }
}

/// An owned, version-independent representation of a complete GSYM file.
///
/// Decode with [`Gsym::decode_all`](crate::Gsym::decode_all), edit the public
/// semantic fields when needed, then use [`Self::into_builder`] or
/// [`Self::transcode`] to move the model into a new encoding.
///
/// A `FunctionInfo` record of a type this crate does not model makes decoding
/// fail rather than silently disappearing during re-encoding. `source_version`
/// and `source_endian` record what the input used, and [`TranscodeOptions`]
/// overrides either one for the output.
///
/// File indices in the model refer to [`Self::files`], including the reserved
/// empty entry at index zero. Re-encoding renumbers the table and keeps only
/// the files the retained functions reference.
#[derive(Eq, PartialEq)]
pub struct DecodedGsym {
    /// Version from which this model was decoded.
    pub source_version: GsymVersion,
    /// Byte order from which this model was decoded.
    pub source_endian: Endian,
    /// Image base address.
    pub base_address: u64,
    /// Opaque build identifier.
    pub build_id: Vec<u8>,
    /// Complete file table, including reserved index zero.
    pub files: Vec<FileEntry>,
    /// Fully decoded semantic functions.
    pub functions: Vec<Function>,
}

impl fmt::Debug for DecodedGsym {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DecodedGsym")
            .field("source_version", &self.source_version)
            .field("source_endian", &self.source_endian)
            .field("base_address", &self.base_address)
            .field("build_id_len", &self.build_id.len())
            .field("file_count", &self.files.len())
            .field("function_count", &self.functions.len())
            .finish_non_exhaustive()
    }
}

/// Output choices for semantic GSYM transcoding.
///
/// `Default` keeps both the version and the byte order of the input, which
/// makes a transcode a pure re-encode. Setting a field converts that property.
///
/// ```
/// use gsym::{Endian, GsymVersion, TranscodeOptions};
///
/// let keep_everything = TranscodeOptions::default();
/// assert!(keep_everything.version.is_none());
///
/// let to_big_endian_v2 = TranscodeOptions {
///     version: Some(GsymVersion::V2),
///     endian: Some(Endian::Big),
/// };
/// # let _ = to_big_endian_v2;
/// ```
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TranscodeOptions {
    /// Preserve the input version when absent.
    pub version: Option<GsymVersion>,
    /// Preserve the input byte order when absent.
    pub endian: Option<Endian>,
}

/// One independently readable shard of a segmented GSYM image.
///
/// Each segment is a complete GSYM file that [`Gsym::parse`](crate::Gsym::parse)
/// accepts on its own, holding a contiguous span of the source file's functions
/// and only the source files those functions reference. The address fields let
/// a consumer pick the right shard for an address without opening it.
///
/// Produced by [`DecodedGsym::segments`].
#[derive(Eq, PartialEq)]
#[non_exhaustive]
pub struct GsymSegment {
    /// Lowest function start address in this segment.
    pub first_address: u64,
    /// Exclusive end of this segment's span, equal to the next segment's
    /// [`first_address`](Self::first_address).
    pub end_address: u64,
    /// Number of top-level functions in this segment.
    pub function_count: usize,
    bytes: Box<[u8]>,
}

impl GsymSegment {
    /// Returns the complete independently readable GSYM image.
    #[must_use]
    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Returns the owned GSYM image.
    #[must_use]
    pub fn into_bytes(self) -> Box<[u8]> {
        self.bytes
    }
}

impl fmt::Debug for GsymSegment {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("GsymSegment")
            .field("first_address", &self.first_address)
            .field("end_address", &self.end_address)
            .field("function_count", &self.function_count)
            .field("byte_len", &self.bytes.len())
            .finish()
    }
}

impl DecodedGsym {
    /// Builds a deterministic writer model by cloning this decoded model.
    ///
    /// Use [`Self::into_builder`] when this model is no longer needed.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid file references or model data.
    pub fn to_builder(&self, options: TranscodeOptions) -> Result<GsymBuilder> {
        self.builder_for_functions(self.functions.iter(), options)
    }

    /// Converts this decoded model into a deterministic writer without cloning
    /// its file or function trees.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid file references or model data.
    pub fn into_builder(self, options: TranscodeOptions) -> Result<GsymBuilder> {
        let Self {
            source_version,
            source_endian,
            base_address,
            build_id,
            files,
            functions,
        } = self;
        let used = used_files(&files, &functions)?;
        let mut builder = new_builder(
            source_version,
            source_endian,
            base_address,
            build_id,
            options,
        );
        let mut remap = vec![FileIndex::ZERO; files.len()];
        for (old, file) in files.into_iter().enumerate().skip(1) {
            if used.get(old).copied().unwrap_or(false)
                && let Some(slot) = remap.get_mut(old)
            {
                *slot = builder.add_file(file)?;
            }
        }
        for mut function in functions {
            remap_function_files(&mut function, &remap)?;
            builder.add_function(function)?;
        }
        Ok(builder)
    }

    /// Encodes the complete model using the requested output settings.
    ///
    /// ```
    /// use gsym::{
    ///     AddressRange, Endian, Function, Gsym, GsymBuilder, GsymVersion,
    ///     TranscodeOptions,
    /// };
    ///
    /// let mut builder = GsymBuilder::new();
    /// builder.add_function(Function::new(
    ///     AddressRange::new(0x4000, 0x4010),
    ///     b"transcoded",
    /// ))?;
    /// let source = builder.to_bytes()?;
    /// let decoded = Gsym::parse(source)?.decode_all()?;
    /// let output = decoded.transcode(TranscodeOptions {
    ///     version: Some(GsymVersion::V2),
    ///     endian: Some(Endian::Big),
    /// })?;
    ///
    /// let reparsed = Gsym::parse(output)?;
    /// assert_eq!(reparsed.header().version, GsymVersion::V2);
    /// assert_eq!(reparsed.header().endian, Endian::Big);
    /// # Ok::<(), gsym::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error when the model cannot be represented by the requested
    /// format version.
    pub fn transcode(self, options: TranscodeOptions) -> Result<Vec<u8>> {
        self.into_builder(options)?.to_bytes()
    }

    /// Splits the model into independently valid GSYM files near `target_size`.
    ///
    /// A single record larger than the target is emitted alone. Each shard
    /// contains only source files referenced by its functions. Boundaries are
    /// selected from exact encoded sizes, so all multi-function shards are at
    /// most the requested target.
    ///
    /// Shards are ordered by address and their `[first_address, end_address)`
    /// spans tile the covered range without a gap, so
    /// [`GsymSegment::first_address`] and [`GsymSegment::end_address`] are
    /// enough to route an address to a shard: the shard whose span holds an
    /// address is the shard that resolves it, and an address below the first
    /// shard resolves nowhere, exactly as in the unsplit file. Segmentation is
    /// considerably more expensive than a single encode.
    ///
    /// # Errors
    ///
    /// Returns an error for a zero target, empty model, invalid references, or
    /// an encoding failure.
    pub fn segments(
        &self,
        target_size: usize,
        options: TranscodeOptions,
    ) -> Result<Vec<GsymSegment>> {
        if target_size == 0 {
            return Err(Error::InvalidModel("segment target size must not be zero"));
        }
        if self.functions.is_empty() {
            return Err(Error::InvalidModel("at least one function is required"));
        }
        let mut functions = self.functions.iter().collect::<Vec<_>>();
        functions.sort_by_key(|function| (function.range.start, function.range.end));
        let mut segments = Vec::new();
        let mut start = 0;
        while start < functions.len() {
            let minimum = start.saturating_add(1);
            let mut best = minimum;
            let window = |end: usize| {
                functions
                    .get(start..end)
                    .ok_or(Error::InvalidModel("segment partition is out of range"))
            };
            let mut best_bytes = self
                .builder_for_functions(window(best)?.iter().copied(), options)?
                .to_bytes()?;
            let mut ceiling = functions.len().saturating_add(1);
            let mut span = 1_usize;
            while best < functions.len() {
                let candidate = minimum.saturating_add(span).min(functions.len());
                if candidate <= best {
                    break;
                }
                let bytes = self
                    .builder_for_functions(window(candidate)?.iter().copied(), options)?
                    .to_bytes()?;
                if bytes.len() > target_size {
                    ceiling = candidate;
                    break;
                }
                best = candidate;
                best_bytes = bytes;
                span = span.saturating_mul(2);
            }
            let mut low = best.saturating_add(1);
            let mut high = ceiling.saturating_sub(1);
            while low <= high && high <= functions.len() {
                let middle = low.saturating_add(high.saturating_sub(low) / 2);
                let bytes = self
                    .builder_for_functions(window(middle)?.iter().copied(), options)?
                    .to_bytes()?;
                if bytes.len() <= target_size {
                    best = middle;
                    best_bytes = bytes;
                    low = middle.saturating_add(1);
                } else {
                    high = middle.saturating_sub(1);
                }
            }
            let selected = window(best)?;
            let first = selected
                .first()
                .ok_or(Error::InvalidModel("segment partition is empty"))?;
            let last = selected
                .last()
                .ok_or(Error::InvalidModel("segment partition is empty"))?;
            segments.push(GsymSegment {
                first_address: first.range.start,
                end_address: match functions.get(best) {
                    Some(next) => next.range.start,
                    None if last.range.is_empty() => u64::MAX,
                    None => selected
                        .iter()
                        .map(|function| function.range.end)
                        .max()
                        .unwrap_or(first.range.end),
                },
                function_count: selected.len(),
                bytes: best_bytes.into_boxed_slice(),
            });
            start = best;
        }
        Ok(segments)
    }

    fn builder_for_functions<'function>(
        &self,
        functions: impl Clone + IntoIterator<Item = &'function Function>,
        options: TranscodeOptions,
    ) -> Result<GsymBuilder> {
        let used = used_files(&self.files, functions.clone())?;
        let mut builder = new_builder(
            self.source_version,
            self.source_endian,
            self.base_address,
            self.build_id.clone(),
            options,
        );
        let mut remap = vec![FileIndex::ZERO; self.files.len()];
        for (old, file) in self.files.iter().enumerate().skip(1) {
            if used.get(old).copied().unwrap_or(false)
                && let Some(slot) = remap.get_mut(old)
            {
                *slot = builder.add_file(file.clone())?;
            }
        }
        for function in functions {
            let mut function = function.clone();
            remap_function_files(&mut function, &remap)?;
            builder.add_function(function)?;
        }
        Ok(builder)
    }
}

fn used_files<'function>(
    files: &[FileEntry],
    functions: impl IntoIterator<Item = &'function Function>,
) -> Result<Vec<bool>> {
    if files
        .first()
        .is_none_or(|file| *file != FileEntry::default())
    {
        return Err(Error::InvalidModel("file-table index zero must be empty"));
    }
    let mut used = vec![false; files.len()];
    if let Some(zero) = used.first_mut() {
        *zero = true;
    }
    for function in functions {
        mark_function_files(function, &mut used)?;
    }
    Ok(used)
}

fn new_builder(
    source_version: GsymVersion,
    source_endian: Endian,
    base_address: u64,
    build_id: Vec<u8>,
    options: TranscodeOptions,
) -> GsymBuilder {
    GsymBuilder::new()
        .version(options.version.unwrap_or(source_version))
        .endian(options.endian.unwrap_or(source_endian))
        .base_address(base_address)
        .build_id(build_id)
        .repair_zero_sized_functions(false)
        .function_set(FunctionSetPolicy::Preserve)
}

fn mark_file(index: FileIndex, used: &mut [bool]) -> Result<()> {
    let slot = used
        .get_mut(index.get() as usize)
        .ok_or(Error::InvalidModel("function references a missing file"))?;
    *slot = true;
    Ok(())
}

fn mark_inline_files(node: &InlineNode, used: &mut [bool], depth: usize) -> Result<()> {
    check_inline_depth(depth)?;
    mark_file(node.call_file, used)?;
    for child in &node.children {
        mark_inline_files(child, used, depth.saturating_add(1))?;
    }
    Ok(())
}

fn mark_function_files(function: &Function, used: &mut [bool]) -> Result<()> {
    mark_function_files_at(function, used, 0)
}

fn mark_function_files_at(function: &Function, used: &mut [bool], depth: usize) -> Result<()> {
    check_merged_depth(depth)?;
    for line in &function.lines {
        mark_file(line.file, used)?;
    }
    if let Some(inline) = &function.inline {
        mark_inline_files(inline, used, 0)?;
    }
    for merged in &function.merged {
        mark_function_files_at(merged, used, depth.saturating_add(1))?;
    }
    Ok(())
}

fn remap_file(index: &mut FileIndex, remap: &[FileIndex]) -> Result<()> {
    *index = *remap
        .get(index.get() as usize)
        .ok_or(Error::InvalidModel("function references a missing file"))?;
    Ok(())
}

fn remap_inline_files(node: &mut InlineNode, remap: &[FileIndex], depth: usize) -> Result<()> {
    check_inline_depth(depth)?;
    remap_file(&mut node.call_file, remap)?;
    for child in &mut node.children {
        remap_inline_files(child, remap, depth.saturating_add(1))?;
    }
    Ok(())
}

fn remap_function_files(function: &mut Function, remap: &[FileIndex]) -> Result<()> {
    remap_function_files_at(function, remap, 0)
}

fn remap_function_files_at(
    function: &mut Function,
    remap: &[FileIndex],
    depth: usize,
) -> Result<()> {
    check_merged_depth(depth)?;
    for line in &mut function.lines {
        remap_file(&mut line.file, remap)?;
    }
    if let Some(inline) = &mut function.inline {
        remap_inline_files(inline, remap, 0)?;
    }
    for merged in &mut function.merged {
        remap_function_files_at(merged, remap, depth.saturating_add(1))?;
    }
    Ok(())
}