Fixed llvm-objcopy to add correct symbol table for ELF with program headers.
authorAlex Orlov <aorlov@accesssoftek.com>
Wed, 12 May 2021 08:39:30 +0000 (12:39 +0400)
committerAlex Orlov <aorlov@accesssoftek.com>
Wed, 12 May 2021 08:39:30 +0000 (12:39 +0400)
This fixes the following bugs:
https://bugs.llvm.org/show_bug.cgi?id=43935

Reviewed By: jhenderson

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

llvm/test/tools/llvm-objcopy/ELF/add-symbol-no-symtab.test
llvm/tools/llvm-objcopy/ELF/Object.cpp

index d428d07..292fe4d 100644 (file)
@@ -105,3 +105,26 @@ DynamicSymbols:
     Binding: STB_GLOBAL
 Symbols: []
 ...
+
+## Check the created .symtab is correct when there are program headers.
+# RUN: yaml2obj --docnum=4 %s -o %t4
+# RUN: llvm-objcopy %t4 %t4.add --add-symbol foo=1234
+# RUN: llvm-readelf -s %t4.add | FileCheck --check-prefix=SEC4 %s
+
+# SEC4:      0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND 
+# SEC4-NEXT: 1: 00000000000004d2     0 NOTYPE  GLOBAL DEFAULT   ABS foo
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_X86_64
+Sections:
+  - Name:    .foo
+    Type:    SHT_PROGBITS
+ProgramHeaders:
+  - Type:     PT_LOAD
+    FirstSec: .foo
+    LastSec:  .foo
+...
index 0ac8b3a..6764b30 100644 (file)
@@ -1205,6 +1205,10 @@ static bool sectionWithinSegment(const SectionBase &Sec, const Segment &Seg) {
   // not the first.
   uint64_t SecSize = Sec.Size ? Sec.Size : 1;
 
+  // Ignore just added sections.
+  if (Sec.OriginalOffset == std::numeric_limits<uint64_t>::max())
+    return false;
+
   if (Sec.Type == SHT_NOBITS) {
     if (!(Sec.Flags & SHF_ALLOC))
       return false;