[PATCH] D96698: RFC: [LLD] [COFF] Include VirtualSize when sizing sections

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 15 03:23:53 PST 2021


mstorsjo created this revision.
mstorsjo added a reviewer: rnk.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

This fixes cases where the Go compiler created bss sections with a zero SizeOfRawData, with the size commnicated via the VirtualSize field.

GNU ld does pick up on and allocate section size based on VirtualSize in addition to SizeOfRawData.

See https://bugs.llvm.org/show_bug.cgi?id=49172 for the full backstory. I'm not entirely sure it's worthwhile doing (if this is the only case that happens to allocate bss sections differently, and it's getting fixed upstream there), and the fix looks overly generic like it could have a number of other unexpected side effects, but it doesn't at least trigger any change in any test case.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96698

Files:
  lld/COFF/Chunks.h
  lld/test/COFF/bss-virtualsize.yaml


Index: lld/test/COFF/bss-virtualsize.yaml
===================================================================
--- /dev/null
+++ lld/test/COFF/bss-virtualsize.yaml
@@ -0,0 +1,66 @@
+# RUN: yaml2obj %s -o %t.obj
+# RUN: lld-link -out:%t.exe -entry:entry -subsystem:console %t.obj
+# RUN: llvm-readobj -s %t.exe | FileCheck %s
+
+# CHECK:      Name: .data
+# CHECK-NEXT: VirtualSize: 0x100
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       16
+    SectionData:     C3
+  - Name:            .bss
+    Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    VirtualSize:     256
+    Alignment:       16
+    SectionData:     ''
+    SizeOfRawData:   0
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          1
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        40735498
+      Number:          1
+  - Name:            .bss
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          256
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          2
+  - Name:            '@feat.00'
+    Value:           0
+    SectionNumber:   -1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            entry
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            bssvar
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
Index: lld/COFF/Chunks.h
===================================================================
--- lld/COFF/Chunks.h
+++ lld/COFF/Chunks.h
@@ -200,7 +200,9 @@
 
   SectionChunk(ObjFile *file, const coff_section *header);
   static bool classof(const Chunk *c) { return c->kind() == SectionKind; }
-  size_t getSize() const { return header->SizeOfRawData; }
+  size_t getSize() const {
+    return std::max(header->VirtualSize, header->SizeOfRawData);
+  }
   ArrayRef<uint8_t> getContents() const;
   void writeTo(uint8_t *buf) const;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96698.323706.patch
Type: text/x-patch
Size: 2810 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210215/b7a09b0a/attachment.bin>


More information about the llvm-commits mailing list