Skip to content

Commit dd74d31

Browse files
committed
[ELF] Refactor ELFCOMPRESS_ZLIB handling and improve diagnostics
And add some tests.
1 parent 73e6826 commit dd74d31

File tree

4 files changed

+79
-26
lines changed

4 files changed

+79
-26
lines changed

lld/ELF/InputSection.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,8 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
7272

7373
// If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no
7474
// longer supported.
75-
if (flags & SHF_COMPRESSED) {
76-
if (!compression::zlib::isAvailable())
77-
error(toString(file) + ": contains a compressed section, " +
78-
"but zlib is not available");
75+
if (flags & SHF_COMPRESSED)
7976
invokeELFT(parseCompressedHeader);
80-
}
8177
}
8278

8379
// Drop SHF_GROUP bit unless we are producing a re-linkable object file.
@@ -212,8 +208,13 @@ template <typename ELFT> void InputSectionBase::parseCompressedHeader() {
212208
}
213209

214210
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(rawData.data());
215-
if (hdr->ch_type != ELFCOMPRESS_ZLIB) {
216-
error(toString(this) + ": unsupported compression type");
211+
if (hdr->ch_type == ELFCOMPRESS_ZLIB) {
212+
if (!compression::zlib::isAvailable())
213+
error(toString(this) + " is compressed with ELFCOMPRESS_ZLIB, but lld is "
214+
"not built with zlib support");
215+
} else {
216+
error(toString(this) + ": unsupported compression type (" +
217+
Twine(hdr->ch_type) + ")");
217218
return;
218219
}
219220

lld/test/ELF/compressed-debug-input-err.s

-19
This file was deleted.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# UNSUPPORTED: zlib
2+
# RUN: yaml2obj %s -o %t.o
3+
# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
4+
5+
# CHECK: error: {{.*}}.o:(.debug_info) is compressed with ELFCOMPRESS_ZLIB, but lld is not built with zlib support
6+
7+
--- !ELF
8+
FileHeader:
9+
Class: ELFCLASS64
10+
Data: ELFDATA2LSB
11+
Type: ET_REL
12+
Machine: EM_X86_64
13+
Sections:
14+
- Type: SHT_PROGBITS
15+
Name: .debug_info
16+
Flags: [ SHF_COMPRESSED ]
17+
AddressAlign: 8
18+
Content: "010000000000000000000000000000000100000000000000789c030000000001"

lld/test/ELF/compressed-input-err.s

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# REQUIRES: zlib
2+
# RUN: yaml2obj --docnum=1 %s -o %t1.o
3+
# RUN: not ld.lld %t1.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=TOO-SHORT
4+
# TOO-SHORT: error: {{.*}}.o:(.debug_info): corrupted compressed section
5+
6+
# RUN: yaml2obj --docnum=2 %s -o %t2.o
7+
# RUN: not ld.lld %t2.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=UNKNOWN
8+
# UNKNOWN: error: {{.*}}.o:(.debug_info): unsupported compression type (3)
9+
10+
# RUN: yaml2obj --docnum=3 %s -o %t3.o
11+
# RUN: not ld.lld %t3.o -o /dev/null -shared 2>&1 | FileCheck %s
12+
13+
## Check we are able to report zlib uncompress errors.
14+
# CHECK: error: {{.*}}.o:(.debug_info): uncompress failed: zlib error: Z_DATA_ERROR
15+
16+
--- !ELF
17+
FileHeader:
18+
Class: ELFCLASS64
19+
Data: ELFDATA2LSB
20+
Type: ET_REL
21+
Machine: EM_X86_64
22+
Sections:
23+
- Type: SHT_PROGBITS
24+
Name: .debug_info
25+
Flags: [ SHF_COMPRESSED ]
26+
AddressAlign: 8
27+
Content: "0100000000000000040000000000000001000000000000"
28+
29+
--- !ELF
30+
FileHeader:
31+
Class: ELFCLASS64
32+
Data: ELFDATA2LSB
33+
Type: ET_REL
34+
Machine: EM_X86_64
35+
Sections:
36+
- Type: SHT_PROGBITS
37+
Name: .debug_info
38+
Flags: [ SHF_COMPRESSED ]
39+
AddressAlign: 8
40+
Content: "030000000000000000000000000000000100000000000000789c030000000001"
41+
42+
--- !ELF
43+
FileHeader:
44+
Class: ELFCLASS64
45+
Data: ELFDATA2LSB
46+
Type: ET_REL
47+
Machine: EM_X86_64
48+
Sections:
49+
- Type: SHT_PROGBITS
50+
Name: .debug_info
51+
Flags: [ SHF_COMPRESSED ]
52+
AddressAlign: 8
53+
Content: "010000000000000004000000000000000100000000000000ffff"

0 commit comments

Comments
 (0)