blob: 53f084d7620e7419a28920b85cf76a95852e33a2 [file] [log] [blame]
George Rimar4bf30832017-01-11 15:26:41 +00001//===-- 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
10#include "llvm/Object/Decompressor.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000011#include "llvm/BinaryFormat/ELF.h"
George Rimar4bf30832017-01-11 15:26:41 +000012#include "llvm/Object/ELFObjectFile.h"
13#include "llvm/Support/Compression.h"
14#include "llvm/Support/DataExtractor.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/Support/Endian.h"
George Rimar4bf30832017-01-11 15:26:41 +000016
17using namespace llvm;
18using namespace llvm::support::endian;
19using namespace object;
20
21Expected<Decompressor> Decompressor::create(StringRef Name, StringRef Data,
22 bool IsLE, bool Is64Bit) {
23 if (!zlib::isAvailable())
24 return createError("zlib is not available");
25
Vedant Kumar3ae41702017-09-05 22:04:00 +000026 Decompressor D(Data);
George Rimar4bf30832017-01-11 15:26:41 +000027 Error Err = isGnuStyle(Name) ? D.consumeCompressedGnuHeader()
28 : D.consumeCompressedZLibHeader(Is64Bit, IsLE);
29 if (Err)
30 return std::move(Err);
31 return D;
32}
33
Vedant Kumar3ae41702017-09-05 22:04:00 +000034Decompressor::Decompressor(StringRef Data)
35 : SectionData(Data), DecompressedSize(0) {}
George Rimar4bf30832017-01-11 15:26:41 +000036
37Error Decompressor::consumeCompressedGnuHeader() {
38 if (!SectionData.startswith("ZLIB"))
39 return createError("corrupted compressed section header");
40
41 SectionData = SectionData.substr(4);
42
43 // Consume uncompressed section size (big-endian 8 bytes).
44 if (SectionData.size() < 8)
45 return createError("corrupted uncompressed section size");
46 DecompressedSize = read64be(SectionData.data());
47 SectionData = SectionData.substr(8);
48
49 return Error::success();
50}
51
52Error Decompressor::consumeCompressedZLibHeader(bool Is64Bit,
53 bool IsLittleEndian) {
54 using namespace ELF;
55 uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);
56 if (SectionData.size() < HdrSize)
57 return createError("corrupted compressed section header");
58
59 DataExtractor Extractor(SectionData, IsLittleEndian, 0);
60 uint32_t Offset = 0;
61 if (Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
62 : sizeof(Elf32_Word)) !=
63 ELFCOMPRESS_ZLIB)
64 return createError("unsupported compression type");
65
66 // Skip Elf64_Chdr::ch_reserved field.
67 if (Is64Bit)
68 Offset += sizeof(Elf64_Word);
69
70 DecompressedSize = Extractor.getUnsigned(
71 &Offset, Is64Bit ? sizeof(Elf64_Xword) : sizeof(Elf32_Word));
72 SectionData = SectionData.substr(HdrSize);
73 return Error::success();
74}
75
76bool Decompressor::isGnuStyle(StringRef Name) {
77 return Name.startswith(".zdebug");
78}
79
80bool Decompressor::isCompressed(const object::SectionRef &Section) {
81 StringRef Name;
Davide Italiano3b5edf42017-01-11 19:05:27 +000082 if (Section.getName(Name))
George Rimar4bf30832017-01-11 15:26:41 +000083 return false;
84 return Section.isCompressed() || isGnuStyle(Name);
85}
86
87bool Decompressor::isCompressedELFSection(uint64_t Flags, StringRef Name) {
88 return (Flags & ELF::SHF_COMPRESSED) || isGnuStyle(Name);
89}
90
George Rimar4bf30832017-01-11 15:26:41 +000091Error Decompressor::decompress(MutableArrayRef<char> Buffer) {
92 size_t Size = Buffer.size();
George Rimar167ca4a2017-01-17 15:45:07 +000093 return zlib::uncompress(SectionData, Buffer.data(), Size);
George Rimar4bf30832017-01-11 15:26:41 +000094}