Index: lld/COFF/Chunks.h =================================================================== --- lld/COFF/Chunks.h +++ lld/COFF/Chunks.h @@ -38,9 +38,11 @@ class OutputSection; class Symbol; -// Mask for section types (code, data, bss, disacardable, etc.) -// and permissions (writable, readable or executable). -const uint32_t PermMask = 0xFF0000F0; +// Mask for permissions (discardable, writable, readable, executable, etc). +const uint32_t PermMask = 0xFE000000; + +// Mask for section types (code, data, bss). +const uint32_t TypeMask = 0x000000E0; // A Chunk represents a chunk of data that will occupy space in the // output (if the resolver chose that). It may or may not be backed by Index: lld/COFF/Chunks.cpp =================================================================== --- lld/COFF/Chunks.cpp +++ lld/COFF/Chunks.cpp @@ -388,7 +388,7 @@ } uint32_t SectionChunk::getOutputCharacteristics() const { - return Header->Characteristics & PermMask; + return Header->Characteristics & (PermMask | TypeMask); } bool SectionChunk::isCOMDAT() const { Index: lld/COFF/Writer.cpp =================================================================== --- lld/COFF/Writer.cpp +++ lld/COFF/Writer.cpp @@ -237,7 +237,8 @@ } void OutputSection::setPermissions(uint32_t C) { - Header.Characteristics = C & PermMask; + Header.Characteristics &= ~PermMask; + Header.Characteristics |= C; } void OutputSection::merge(OutputSection *Other) { Index: lld/test/COFF/output-chars.test =================================================================== --- lld/test/COFF/output-chars.test +++ lld/test/COFF/output-chars.test @@ -23,6 +23,7 @@ # SECTION: Name: .foo # SECTION: Characteristics [ +# SECTION-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA # SECTION-NEXT: IMAGE_SCN_MEM_EXECUTE # SECTION-NEXT: IMAGE_SCN_MEM_READ # SECTION-NEXT: IMAGE_SCN_MEM_WRITE @@ -30,6 +31,7 @@ # SECTION: Name: .foo # SECTION: Characteristics [ +# SECTION-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA # SECTION-NEXT: IMAGE_SCN_MEM_EXECUTE # SECTION-NEXT: IMAGE_SCN_MEM_READ # SECTION-NEXT: IMAGE_SCN_MEM_WRITE @@ -76,6 +78,7 @@ # MERGE-SECTION: Name: .foo # MERGE-SECTION: Characteristics [ +# MERGE-SECTION-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA # MERGE-SECTION-NEXT: IMAGE_SCN_MEM_EXECUTE # MERGE-SECTION-NEXT: IMAGE_SCN_MEM_READ # MERGE-SECTION-NEXT: IMAGE_SCN_MEM_WRITE Index: lld/test/COFF/section.test =================================================================== --- lld/test/COFF/section.test +++ lld/test/COFF/section.test @@ -16,18 +16,22 @@ # RUN: llvm-readobj -sections %t.exe | FileCheck -check-prefix=S %s # R: Characteristics [ +# R-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA # R-NEXT: IMAGE_SCN_MEM_READ # R-NEXT: ] # W: Characteristics [ +# W-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA # W-NEXT: IMAGE_SCN_MEM_WRITE # W-NEXT: ] # E: Characteristics [ +# E-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA # E-NEXT: IMAGE_SCN_MEM_EXECUTE # E-NEXT: ] # S: Characteristics [ +# S-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA # S-NEXT: IMAGE_SCN_MEM_SHARED # S-NEXT: ]