Skip to content

Commit

Permalink
Revert "[Support] Add llvm::compression::{getReasonIfUnsupported,comp…
Browse files Browse the repository at this point in the history
…ress,decompress}"

This reverts commit 19dc3cf.
This reverts commit 5b19a1f.
This reverts commit 9397648.
This reverts commit 10842b4.

Breaks the GCC build, as reported here:
https://reviews.llvm.org/D130506#3776415
  • Loading branch information
nikic committed Sep 8, 2022
1 parent 10842b4 commit 0444b40
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 411 deletions.
2 changes: 1 addition & 1 deletion llvm/docs/CommandGuide/llvm-objcopy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ them.
.. option:: --compress-debug-sections [<format>]

Compress DWARF debug sections in the output, using the specified format.
Supported formats are ``zlib`` and ``zstd``. Use ``zlib`` if ``<format>`` is omitted.
Supported formats are ``zlib``. Use ``zlib`` if ``<format>`` is omitted.

.. option:: --decompress-debug-sections

Expand Down
1 change: 0 additions & 1 deletion llvm/include/llvm/BinaryFormat/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,6 @@ struct Elf64_Nhdr {
// Legal values for ch_type field of compressed section header.
enum {
ELFCOMPRESS_ZLIB = 1, // ZLIB/DEFLATE algorithm.
ELFCOMPRESS_ZSTD = 2, // Zstandard algorithm
ELFCOMPRESS_LOOS = 0x60000000, // Start of OS-specific.
ELFCOMPRESS_HIOS = 0x6fffffff, // End of OS-specific.
ELFCOMPRESS_LOPROC = 0x70000000, // Start of processor-specific.
Expand Down
6 changes: 5 additions & 1 deletion llvm/include/llvm/MC/MCTargetOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define LLVM_MC_MCTARGETOPTIONS_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compression.h"
#include <string>
#include <vector>

Expand All @@ -26,6 +25,11 @@ enum class ExceptionHandling {
AIX, ///< AIX Exception Handling
};

enum class DebugCompressionType {
None, ///< No compression
Z, ///< zlib style complession
};

enum class EmitDwarfUnwindType {
Always, // Always emit dwarf unwind
NoCompactUnwind, // Only emit if compact unwind isn't available
Expand Down
56 changes: 0 additions & 56 deletions llvm/include/llvm/Support/Compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ namespace llvm {
template <typename T> class SmallVectorImpl;
class Error;

// None indicates no compression. The other members are a subset of
// compression::Format, which is used for compressed debug sections in some
// object file formats (e.g. ELF). This is a separate class as we may add new
// compression::Format members for non-debugging purposes.
enum class DebugCompressionType {
None, ///< No compression
Z, ///< zlib
Zstd, ///< Zstandard
};

namespace compression {
namespace zlib {

Expand Down Expand Up @@ -75,52 +65,6 @@ Error uncompress(ArrayRef<uint8_t> Input,

} // End of namespace zstd

enum class Format {
Zlib,
Zstd,
};

inline Format formatFor(DebugCompressionType Type) {
switch (Type) {
case DebugCompressionType::None:
llvm_unreachable("not a compression type");
case DebugCompressionType::Z:
return Format::Zlib;
case DebugCompressionType::Zstd:
return Format::Zstd;
}
llvm_unreachable("invalid type");
}

struct Params {
constexpr Params(Format F)
: Format(F), Level(F == Format::Zlib ? zlib::DefaultCompression
: zstd::DefaultCompression) {}
Params(DebugCompressionType Type) : Params(formatFor(Type)) {}

Format Format;
int Level;
// This may support multi-threading for zstd in the future. Note that
// different threads may produce different output, so be careful if certain
// output determinism is desired.
};

// Return nullptr if LLVM was built with support (LLVM_ENABLE_ZLIB,
// LLVM_ENABLE_ZSTD) for the specified compression format; otherwise
// return a string literal describing the reason.
const char *getReasonIfUnsupported(Format F);

// Compress Input with the specified format P.Format. If Level is -1, use
// *::DefaultCompression for the format.
void compress(Params P, ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &Output);

// Decompress Input. The uncompressed size must be available.
Error decompress(Format F, ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &Output, size_t UncompressedSize);
Error decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &Output, size_t UncompressedSize);

} // End of namespace compression

} // End of namespace llvm
Expand Down
31 changes: 12 additions & 19 deletions llvm/lib/MC/ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,34 +848,27 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
auto &MC = Asm.getContext();
const auto &MAI = MC.getAsmInfo();

const DebugCompressionType CompressionType = MAI->compressDebugSections();
if (CompressionType == DebugCompressionType::None ||
!SectionName.startswith(".debug_")) {
bool CompressionEnabled =
MAI->compressDebugSections() != DebugCompressionType::None;
if (!CompressionEnabled || !SectionName.startswith(".debug_")) {
Asm.writeSectionData(W.OS, &Section, Layout);
return;
}

assert(MAI->compressDebugSections() == DebugCompressionType::Z &&
"expected zlib style compression");

SmallVector<char, 128> UncompressedData;
raw_svector_ostream VecOS(UncompressedData);
Asm.writeSectionData(VecOS, &Section, Layout);
ArrayRef<uint8_t> Uncompressed =
makeArrayRef(reinterpret_cast<uint8_t *>(UncompressedData.data()),
UncompressedData.size());

SmallVector<uint8_t, 128> Compressed;
uint32_t ChType;
switch (CompressionType) {
case DebugCompressionType::None:
llvm_unreachable("has been handled");
case DebugCompressionType::Z:
ChType = ELF::ELFCOMPRESS_ZLIB;
break;
case DebugCompressionType::Zstd:
ChType = ELF::ELFCOMPRESS_ZSTD;
break;
}
compression::compress(compression::Params(CompressionType), Uncompressed,
Compressed);
const uint32_t ChType = ELF::ELFCOMPRESS_ZLIB;
compression::zlib::compress(
makeArrayRef(reinterpret_cast<uint8_t *>(UncompressedData.data()),
UncompressedData.size()),
Compressed);

if (!maybeWriteCompression(ChType, UncompressedData.size(), Compressed,
Sec.getAlignment())) {
W.OS << UncompressedData;
Expand Down
40 changes: 11 additions & 29 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,29 +438,14 @@ template <class ELFT>
Error ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
ArrayRef<uint8_t> Compressed =
Sec.OriginalData.slice(sizeof(Elf_Chdr_Impl<ELFT>));
SmallVector<uint8_t, 128> Decompressed;
DebugCompressionType Type;
switch (Sec.ChType) {
case ELFCOMPRESS_ZLIB:
Type = DebugCompressionType::Z;
break;
case ELFCOMPRESS_ZSTD:
Type = DebugCompressionType::Zstd;
break;
default:
SmallVector<uint8_t, 128> DecompressedContent;
if (Error Err = compression::zlib::uncompress(Compressed, DecompressedContent,
static_cast<size_t>(Sec.Size)))
return createStringError(errc::invalid_argument,
"--decompress-debug-sections: ch_type (" +
Twine(Sec.ChType) + ") of section '" +
Sec.Name + "' is unsupported");
}
if (Error E = compression::decompress(Type, Compressed, Decompressed,
static_cast<size_t>(Sec.Size)))
return createStringError(errc::invalid_argument,
"failed to decompress section '" + Sec.Name +
"': " + toString(std::move(E)));
"'" + Sec.Name + "': " + toString(std::move(Err)));

uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
std::copy(Decompressed.begin(), Decompressed.end(), Buf);
std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf);

return Error::success();
}
Expand Down Expand Up @@ -513,9 +498,6 @@ Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
case DebugCompressionType::Z:
Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB;
break;
case DebugCompressionType::Zstd:
Chdr.ch_type = ELF::ELFCOMPRESS_ZSTD;
break;
}
Chdr.ch_size = Sec.DecompressedSize;
Chdr.ch_addralign = Sec.DecompressedAlign;
Expand All @@ -530,9 +512,9 @@ CompressedSection::CompressedSection(const SectionBase &Sec,
DebugCompressionType CompressionType)
: SectionBase(Sec), CompressionType(CompressionType),
DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
compression::compress(compression::Params(CompressionType), OriginalData,
CompressedData);
compression::zlib::compress(OriginalData, CompressedData);

assert(CompressionType != DebugCompressionType::None);
Flags |= ELF::SHF_COMPRESSED;
size_t ChdrSize =
std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
Expand All @@ -544,9 +526,9 @@ CompressedSection::CompressedSection(const SectionBase &Sec,
}

CompressedSection::CompressedSection(ArrayRef<uint8_t> CompressedData,
uint32_t ChType, uint64_t DecompressedSize,
uint64_t DecompressedSize,
uint64_t DecompressedAlign)
: ChType(ChType), CompressionType(DebugCompressionType::None),
: CompressionType(DebugCompressionType::None),
DecompressedSize(DecompressedSize), DecompressedAlign(DecompressedAlign) {
OriginalData = CompressedData;
}
Expand Down Expand Up @@ -1724,8 +1706,8 @@ Expected<SectionBase &> ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
if (!(Shdr.sh_flags & ELF::SHF_COMPRESSED))
return Obj.addSection<Section>(*Data);
auto *Chdr = reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data->data());
return Obj.addSection<CompressedSection>(CompressedSection(
*Data, Chdr->ch_type, Chdr->ch_size, Chdr->ch_addralign));
return Obj.addSection<CompressedSection>(
CompressedSection(*Data, Chdr->ch_size, Chdr->ch_addralign));
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions llvm/lib/ObjCopy/ELF/ELFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,6 @@ class OwnedDataSection : public SectionBase {
class CompressedSection : public SectionBase {
MAKE_SEC_WRITER_FRIEND

uint32_t ChType = 0;
DebugCompressionType CompressionType;
uint64_t DecompressedSize;
uint64_t DecompressedAlign;
Expand All @@ -545,12 +544,11 @@ class CompressedSection : public SectionBase {
public:
CompressedSection(const SectionBase &Sec,
DebugCompressionType CompressionType);
CompressedSection(ArrayRef<uint8_t> CompressedData, uint32_t ChType,
uint64_t DecompressedSize, uint64_t DecompressedAlign);
CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
uint64_t DecompressedAlign);

uint64_t getDecompressedSize() const { return DecompressedSize; }
uint64_t getDecompressedAlign() const { return DecompressedAlign; }
uint64_t getChType() const { return ChType; }

Error accept(SectionVisitor &Visitor) const override;
Error accept(MutableSectionVisitor &Visitor) override;
Expand All @@ -564,9 +562,8 @@ class DecompressedSection : public SectionBase {
MAKE_SEC_WRITER_FRIEND

public:
uint32_t ChType;
explicit DecompressedSection(const CompressedSection &Sec)
: SectionBase(Sec), ChType(Sec.getChType()) {
: SectionBase(Sec) {
Size = Sec.getDecompressedSize();
Align = Sec.getDecompressedAlign();
Flags = OriginalFlags = (Flags & ~ELF::SHF_COMPRESSED);
Expand Down
44 changes: 0 additions & 44 deletions llvm/lib/Support/Compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,6 @@
using namespace llvm;
using namespace llvm::compression;

const char *compression::getReasonIfUnsupported(compression::Format F) {
switch (F) {
case compression::Format::Zlib:
if (zlib::isAvailable())
return nullptr;
return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
"build time";
case compression::Format::Zstd:
if (zstd::isAvailable())
return nullptr;
return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
"build time";
}
}

void compression::compress(Params P, ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &Output) {
switch (P.Format) {
case compression::Format::Zlib:
zlib::compress(Input, Output, P.Level);
break;
case compression::Format::Zstd:
zstd::compress(Input, Output, P.Level);
break;
}
}

Error compression::decompress(compression::Format F, ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &Output,
size_t UncompressedSize) {
switch (F) {
case compression::Format::Zlib:
return zlib::uncompress(Input, Output, UncompressedSize);
case compression::Format::Zstd:
return zstd::uncompress(Input, Output, UncompressedSize);
}
}

Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &Output,
size_t UncompressedSize) {
return decompress(formatFor(T), Input, Output, UncompressedSize);
}

#if LLVM_ENABLE_ZLIB

static StringRef convertZlibCodeToString(int Code) {
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/MC/ELF/compress-debug-sections-zlib-unavailable.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// REQUIRES: !zlib
// RUN: not llvm-mc -filetype=obj -compress-debug-sections=zlib -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s

// CHECK: llvm-mc{{[^:]*}}: error: build tools with LLVM_ENABLE_ZLIB to enable --compress-debug-sections=zlib
// CHECK-NOT: {{.}}
// CHECK: llvm-mc{{[^:]*}}: error: build tools with zlib to enable -compress-debug-sections
5 changes: 0 additions & 5 deletions llvm/test/MC/ELF/compress-debug-sections-zstd-unavailable.s

This file was deleted.

Loading

0 comments on commit 0444b40

Please sign in to comment.