]> granicus.if.org Git - llvm/commitdiff
[llvm-objcopy] - Check dynamic relocation sections for broken references.
authorGeorge Rimar <grimar@accesssoftek.com>
Tue, 30 Apr 2019 11:02:09 +0000 (11:02 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Tue, 30 Apr 2019 11:02:09 +0000 (11:02 +0000)
This is a fix for https://bugs.llvm.org/show_bug.cgi?id=41371.

Currently, it is possible to break the sh_link field of the dynamic relocation section
by removing the section it refers to. The patch fixes an issue and adds 2 test cases.

One of them shows that it does not seem possible to break the sh_info field.
I added an assert to verify this.

Differential revision: https://reviews.llvm.org/D60825

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359552 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shinfo-reference.test [new file with mode: 0644]
test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shlink-reference.test [new file with mode: 0644]
tools/llvm-objcopy/ELF/Object.cpp
tools/llvm-objcopy/ELF/Object.h

diff --git a/test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shinfo-reference.test b/test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shinfo-reference.test
new file mode 100644 (file)
index 0000000..a66b38d
--- /dev/null
@@ -0,0 +1,30 @@
+# RUN: yaml2obj %s > %t\r
+# RUN: llvm-objcopy -R .got.plt %t %t2\r
+\r
+## .rela.plt is a dynamic relocation section that has a connection\r
+## via sh_info field with its target section .got.plt.\r
+## Here we check that if the target section is removed then dynamic\r
+## relocation section is also removed and we do not end up with a broken\r
+## sh_info value, for example.\r
+\r
+# RUN: llvm-readelf --sections %t2 \\r
+# RUN:  | FileCheck %s --implicit-check-not=".got.plt" --implicit-check-not=".rela.plt"\r
+\r
+--- !ELF\r
+FileHeader:\r
+  Class:          ELFCLASS64\r
+  Data:           ELFDATA2LSB\r
+  Type:           ET_DYN\r
+  Machine:        EM_X86_64\r
+Sections:\r
+  - Name:         .rela.plt\r
+    Type:         SHT_RELA\r
+    Flags:        [ SHF_ALLOC ]\r
+    Link:         .dynsym\r
+    Info:         .got.plt\r
+  - Name:         .got.plt\r
+    Type:         SHT_PROGBITS\r
+    Flags:        [ SHF_WRITE, SHF_ALLOC ]\r
+DynamicSymbols:\r
+  - Name:          bar\r
+    Binding:       STB_GLOBAL\r
diff --git a/test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shlink-reference.test b/test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shlink-reference.test
new file mode 100644 (file)
index 0000000..d813d30
--- /dev/null
@@ -0,0 +1,34 @@
+# RUN: yaml2obj %s > %t\r
+\r
+## Check we cannot remove the .dynsym symbol table because dynamic\r
+## relocation section .rela.dyn still references it via sh_link field.\r
+# RUN: not llvm-objcopy -R .dynsym %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR\r
+# ERR: error: Symbol table .dynsym cannot be removed because it is referenced by the relocation section .rela.dyn.\r
+\r
+## Check we can remove .dynsym after removing the reference.\r
+# RUN: llvm-objcopy -R .dynsym -R .rela.dyn %t %t2\r
+# RUN: llvm-readelf --sections %t2 | FileCheck %s --implicit-check-not=".dynsym"\r
+\r
+## Check we zero out sh_link field and allow producing output with the --allow-broken-links switch.\r
+# RUN: llvm-objcopy -R .dynsym --allow-broken-links %t %t2\r
+# RUN: llvm-readelf --sections %t2 | FileCheck %s --check-prefix=DROP-LINK\r
+# DROP-LINK:     [Nr] Name      Type Address          Off    Size   ES   Flg L\r
+# DROP-LINK:     [ 1] .rela.dyn RELA 0000000000000270 000040 000000 18   A   0\r
+# DROP-LINK-NOT: .dynsym\r
+\r
+!ELF\r
+FileHeader:\r
+  Class:           ELFCLASS64\r
+  Data:            ELFDATA2LSB\r
+  Type:            ET_DYN\r
+  Machine:         EM_X86_64\r
+Sections:\r
+  - Name:          .rela.dyn\r
+    Type:          SHT_RELA\r
+    Flags:         [ SHF_ALLOC ]\r
+    Address:       0x0000000000000270\r
+    Link:          .dynsym\r
+    EntSize:       0x0000000000000018\r
+DynamicSymbols:\r
+  - Name:          bar\r
+    Binding:       STB_GLOBAL\r
index 3830f9273d9f6f1fb67f4c6b5a8fb8894962808d..60163b6eed38bf1e0bf21d2245edd34d07fec72c 100644 (file)
@@ -680,12 +680,33 @@ void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
 }
 
 void DynamicRelocationSection::accept(MutableSectionVisitor &Visitor) {
-  Visitor.visit(*this);
-}
-
-Error Section::removeSectionReferences(bool AllowBrokenDependency,
-    function_ref<bool(const SectionBase *)> ToRemove) {
-  if (ToRemove(LinkSection)) {
+  Visitor.visit(*this);\r
+}\r
+\r
+Error DynamicRelocationSection::removeSectionReferences(\r
+    bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) {\r
+  if (ToRemove(Symbols)) {\r
+    if (!AllowBrokenLinks)\r
+      return createStringError(\r
+          llvm::errc::invalid_argument,\r
+          "Symbol table %s cannot be removed because it is "\r
+          "referenced by the relocation section %s.",\r
+          Symbols->Name.data(), this->Name.data());\r
+    Symbols = nullptr;\r
+  }\r
+\r
+  // SecToApplyRel contains a section referenced by sh_info field. It keeps\r
+  // a section to which the relocation section applies. When we remove any\r
+  // sections we also remove their relocation sections. Since we do that much\r
+  // earlier, this assert should never be triggered.\r
+  assert(!SecToApplyRel || !ToRemove(SecToApplyRel));\r
+\r
+  return Error::success();\r
+}\r
+\r
+Error Section::removeSectionReferences(bool AllowBrokenDependency,\r
+    function_ref<bool(const SectionBase *)> ToRemove) {\r
+  if (ToRemove(LinkSection)) {\r
     if (!AllowBrokenDependency)
       return createStringError(llvm::errc::invalid_argument,
                                "Section %s cannot be removed because it is "
index 16ca251b914b07a6b5f677c3389841da26c644c7..46687dd8b196f0bb2589bab7245a4d4ee4dcbd99 100644 (file)
@@ -682,12 +682,15 @@ private:
 
 public:
   explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
-
-  void accept(SectionVisitor &) const override;
-  void accept(MutableSectionVisitor &Visitor) override;
-
-  static bool classof(const SectionBase *S) {
-    if (!(S->Flags & ELF::SHF_ALLOC))
+\r
+  void accept(SectionVisitor &) const override;\r
+  void accept(MutableSectionVisitor &Visitor) override;\r
+  Error removeSectionReferences(\r
+      bool AllowBrokenLinks,\r
+      function_ref<bool(const SectionBase *)> ToRemove) override;\r
+\r
+  static bool classof(const SectionBase *S) {\r
+    if (!(S->Flags & ELF::SHF_ALLOC))\r
       return false;
     return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
   }