[PATCH] D102258: Fixed llvm-objcopy to add correct symbol table for ELF with program headers.

Alex Orlov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 11 10:36:40 PDT 2021


aorlov created this revision.
aorlov added reviewers: jhenderson, dblaikie.
aorlov added a project: LLVM.
Herald added subscribers: abrachet, emaste.
Herald added a reviewer: alexshap.
Herald added a reviewer: rupprecht.
aorlov requested review of this revision.
Herald added subscribers: llvm-commits, MaskRay.

This fixes the following bugs:
https://bugs.llvm.org/show_bug.cgi?id=43935


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102258

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


Index: llvm/tools/llvm-objcopy/ELF/Object.cpp
===================================================================
--- llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -1205,6 +1205,10 @@
   // 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;
Index: llvm/test/tools/llvm-objcopy/ELF/add-symbol-no-symtab.test
===================================================================
--- llvm/test/tools/llvm-objcopy/ELF/add-symbol-no-symtab.test
+++ llvm/test/tools/llvm-objcopy/ELF/add-symbol-no-symtab.test
@@ -105,3 +105,26 @@
     Binding: STB_GLOBAL
 Symbols: []
 ...
+
+## Check the created .symtab is correct in case of existing ProgramHeaders.
+# 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
+...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102258.344469.patch
Type: text/x-patch
Size: 1467 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210511/127e15c2/attachment.bin>


More information about the llvm-commits mailing list