@@ -88,7 +88,22 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
88
88
if (!zlib::isAvailable ())
89
89
error (toString (file) + " : contains a compressed section, " +
90
90
" but zlib is not available" );
91
- parseCompressedHeader ();
91
+ switch (config->ekind ) {
92
+ case ELF32LEKind:
93
+ parseCompressedHeader<ELF32LE>();
94
+ break ;
95
+ case ELF32BEKind:
96
+ parseCompressedHeader<ELF32BE>();
97
+ break ;
98
+ case ELF64LEKind:
99
+ parseCompressedHeader<ELF64LE>();
100
+ break ;
101
+ case ELF64BEKind:
102
+ parseCompressedHeader<ELF64BE>();
103
+ break ;
104
+ default :
105
+ llvm_unreachable (" unknown ELFT" );
106
+ }
92
107
}
93
108
}
94
109
@@ -210,10 +225,7 @@ OutputSection *SectionBase::getOutputSection() {
210
225
// When a section is compressed, `rawData` consists with a header followed
211
226
// by zlib-compressed data. This function parses a header to initialize
212
227
// `uncompressedSize` member and remove the header from `rawData`.
213
- void InputSectionBase::parseCompressedHeader () {
214
- using Chdr64 = typename ELF64LE::Chdr;
215
- using Chdr32 = typename ELF32LE::Chdr;
216
-
228
+ template <typename ELFT> void InputSectionBase::parseCompressedHeader () {
217
229
// Old-style header
218
230
if (name.startswith (" .zdebug" )) {
219
231
if (!toStringRef (rawData).startswith (" ZLIB" )) {
@@ -239,32 +251,13 @@ void InputSectionBase::parseCompressedHeader() {
239
251
assert (flags & SHF_COMPRESSED);
240
252
flags &= ~(uint64_t )SHF_COMPRESSED;
241
253
242
- // New-style 64-bit header
243
- if (config->is64 ) {
244
- if (rawData.size () < sizeof (Chdr64)) {
245
- error (toString (this ) + " : corrupted compressed section" );
246
- return ;
247
- }
248
-
249
- auto *hdr = reinterpret_cast <const Chdr64 *>(rawData.data ());
250
- if (hdr->ch_type != ELFCOMPRESS_ZLIB) {
251
- error (toString (this ) + " : unsupported compression type" );
252
- return ;
253
- }
254
-
255
- uncompressedSize = hdr->ch_size ;
256
- alignment = std::max<uint32_t >(hdr->ch_addralign , 1 );
257
- rawData = rawData.slice (sizeof (*hdr));
258
- return ;
259
- }
260
-
261
- // New-style 32-bit header
262
- if (rawData.size () < sizeof (Chdr32)) {
254
+ // New-style header
255
+ if (rawData.size () < sizeof (typename ELFT::Chdr)) {
263
256
error (toString (this ) + " : corrupted compressed section" );
264
257
return ;
265
258
}
266
259
267
- auto *hdr = reinterpret_cast <const Chdr32 *>(rawData.data ());
260
+ auto *hdr = reinterpret_cast <const typename ELFT::Chdr *>(rawData.data ());
268
261
if (hdr->ch_type != ELFCOMPRESS_ZLIB) {
269
262
error (toString (this ) + " : unsupported compression type" );
270
263
return ;
0 commit comments