[llvm] 76a441a - [MC] Fix compression header size check in ELF writer

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 17 01:13:44 PST 2023


Author: Fangrui Song
Date: 2023-11-17T01:13:38-08:00
New Revision: 76a441a6efa5b7e73d96a3d67512493f3873b1cb

URL: https://github.com/llvm/llvm-project/commit/76a441a6efa5b7e73d96a3d67512493f3873b1cb
DIFF: https://github.com/llvm/llvm-project/commit/76a441a6efa5b7e73d96a3d67512493f3873b1cb.diff

LOG: [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.

Added: 
    

Modified: 
    clang/test/Misc/cc1as-compress.s
    llvm/lib/MC/ELFObjectWriter.cpp
    llvm/test/MC/ELF/compress-debug-sections-zstd.s

Removed: 
    


################################################################################
diff  --git a/clang/test/Misc/cc1as-compress.s b/clang/test/Misc/cc1as-compress.s
index 54563c33bd165cb..4c81c5ca0f7f26f 100644
--- a/clang/test/Misc/cc1as-compress.s
+++ b/clang/test/Misc/cc1as-compress.s
@@ -12,4 +12,4 @@
 // ZSTD: 0000 02000000 00000000
 
 .section        .debug_str,"MS", at progbits,1
-.asciz  "perfectly compressable data sample *****************************************"
+.asciz  "perfectly compressable data sample ******************************************"

diff  --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index 8490fefe7ff5351..e4d18d8a7dd5b54 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -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.

diff  --git a/llvm/test/MC/ELF/compress-debug-sections-zstd.s b/llvm/test/MC/ELF/compress-debug-sections-zstd.s
index 01ae21a2b8abb7b..a05c5015728156a 100644
--- a/llvm/test/MC/ELF/compress-debug-sections-zstd.s
+++ b/llvm/test/MC/ELF/compress-debug-sections-zstd.s
@@ -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,"", at progbits
 .rept 50


        


More information about the llvm-commits mailing list