Skip to content

Commit

Permalink
[llvm-objcopy] Remove support for legacy .zdebug sections
Browse files Browse the repository at this point in the history
clang 14 removed -gz=zlib-gnu support and ld.lld removed linker input support
for zlib-gnu in D126793. Now let's remove zlib-gnu from llvm-objcopy.

* .zdebug* sections are no longer recognized as debug sections. --strip* don't remove them.
  They are copied like other opaque sections
* --decompress-debug-sections does not uncompress .zdebug* sections
* --compress-debug-sections=zlib-gnu is not supported

It is very rare but in case a user has object files using .zdebug . They can use
llvm-objcopy<15 or GNU objcopy for uncompression.
--compress-debug-sections=zlib-gnu is unlikely ever used by anyone, so I do not
add a custom diagnostic.

Differential Revision: https://reviews.llvm.org/D128688
  • Loading branch information
MaskRay committed Jun 29, 2022
1 parent dd48d3a commit 45ae553
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 135 deletions.
1 change: 1 addition & 0 deletions llvm/docs/ReleaseNotes.rst
Expand Up @@ -204,6 +204,7 @@ Changes to the LLVM tools
* (Experimental) :manpage:`llvm-symbolizer(1)` now has ``--filter-markup`` to
filter :doc:`Symbolizer Markup </SymbolizerMarkupFormat>` into human-readable
form.
* :doc:`llvm-objcopy <CommandGuide/llvm-objcopy>` has removed support for the legacy ``zlib-gnu`` format.

Changes to LLDB
---------------------------------
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Expand Up @@ -53,8 +53,7 @@ using namespace llvm::object;
using SectionPred = std::function<bool(const SectionBase &Sec)>;

static bool isDebugSection(const SectionBase &Sec) {
return StringRef(Sec.Name).startswith(".debug") ||
StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index";
return StringRef(Sec.Name).startswith(".debug") || Sec.Name == ".gdb_index";
}

static bool isDWOSection(const SectionBase &Sec) {
Expand Down
48 changes: 19 additions & 29 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Expand Up @@ -519,27 +519,22 @@ Error BinarySectionWriter::visit(const CompressedSection &Sec) {
template <class ELFT>
Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
if (Sec.CompressionType == DebugCompressionType::None) {
Elf_Chdr_Impl<ELFT> Chdr;
switch (Sec.CompressionType) {
case DebugCompressionType::None:
std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
return Error::success();
}

if (Sec.CompressionType == DebugCompressionType::GNU) {
const char *Magic = "ZLIB";
memcpy(Buf, Magic, strlen(Magic));
Buf += strlen(Magic);
const uint64_t DecompressedSize =
support::endian::read64be(&Sec.DecompressedSize);
memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize));
Buf += sizeof(DecompressedSize);
} else {
Elf_Chdr_Impl<ELFT> Chdr;
case DebugCompressionType::GNU:
llvm_unreachable("unexpected zlib-gnu");
break;
case DebugCompressionType::Z:
Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB;
Chdr.ch_size = Sec.DecompressedSize;
Chdr.ch_addralign = Sec.DecompressedAlign;
memcpy(Buf, &Chdr, sizeof(Chdr));
Buf += sizeof(Chdr);
break;
}
Chdr.ch_size = Sec.DecompressedSize;
Chdr.ch_addralign = Sec.DecompressedAlign;
memcpy(Buf, &Chdr, sizeof(Chdr));
Buf += sizeof(Chdr);

std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf);
return Error::success();
Expand All @@ -553,18 +548,13 @@ CompressedSection::CompressedSection(const SectionBase &Sec,
OriginalData.size()),
CompressedData);

size_t ChdrSize;
if (CompressionType == DebugCompressionType::GNU) {
Name = ".z" + Sec.Name.substr(1);
ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t);
} else {
Flags |= ELF::SHF_COMPRESSED;
ChdrSize =
std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)),
std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>),
sizeof(object::Elf_Chdr_Impl<object::ELF32BE>)));
}
assert(CompressionType != DebugCompressionType::None);
Flags |= ELF::SHF_COMPRESSED;
size_t ChdrSize =
std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)),
std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>),
sizeof(object::Elf_Chdr_Impl<object::ELF32BE>)));
Size = ChdrSize + CompressedData.size();
Align = 8;
}
Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/ObjCopy/ELF/ELFObject.h
Expand Up @@ -554,8 +554,7 @@ class CompressedSection : public SectionBase {
Error accept(MutableSectionVisitor &Visitor) override;

static bool classof(const SectionBase *S) {
return (S->OriginalFlags & ELF::SHF_COMPRESSED) ||
(StringRef(S->Name).startswith(".zdebug"));
return S->OriginalFlags & ELF::SHF_COMPRESSED;
}
};

Expand All @@ -568,8 +567,6 @@ class DecompressedSection : public SectionBase {
Size = Sec.getDecompressedSize();
Align = Sec.getDecompressedAlign();
Flags = OriginalFlags = (Flags & ~ELF::SHF_COMPRESSED);
if (StringRef(Name).startswith(".zdebug"))
Name = "." + Name.substr(2);
}

Error accept(SectionVisitor &Visitor) const override;
Expand Down

This file was deleted.

Expand Up @@ -10,27 +10,11 @@
# RUN: llvm-readobj -S --section-groups %t-compressed.o | \
# RUN: FileCheck %s --check-prefixes=CHECK,COMPRESS

## Check zlib-gnu compression of debug sections.
# RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t.o %t-compressed-gnu.o
# RUN: llvm-readobj -S --section-groups %t-compressed-gnu.o | \
# RUN: FileCheck %s --check-prefixes=CHECK,COMPRESSZLIB

## Check decompression of debug sections.
# RUN: llvm-objcopy --decompress-debug-sections %t-compressed.o %t-decompressed.o
# RUN: llvm-readobj --section-groups %t-decompressed.o | \
# RUN: FileCheck %s --check-prefixes=CHECK,DECOMPRESS

## Check decompression of zlib-gnu debug sections.
# RUN: llvm-objcopy --decompress-debug-sections %t-compressed-gnu.o %t-decompressed-gnu.o
# RUN: llvm-readobj --section-groups %t-decompressed-gnu.o | \
# RUN: FileCheck %s --check-prefixes=CHECK,DECOMPRESS

# COMPRESSZLIB: Name: .zdebug_in_group
# COMPRESSZLIB-NEXT: Type: SHT_PROGBITS
# COMPRESSZLIB-NEXT: Flags [
# COMPRESSZLIB-NEXT: SHF_GROUP
# COMPRESSZLIB-NEXT: ]

# COMPRESS: Name: .debug_in_group
# COMPRESS-NEXT: Type: SHT_PROGBITS
# COMPRESS-NEXT: Flags [
Expand All @@ -47,7 +31,6 @@
# CHECK-NEXT: Signature: groupname
# CHECK-NEXT: Section(s) in group [
# CHECK-NEXT: .text.in.group
# COMPRESSZLIB-NEXT: .zdebug_in_group
# COMPRESS-NEXT: .debug_in_group
# DECOMPRESS-NEXT: .debug_in_group
# CHECK-NEXT: ]
@@ -1,5 +1,6 @@
# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o
# RUN: not llvm-objcopy --compress-debug-sections=zlib-fake %t.o 2>&1 | FileCheck %s
# RUN: not llvm-objcopy --compress-debug-sections=zlib-gnu %t.o 2>&1 | FileCheck %s --check-prefix=GNU

# CHECK: invalid or unsupported --compress-debug-sections format: zlib-fake

# GNU: invalid or unsupported --compress-debug-sections format: zlib-gnu
Expand Up @@ -6,16 +6,12 @@
## and it is placed into the right section.

# RUN: llvm-objcopy --compress-debug-sections %t.o %t-compressed1.o
# RUN: llvm-readobj --symbols %t-compressed1.o | FileCheck %s --check-prefixes=CHECK,ZLIB

# RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t.o %t-compressed2.o
# RUN: llvm-readobj --symbols %t-compressed2.o | FileCheck %s --check-prefixes=CHECK,ZLIBGNU
# RUN: llvm-readobj --symbols %t-compressed1.o | FileCheck %s

# CHECK: Name: .Linfo_string0
# CHECK-NEXT: Value: 0x0
# CHECK-NEXT: Size: 0
# CHECK-NEXT: Binding: Global
# CHECK-NEXT: Type: None
# CHECK-NEXT: Other: 0
# ZLIB-NEXT: Section: .debug_bar
# ZLIBGNU-NEXT: Section: .zdebug_bar
# CHECK-NEXT: Section: .debug_bar

This file was deleted.

6 changes: 0 additions & 6 deletions llvm/test/tools/llvm-objcopy/ELF/compress-debug-sections.test
Expand Up @@ -4,21 +4,15 @@
# RUN: cp %t.o %t.copy.o

# RUN: llvm-objcopy --compress-debug-sections=zlib %t.o %tz.o
# RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t.o %tzg.o
# RUN: cp %tz.o %tz.copy.o
# RUN: cp %tzg.o %tzg.copy.o

# RUN: llvm-objcopy --decompress-debug-sections %tz.o %t2.o
# RUN: llvm-objcopy --decompress-debug-sections %tzg.o %t3.o

# Using redirects to avoid llvm-objdump from printing the filename.
# RUN: llvm-objdump -s --section=.debug_str - < %t.o > %t.txt
# RUN: llvm-objdump -s --section=.debug_str - < %t2.o > %t2.txt
# RUN: llvm-objdump -s --section=.debug_str - < %t3.o > %t3.txt

# RUN: diff %t.txt %t2.txt
# RUN: diff %t.txt %t3.txt

# RUN: cmp %t.o %t.copy.o
# RUN: cmp %tz.o %tz.copy.o
# RUN: cmp %tzg.o %tzg.copy.o
4 changes: 3 additions & 1 deletion llvm/test/tools/llvm-objcopy/ELF/strip-debug.test
Expand Up @@ -98,6 +98,7 @@ Sections:
- Name: .debugfoo
Type: SHT_PROGBITS
Content: "00000000"
## .zdebug is no longer recognized as a debug section.
- Name: .zdebugfoo
Type: SHT_PROGBITS
Content: "00000000"
Expand All @@ -119,8 +120,9 @@ Symbols:
- Name: filesymbol
Type: STT_FILE

# CHECK: SectionHeaderCount: 5
# CHECK: SectionHeaderCount: 6

# CHECK: Name: .zdebugfoo
# CHECK: Name: .text
# CHECK: Name: .symtab
# CHECK: Name: .strtab
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/tools/llvm-objcopy/ELF/zdebug.yaml
@@ -0,0 +1,24 @@
## .zdebug is no longer recognized as zlib-gnu compressed sections. It's treated
## as an opaque non-debug section.
# RUN: yaml2obj %s -o %t
# RUN: llvm-objcopy --decompress-debug-sections %t %t.copy
# RUN: llvm-readelf -S %t.copy | FileCheck %s

# CHECK: .zdebug_str

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_X86_64
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Content: '00'
- Name: .zdebug_str
Type: SHT_PROGBITS
Content: 5a4c49420000000000000002789c4b64000000c40062
AddressAlign: 8
...
1 change: 0 additions & 1 deletion llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
Expand Up @@ -729,7 +729,6 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
Config.CompressionType =
StringSwitch<DebugCompressionType>(
InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq))
.Case("zlib-gnu", DebugCompressionType::GNU)
.Case("zlib", DebugCompressionType::Z)
.Default(DebugCompressionType::None);
if (Config.CompressionType == DebugCompressionType::None)
Expand Down
4 changes: 2 additions & 2 deletions llvm/tools/llvm-objcopy/ObjcopyOpts.td
Expand Up @@ -32,9 +32,9 @@ defm new_symbol_visibility : Eq<"new-symbol-visibility", "Visibility of "
def compress_debug_sections : Flag<["--"], "compress-debug-sections">;
def compress_debug_sections_eq
: Joined<["--"], "compress-debug-sections=">,
MetaVarName<"[ zlib | zlib-gnu ]">,
MetaVarName<"[ zlib ]">,
HelpText<"Compress DWARF debug sections using specified style. Supported "
"styles: 'zlib-gnu' and 'zlib'">;
"formats: 'zlib'">;
def decompress_debug_sections : Flag<["--"], "decompress-debug-sections">,
HelpText<"Decompress DWARF debug sections.">;
defm split_dwo
Expand Down

0 comments on commit 45ae553

Please sign in to comment.