[llvm] 1de18ad - [llvm-objcopy] Make ihex writer similar to binary writer

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 16 10:08:29 PDT 2021


Author: Fangrui Song
Date: 2021-06-16T10:08:20-07:00
New Revision: 1de18ad8d79eb328ad410fa60209e5dfa19752c8

URL: https://github.com/llvm/llvm-project/commit/1de18ad8d79eb328ad410fa60209e5dfa19752c8
DIFF: https://github.com/llvm/llvm-project/commit/1de18ad8d79eb328ad410fa60209e5dfa19752c8.diff

LOG: [llvm-objcopy] Make ihex writer similar to binary writer

There is no need to differentiate whether `UseSegments` is true or
false. Unifying the cases makes the behavior closer to BinaryWriter.

This improves compatibility with objcopy because SHF_ALLOC sections not in
a PT_LOAD will not be skipped. Such cases are usually erroneous input, though.

Reviewed By: jhenderson

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

Added: 
    

Modified: 
    llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml
    llvm/test/tools/llvm-objcopy/ELF/ihex-writer.test
    llvm/tools/llvm-objcopy/ELF/Object.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml b/llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml
index ab678a607af1..17890373e248 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml
+++ b/llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml
@@ -43,10 +43,15 @@ Sections:
     AddressAlign:    0x8
   - Name:            .dummy
     Type:            SHT_PROGBITS
-    Flags:           [ ]
+    Flags:           [ SHF_ALLOC ]
     Address:         0x20FFF8
-    Size:            65536
+    Size:            3
     AddressAlign:    0x8
+  - Name:            .nonalloc
+    Type:            SHT_PROGBITS
+    Flags:           [ ]
+    Address:         0x300000
+    Size:            1
 ProgramHeaders:
   - Type:     PT_LOAD
     Flags:    [ PF_X, PF_R ]

diff  --git a/llvm/test/tools/llvm-objcopy/ELF/ihex-writer.test b/llvm/test/tools/llvm-objcopy/ELF/ihex-writer.test
index f9c9155e4a46..09ff8ae951d4 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/ihex-writer.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/ihex-writer.test
@@ -57,8 +57,11 @@
 # SEGMENTS-NEXT:  :0B001800303132333435363738394090
 # SEGMENTS-NEXT:  :0400280040414243CE
 # SEGMENTS-NEXT:  :0B003000505152535455565758596018
+# SEGMENTS-NEXT:  :020000040020DA
+# SEGMENTS-NEXT:  :03FFF80000000006
 # SEGMENTS-NEXT:  :0400000500100000E7
 # SEGMENTS-NEXT:  :00000001FF
+# SEGMENTS-NOT:   {{.}}
 
 # 'ExtendedAddr' (04) record shouldn't be created
 # PT_NULL-NOT: :02000004

diff  --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index 7b34110b808f..5f1b4c30fb78 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -2666,32 +2666,15 @@ Error IHexWriter::checkSection(const SectionBase &Sec) {
 }
 
 Error IHexWriter::finalize() {
-  bool UseSegments = false;
-  auto ShouldWrite = [](const SectionBase &Sec) {
-    return (Sec.Flags & ELF::SHF_ALLOC) && Sec.Type != ELF::SHT_NOBITS &&
-           Sec.Size > 0;
-  };
-  auto IsInPtLoad = [](const SectionBase &Sec) {
-    return Sec.ParentSegment && Sec.ParentSegment->Type == ELF::PT_LOAD;
-  };
-
   // We can't write 64-bit addresses.
   if (addressOverflows32bit(Obj.Entry))
     return createStringError(errc::invalid_argument,
                              "Entry point address 0x%llx overflows 32 bits",
                              Obj.Entry);
 
-  // If any section we're to write has segment then we
-  // switch to using physical addresses. Otherwise we
-  // use section virtual address.
-  for (const SectionBase &Sec : Obj.sections())
-    if (ShouldWrite(Sec) && IsInPtLoad(Sec)) {
-      UseSegments = true;
-      break;
-    }
-
   for (const SectionBase &Sec : Obj.sections())
-    if (ShouldWrite(Sec) && (!UseSegments || IsInPtLoad(Sec))) {
+    if ((Sec.Flags & ELF::SHF_ALLOC) && Sec.Type != ELF::SHT_NOBITS &&
+        Sec.Size > 0) {
       if (Error E = checkSection(Sec))
         return E;
       Sections.insert(&Sec);


        


More information about the llvm-commits mailing list