LLVM 19.0.0git
Decompressor.cpp
Go to the documentation of this file.
1//===-- Decompressor.cpp --------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
15#include "llvm/Support/Endian.h"
16
17using namespace llvm;
18using namespace llvm::support::endian;
19using namespace object;
20
22 bool IsLE, bool Is64Bit) {
24 if (Error Err = D.consumeCompressedHeader(Is64Bit, IsLE))
25 return std::move(Err);
26 return D;
27}
28
29Decompressor::Decompressor(StringRef Data)
30 : SectionData(Data), DecompressedSize(0) {}
31
32Error Decompressor::consumeCompressedHeader(bool Is64Bit, bool IsLittleEndian) {
33 using namespace ELF;
34 uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);
35 if (SectionData.size() < HdrSize)
36 return createError("corrupted compressed section header");
37
38 DataExtractor Extractor(SectionData, IsLittleEndian, 0);
39 uint64_t Offset = 0;
40 auto ChType = Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
41 : sizeof(Elf32_Word));
42 switch (ChType) {
43 case ELFCOMPRESS_ZLIB:
44 CompressionType = DebugCompressionType::Zlib;
45 break;
46 case ELFCOMPRESS_ZSTD:
47 CompressionType = DebugCompressionType::Zstd;
48 break;
49 default:
50 return createError("unsupported compression type (" + Twine(ChType) + ")");
51 }
52 if (const char *Reason = llvm::compression::getReasonIfUnsupported(
53 compression::formatFor(CompressionType)))
54 return createError(Reason);
55
56 // Skip Elf64_Chdr::ch_reserved field.
57 if (Is64Bit)
58 Offset += sizeof(Elf64_Word);
59
60 DecompressedSize = Extractor.getUnsigned(
61 &Offset, Is64Bit ? sizeof(Elf64_Xword) : sizeof(Elf32_Word));
62 SectionData = SectionData.substr(HdrSize);
63 return Error::success();
64}
65
67 return compression::decompress(CompressionType,
68 arrayRefFromStringRef(SectionData),
69 Output.data(), Output.size());
70}
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
std::string Name
This file contains some functions that are useful when dealing with strings.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
T * data() const
Definition: ArrayRef.h:354
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:567
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Decompressor helps to handle decompression of compressed sections.
Definition: Decompressor.h:21
Error decompress(MutableArrayRef< uint8_t > Output)
Uncompress section data to raw buffer provided.
static Expected< Decompressor > create(StringRef Name, StringRef Data, bool IsLE, bool Is64Bit)
Create decompressor object.
const char * getReasonIfUnsupported(Format F)
Definition: Compression.cpp:30
Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
Definition: Compression.cpp:58
Format formatFor(DebugCompressionType Type)
Definition: Compression.h:81
Error createError(const Twine &Err)
Definition: Error.h:84
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456