Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Object/Decompressor.cpp
Line
Count
Source (jump to first uncovered line)
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
9
#include "llvm/Object/Decompressor.h"
10
#include "llvm/BinaryFormat/ELF.h"
11
#include "llvm/Object/ELFObjectFile.h"
12
#include "llvm/Support/Compression.h"
13
#include "llvm/Support/DataExtractor.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
46
                                            bool IsLE, bool Is64Bit) {
22
46
  if (!zlib::isAvailable())
23
0
    return createError("zlib is not available");
24
46
25
46
  Decompressor D(Data);
26
46
  Error Err = isGnuStyle(Name) ? 
D.consumeCompressedGnuHeader()27
27
46
                               : 
D.consumeCompressedZLibHeader(Is64Bit, IsLE)19
;
28
46
  if (Err)
29
6
    return std::move(Err);
30
40
  return D;
31
40
}
32
33
Decompressor::Decompressor(StringRef Data)
34
46
    : SectionData(Data), DecompressedSize(0) {}
35
36
27
Error Decompressor::consumeCompressedGnuHeader() {
37
27
  if (!SectionData.startswith("ZLIB"))
38
5
    return createError("corrupted compressed section header");
39
22
40
22
  SectionData = SectionData.substr(4);
41
22
42
22
  // Consume uncompressed section size (big-endian 8 bytes).
43
22
  if (SectionData.size() < 8)
44
0
    return createError("corrupted uncompressed section size");
45
22
  DecompressedSize = read64be(SectionData.data());
46
22
  SectionData = SectionData.substr(8);
47
22
48
22
  return Error::success();
49
22
}
50
51
Error Decompressor::consumeCompressedZLibHeader(bool Is64Bit,
52
19
                                                bool IsLittleEndian) {
53
19
  using namespace ELF;
54
19
  uint64_t HdrSize = Is64Bit ? 
sizeof(Elf64_Chdr)18
:
sizeof(Elf32_Chdr)1
;
55
19
  if (SectionData.size() < HdrSize)
56
1
    return createError("corrupted compressed section header");
57
18
58
18
  DataExtractor Extractor(SectionData, IsLittleEndian, 0);
59
18
  uint32_t Offset = 0;
60
18
  if (Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
61
18
                                             : 
sizeof(Elf32_Word)0
) !=
62
18
      ELFCOMPRESS_ZLIB)
63
0
    return createError("unsupported compression type");
64
18
65
18
  // Skip Elf64_Chdr::ch_reserved field.
66
18
  if (Is64Bit)
67
18
    Offset += sizeof(Elf64_Word);
68
18
69
18
  DecompressedSize = Extractor.getUnsigned(
70
18
      &Offset, Is64Bit ? sizeof(Elf64_Xword) : 
sizeof(Elf32_Word)0
);
71
18
  SectionData = SectionData.substr(HdrSize);
72
18
  return Error::success();
73
18
}
74
75
19.6k
bool Decompressor::isGnuStyle(StringRef Name) {
76
19.6k
  return Name.startswith(".zdebug");
77
19.6k
}
78
79
19.1k
bool Decompressor::isCompressed(const object::SectionRef &Section) {
80
19.1k
  StringRef Name;
81
19.1k
  if (Section.getName(Name))
82
14
    return false;
83
19.1k
  return Section.isCompressed() || 
isGnuStyle(Name)19.1k
;
84
19.1k
}
85
86
0
bool Decompressor::isCompressedELFSection(uint64_t Flags, StringRef Name) {
87
0
  return (Flags & ELF::SHF_COMPRESSED) || isGnuStyle(Name);
88
0
}
89
90
40
Error Decompressor::decompress(MutableArrayRef<char> Buffer) {
91
40
  size_t Size = Buffer.size();
92
40
  return zlib::uncompress(SectionData, Buffer.data(), Size);
93
40
}