Commit 76a441a6 authored by Fangrui Song's avatar Fangrui Song
Browse files

[MC] Fix compression header size check in ELF writer

This is #66888 with a test. For MC we only use a zstd test, as zlib has
a lot of versions/forks with different speed/size tradeoff, which would
make the test more brittle. If clang/test/Misc/cc1as-compress.s turns
out to be brittle, we could make the string longer.
parent fd2d5add
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -12,4 +12,4 @@
// ZSTD: 0000 02000000 00000000

.section        .debug_str,"MS",@progbits,1
.asciz  "perfectly compressable data sample *****************************************"
.asciz  "perfectly compressable data sample ******************************************"
+1 −1
Original line number Diff line number Diff line
@@ -843,7 +843,7 @@ bool ELFWriter::maybeWriteCompression(
    uint32_t ChType, uint64_t Size,
    SmallVectorImpl<uint8_t> &CompressedContents, Align Alignment) {
  uint64_t HdrSize =
      is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
      is64Bit() ? sizeof(ELF::Elf64_Chdr) : sizeof(ELF::Elf32_Chdr);
  if (Size <= HdrSize + CompressedContents.size())
    return false;
  // Platform specific header is followed by compressed data.
+8 −1
Original line number Diff line number Diff line
@@ -10,7 +10,8 @@
# SEC: .debug_line   PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00   C 0 0  8
# SEC: .debug_abbrev PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00     0 0  1
# SEC: .debug_info   PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00     0 0  1
# SEC: .debug_str    PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MSC 0 0  8
## .debug_str is uncompressed becuase sizeof(Elf64_Chdr)+len(compressed) >= len(uncompressed).
# SEC: .debug_str    PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MS  0 0  1
# SEC: .debug_frame  PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 00   C 0 0  8

# CHECK:      Contents of section .debug_line
@@ -23,6 +24,12 @@
# RUN: llvm-objcopy --decompress-debug-sections %t %t.decom
# RUN: cmp %t.uncom %t.decom

## .debug_str is compressed becuase sizeof(Elf32_Chdr)+len(compressed) < len(uncompressed).
# RUN: llvm-mc -filetype=obj -triple=i386 --defsym I386=1 -compress-debug-sections=zstd --defsym LONG=1 %s -o %t1
# RUN: llvm-readelf -S %t1 | FileCheck %s --check-prefix=SEC1

# SEC1: .debug_str    PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] 01 MSC 0 0  4

## Don't compress a section not named .debug_*.
	.section .nonalloc,"",@progbits
.rept 50