LLVM  16.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 
10 #include "llvm/BinaryFormat/ELF.h"
11 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/Support/Endian.h"
15 
16 using namespace llvm;
17 using namespace llvm::support::endian;
18 using namespace object;
19 
20 Expected<Decompressor> Decompressor::create(StringRef Name, StringRef Data,
21  bool IsLE, bool Is64Bit) {
23  if (Error Err = D.consumeCompressedHeader(Is64Bit, IsLE))
24  return std::move(Err);
25  return D;
26 }
27 
28 Decompressor::Decompressor(StringRef Data)
29  : SectionData(Data), DecompressedSize(0) {}
30 
31 Error Decompressor::consumeCompressedHeader(bool Is64Bit, bool IsLittleEndian) {
32  using namespace ELF;
33  uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);
34  if (SectionData.size() < HdrSize)
35  return createError("corrupted compressed section header");
36 
37  DataExtractor Extractor(SectionData, IsLittleEndian, 0);
38  uint64_t Offset = 0;
39  auto ChType = Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
40  : sizeof(Elf32_Word));
41  switch (ChType) {
42  case ELFCOMPRESS_ZLIB:
43  CompressionType = DebugCompressionType::Zlib;
44  break;
45  case ELFCOMPRESS_ZSTD:
46  CompressionType = DebugCompressionType::Zstd;
47  break;
48  default:
49  return createError("unsupported compression type (" + Twine(ChType) + ")");
50  }
51  if (const char *Reason = llvm::compression::getReasonIfUnsupported(
52  compression::formatFor(CompressionType)))
53  return createError(Reason);
54 
55  // Skip Elf64_Chdr::ch_reserved field.
56  if (Is64Bit)
57  Offset += sizeof(Elf64_Word);
58 
59  DecompressedSize = Extractor.getUnsigned(
60  &Offset, Is64Bit ? sizeof(Elf64_Xword) : sizeof(Elf32_Word));
61  SectionData = SectionData.substr(HdrSize);
62  return Error::success();
63 }
64 
66  return compression::decompress(CompressionType,
67  arrayRefFromStringRef(SectionData),
68  Output.data(), Output.size());
69 }
Compression.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::ELF::Elf64_Xword
uint64_t Elf64_Xword
Definition: ELF.h:40
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::ELF::ELFCOMPRESS_ZLIB
@ ELFCOMPRESS_ZLIB
Definition: ELF.h:1807
llvm::support::endian
Definition: Endian.h:42
llvm::DebugCompressionType::Zstd
@ Zstd
Zstandard.
ELF.h
llvm::MutableArrayRef< uint8_t >
llvm::ELF::ELFCOMPRESS_ZSTD
@ ELFCOMPRESS_ZSTD
Definition: ELF.h:1808
llvm::ELF::Elf64_Word
uint32_t Elf64_Word
Definition: ELF.h:38
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
uint64_t
llvm::ELF::Elf32_Word
uint32_t Elf32_Word
Definition: ELF.h:32
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
ObjectFile.h
llvm::object::Decompressor::decompress
Error decompress(MutableArrayRef< uint8_t > Output)
Uncompress section data to raw buffer provided.
Definition: Decompressor.cpp:65
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::MutableArrayRef::data
T * data() const
Definition: ArrayRef.h:353
Decompressor.h
llvm::DebugCompressionType::Zlib
@ Zlib
zlib
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
DataExtractor.h
llvm::compression::getReasonIfUnsupported
const char * getReasonIfUnsupported(Format F)
Definition: Compression.cpp:30
llvm::compression::formatFor
Format formatFor(DebugCompressionType Type)
Definition: Compression.h:81
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::object::createError
Error createError(const Twine &Err)
Definition: Error.h:84
llvm::compression::decompress
Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
Definition: Compression.cpp:58
Endian.h
llvm::object::Decompressor
Decompressor helps to handle decompression of compressed sections.
Definition: Decompressor.h:21