[llvm] 93345e8 - [llvm-objcopy] -O binary: consider SHT_NOBITS sections to be empty

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 1 15:01:34 PST 2021


Author: Patrick Oppenlander
Date: 2021-02-01T15:01:25-08:00
New Revision: 93345e825a0733f6a67898f6d6d156ff56db80e7

URL: https://github.com/llvm/llvm-project/commit/93345e825a0733f6a67898f6d6d156ff56db80e7
DIFF: https://github.com/llvm/llvm-project/commit/93345e825a0733f6a67898f6d6d156ff56db80e7.diff

LOG: [llvm-objcopy] -O binary: consider SHT_NOBITS sections to be empty

This is consistent with BFD objcopy.

Previously llvm objcopy would allocate space for SHT_NOBITS sections
often resulting in enormous binary files.

New test case (binary-paddr.test %t6).

Reviewed By: jhenderson, MaskRay

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

Added: 
    

Modified: 
    llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
    llvm/tools/llvm-objcopy/ELF/Object.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test b/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
index bee80b28b532..023414429a3c 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
@@ -183,3 +183,34 @@ Sections:
   - Name:         .data
     Type:         SHT_PROGBITS
     Flags:        [ SHF_ALLOC, SHF_WRITE ]
+
+## NOBITS sections should not appear in output.
+# RUN: yaml2obj --docnum=6 %s -o %t6
+# RUN: llvm-objcopy -O binary %t6 %t6.out
+# RUN: od -A x -t x2 %t6.out | FileCheck %s --check-prefix=SKIPNOBITS --ignore-case
+
+# SKIPNOBITS:      000000 c3c3 c3c3
+# SKIPNOBITS-NEXT: 000004
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:         .bss
+    Type:         SHT_NOBITS
+    Flags:        [ SHF_ALLOC ]
+    Address:      0x1000
+    AddressAlign: 0x1000
+    Size:         0x123
+  - Name:         gap
+    Type:         Fill
+    Size:         0xffd
+  - Name:         .text
+    Type:         SHT_PROGBITS
+    Flags:        [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:      0x4000
+    AddressAlign: 0x1000
+    Content:      "c3c3c3c3"

diff  --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index 0ff82f951b62..7faf415644fb 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -2553,7 +2553,7 @@ Error BinaryWriter::finalize() {
     if (Sec.ParentSegment != nullptr)
       Sec.Addr =
           Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr;
-    if (Sec.Size > 0)
+    if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
       MinAddr = std::min(MinAddr, Sec.Addr);
   }
 


        


More information about the llvm-commits mailing list