Decompressor.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===-- Decompressor.cpp --------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Object/Decompressor.h"
  10. #include "llvm/Object/ELFObjectFile.h"
  11. #include "llvm/Support/Compression.h"
  12. #include "llvm/Support/DataExtractor.h"
  13. #include "llvm/Support/Endian.h"
  14. #include "llvm/Support/ELF.h"
  15. using namespace llvm;
  16. using namespace llvm::support::endian;
  17. using namespace object;
  18. Expected<Decompressor> Decompressor::create(StringRef Name, StringRef Data,
  19. bool IsLE, bool Is64Bit) {
  20. if (!zlib::isAvailable())
  21. return createError("zlib is not available");
  22. Decompressor D(Data);
  23. Error Err = isGnuStyle(Name) ? D.consumeCompressedGnuHeader()
  24. : D.consumeCompressedZLibHeader(Is64Bit, IsLE);
  25. if (Err)
  26. return std::move(Err);
  27. return D;
  28. }
  29. Decompressor::Decompressor(StringRef Data)
  30. : SectionData(Data), DecompressedSize(0) {}
  31. Error Decompressor::consumeCompressedGnuHeader() {
  32. if (!SectionData.startswith("ZLIB"))
  33. return createError("corrupted compressed section header");
  34. SectionData = SectionData.substr(4);
  35. // Consume uncompressed section size (big-endian 8 bytes).
  36. if (SectionData.size() < 8)
  37. return createError("corrupted uncompressed section size");
  38. DecompressedSize = read64be(SectionData.data());
  39. SectionData = SectionData.substr(8);
  40. return Error::success();
  41. }
  42. Error Decompressor::consumeCompressedZLibHeader(bool Is64Bit,
  43. bool IsLittleEndian) {
  44. using namespace ELF;
  45. uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);
  46. if (SectionData.size() < HdrSize)
  47. return createError("corrupted compressed section header");
  48. DataExtractor Extractor(SectionData, IsLittleEndian, 0);
  49. uint32_t Offset = 0;
  50. if (Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
  51. : sizeof(Elf32_Word)) !=
  52. ELFCOMPRESS_ZLIB)
  53. return createError("unsupported compression type");
  54. // Skip Elf64_Chdr::ch_reserved field.
  55. if (Is64Bit)
  56. Offset += sizeof(Elf64_Word);
  57. DecompressedSize = Extractor.getUnsigned(
  58. &Offset, Is64Bit ? sizeof(Elf64_Xword) : sizeof(Elf32_Word));
  59. SectionData = SectionData.substr(HdrSize);
  60. return Error::success();
  61. }
  62. bool Decompressor::isGnuStyle(StringRef Name) {
  63. return Name.startswith(".zdebug");
  64. }
  65. bool Decompressor::isCompressed(const object::SectionRef &Section) {
  66. StringRef Name;
  67. if (Section.getName(Name))
  68. return false;
  69. return Section.isCompressed() || isGnuStyle(Name);
  70. }
  71. bool Decompressor::isCompressedELFSection(uint64_t Flags, StringRef Name) {
  72. return (Flags & ELF::SHF_COMPRESSED) || isGnuStyle(Name);
  73. }
  74. Error Decompressor::decompress(MutableArrayRef<char> Buffer) {
  75. size_t Size = Buffer.size();
  76. return zlib::uncompress(SectionData, Buffer.data(), Size);
  77. }