Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 2e3463e

Browse files
committed
Compress debug sections only when beneficial.
Both ZLIB and the debug info compressed section header ("ZLIB" + the size of the uncompressed data) take some constant overhead so in some cases the compressed data is actually larger than the uncompressed data. In these cases, just don't compress or rename the section at all. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206659 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e153fb3 commit 2e3463e

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lib/MC/ELFObjectWriter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,12 @@ getUncompressedData(MCAsmLayout &Layout,
12091209
// Include the debug info compression header:
12101210
// "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
12111211
// useful for consumers to preallocate a buffer to decompress into.
1212-
static void
1212+
static bool
12131213
prependCompressionHeader(uint64_t Size,
12141214
SmallVectorImpl<char> &CompressedContents) {
12151215
static const StringRef Magic = "ZLIB";
1216+
if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
1217+
return false;
12161218
if (sys::IsLittleEndianHost)
12171219
Size = sys::SwapByteOrder(Size);
12181220
CompressedContents.insert(CompressedContents.begin(),
@@ -1221,6 +1223,7 @@ prependCompressionHeader(uint64_t Size,
12211223
std::copy(reinterpret_cast<char *>(&Size),
12221224
reinterpret_cast<char *>(&Size + 1),
12231225
CompressedContents.begin() + Magic.size());
1226+
return true;
12241227
}
12251228

12261229
// Return a single fragment containing the compressed contents of the whole
@@ -1243,7 +1246,8 @@ getCompressedFragment(MCAsmLayout &Layout,
12431246
if (Success != zlib::StatusOK)
12441247
return nullptr;
12451248

1246-
prependCompressionHeader(UncompressedData.size(), CompressedContents);
1249+
if (!prependCompressionHeader(UncompressedData.size(), CompressedContents))
1250+
return nullptr;
12471251

12481252
return CompressedFragment;
12491253
}

test/MC/ELF/compression.s

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: llvm-mc -filetype=obj -compress-debug-sections -triple x86_64-pc-linux-gnu < %s -o %t
22
// RUN: llvm-objdump -s %t | FileCheck %s
3-
// RUN: llvm-dwarfdump -debug-dump=abbrev %t | FileCheck --check-prefix=ABBREV %s
3+
// RUN: llvm-dwarfdump -debug-dump=info %t | FileCheck --check-prefix=INFO %s
44
// RUN: llvm-mc -filetype=obj -compress-debug-sections -triple i386-pc-linux-gnu < %s \
55
// RUN: | llvm-readobj -symbols - | FileCheck --check-prefix=386-SYMBOLS %s
66

@@ -12,17 +12,20 @@
1212
// CHECK-NOT: ZLIB
1313
// CHECK: Contents of
1414

15-
// CHECK: Contents of section .zdebug_abbrev:
16-
// CHECK-NEXT: ZLIB
15+
// Don't compress small sections, such as this simple debug_abbrev example
16+
// CHECK: Contents of section .debug_abbrev:
17+
// CHECK-NOT: ZLIB
18+
// CHECK-NOT: Contents of
19+
20+
// CHECK: Contents of section .debug_info:
1721

1822
// FIXME: Handle compressing alignment fragments to support compressing debug_frame
1923
// CHECK: Contents of section .debug_frame:
2024
// CHECK-NOT: ZLIB
2125
// CHECK: Contents of
2226

2327
// Decompress one valid dwarf section just to check that this roundtrips
24-
// ABBREV: Abbrev table for offset: 0x00000000
25-
// ABBREV: [1] DW_TAG_compile_unit DW_CHILDREN_no
28+
// INFO: 0x00000000: Compile Unit: length = 0x0000000c version = 0x0004 abbr_offset = 0x0000 addr_size = 0x08 (next unit at 0x00000010)
2629

2730
// In x86 32 bit named symbols are used for temporary symbols in merge
2831
// sections, so make sure we handle symbols inside compressed sections
@@ -41,23 +44,37 @@
4144
.byte 14 # DW_FORM_strp
4245
.byte 0 # EOM(1)
4346
.byte 0 # EOM(2)
47+
48+
.section .debug_info,"",@progbits
49+
.long 12 # Length of Unit
50+
.short 4 # DWARF version number
51+
.long .Lsection_abbrev # Offset Into Abbrev. Section
52+
.byte 8 # Address Size (in bytes)
53+
.byte 1 # Abbrev [1] DW_TAG_compile_unit
54+
.long .Linfo_string0 # DW_AT_comp_dir
55+
4456
.text
4557
foo:
4658
.cfi_startproc
4759
.file 1 "Driver.ii"
60+
# pad out the line table to make sure it's big enough to warrant compression
4861
.loc 1 2 0
4962
nop
63+
.loc 1 3 0
64+
nop
65+
.loc 1 4 0
66+
nop
67+
.loc 1 5 0
68+
nop
69+
.loc 1 6 0
70+
nop
71+
.loc 1 7 0
72+
nop
73+
.loc 1 8 0
74+
nop
5075
.cfi_endproc
5176
.cfi_sections .debug_frame
5277

5378
.section .debug_str,"MS",@progbits,1
5479
.Linfo_string0:
55-
.asciz "foo"
56-
57-
.section .debug_info,"",@progbits
58-
.long 40 # Length of Unit
59-
.short 4 # DWARF version number
60-
.long .Lsection_abbrev # Offset Into Abbrev. Section
61-
.byte 4 # Address Size (in bytes)
62-
.byte 1 # Abbrev [1] DW_TAG_compile_unit
63-
.long .Linfo_string0 # DW_AT_comp_dir
80+
.asciz "compress this "

0 commit comments

Comments
 (0)