[lld] r318391 - [COFF] Don't write long section names for sections that will be mapped at runtime

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 16 04:06:42 PST 2017


Author: mstorsjo
Date: Thu Nov 16 04:06:42 2017
New Revision: 318391

URL: http://llvm.org/viewvc/llvm-project?rev=318391&view=rev
Log:
[COFF] Don't write long section names for sections that will be mapped at runtime

Sections that will be mapped at runtime will only have the short
section name available, since the string table it points into isn't
mapped. Therefore prefer truncating those names over writing a
long name that is unavailable at runtime.

This allows libunwind to find the .eh_frame section at runtime even
if the module was built with debug info enabled.

Differential Revision: https://reviews.llvm.org/D40025

Modified:
    lld/trunk/COFF/Writer.cpp
    lld/trunk/test/COFF/long-section-name.test

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=318391&r1=318390&r2=318391&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Thu Nov 16 04:06:42 2017
@@ -211,7 +211,8 @@ void OutputSection::writeHeaderTo(uint8_
     // If name is too long, write offset into the string table as a name.
     sprintf(Hdr->Name, "/%d", StringTableOff);
   } else {
-    assert(!Config->Debug || Name.size() <= COFF::NameSize);
+    assert(!Config->Debug || Name.size() <= COFF::NameSize ||
+           (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
     strncpy(Hdr->Name, Name.data(),
             std::min(Name.size(), (size_t)COFF::NameSize));
   }
@@ -541,6 +542,13 @@ void Writer::createSymbolAndStringTable(
     StringRef Name = Sec->getName();
     if (Name.size() <= COFF::NameSize)
       continue;
+    // If a section isn't discardable (i.e. will be mapped at runtime),
+    // prefer a truncated section name over a long section name in
+    // the string table that is unavailable at runtime. This is different from
+    // what link.exe does, but finding ".eh_fram" instead of "/4" is useful
+    // to libunwind.
+    if ((Sec->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) == 0)
+      continue;
     Sec->setStringTableOff(addEntryToStringTable(Name));
   }
 

Modified: lld/trunk/test/COFF/long-section-name.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/long-section-name.test?rev=318391&r1=318390&r2=318391&view=diff
==============================================================================
--- lld/trunk/test/COFF/long-section-name.test (original)
+++ lld/trunk/test/COFF/long-section-name.test Thu Nov 16 04:06:42 2017
@@ -2,6 +2,7 @@
 # RUN: lld-link /debug /out:%t.exe /entry:main %t.obj
 # RUN: llvm-readobj -sections %t.exe | FileCheck %s
 
+# CHECK: Name: .eh_fram (
 # CHECK: Name: .data_long_section_name
 # CHECK: Name: .text_long_section_name
 
@@ -11,10 +12,14 @@ header:
   Characteristics: [  ]
 sections:
   - Name:            .text_long_section_name
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_DISCARDABLE ]
     Alignment:       4
     SectionData:     B82A000000C3
   - Name:            .data_long_section_name
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE, IMAGE_SCN_MEM_DISCARDABLE ]
+    Alignment:       4
+    SectionData:     "00"
+  - Name:            .eh_frame
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
     Alignment:       4
     SectionData:     "00"
@@ -43,6 +48,18 @@ symbols:
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            .eh_frame
+    Value:           0
+    SectionNumber:   3
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
     SectionDefinition:
       Length:          0
       NumberOfRelocations: 0




More information about the llvm-commits mailing list