<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Feb 10, 2014 at 9:34 PM, Simon Atanasyan <span dir="ltr"><<a href="mailto:simon@atanasyan.com" target="_blank">simon@atanasyan.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: atanasyan<br>
Date: Mon Feb 10 23:34:02 2014<br>
New Revision: 201129<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=201129&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=201129&view=rev</a><br>
Log:<br>
[Mips] Handle R_MIPS_COPY relocation.<br>
<br>
Added:<br>
    lld/trunk/test/elf/Mips/rcopy.test<br>
Modified:<br>
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp<br>
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h<br>
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp<br>
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp?rev=201129&r1=201128&r2=201129&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp?rev=201129&r1=201128&r2=201129&view=diff</a><br>

==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.cpp Mon Feb 10 23:34:02 2014<br>
@@ -46,6 +46,19 @@ void MipsLinkingContext::addPasses(PassM<br>
   ELFLinkingContext::addPasses(pm);<br>
 }<br>
<br>
+bool MipsLinkingContext::isDynamicRelocation(const DefinedAtom &,<br>
+                                             const Reference &r) const {<br>
+  if (r.kindNamespace() != Reference::KindNamespace::ELF)<br>
+    return false;<br>
+<br>
+  switch (r.kindValue()) {<br>
+  case llvm::ELF::R_MIPS_COPY:<br>
+    return true;<br>
+  default:<br>
+    return false;<br>
+  }<br></blockquote><div><br></div><div>This switch looks like it could be more easily written as:<br><br>  return r.kindValue() == llvm::ELF::R_MIPS_COPY;<br><br>for what it's worth.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+}<br>
+<br>
 bool MipsLinkingContext::isPLTRelocation(const DefinedAtom &,<br>
                                          const Reference &r) const {<br>
   if (r.kindNamespace() != Reference::KindNamespace::ELF)<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h?rev=201129&r1=201128&r2=201129&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h?rev=201129&r1=201128&r2=201129&view=diff</a><br>

==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h (original)<br>
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsLinkingContext.h Mon Feb 10 23:34:02 2014<br>
@@ -46,6 +46,8 @@ public:<br>
   virtual StringRef getDefaultInterpreter() const;<br>
   virtual void addPasses(PassManager &pm);<br>
   virtual bool isRelaOutputFormat() const { return false; }<br>
+  virtual bool isDynamicRelocation(const DefinedAtom &,<br>
+                                   const Reference &r) const;<br>
   virtual bool isPLTRelocation(const DefinedAtom &, const Reference &r) const;<br>
 };<br>
<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp?rev=201129&r1=201128&r2=201129&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp?rev=201129&r1=201128&r2=201129&view=diff</a><br>

==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp Mon Feb 10 23:34:02 2014<br>
@@ -155,6 +155,11 @@ public:<br>
       gotplt->setOrdinal(ordinal++);<br>
       mf->addAtom(*gotplt);<br>
     }<br>
+<br>
+    for (auto obj : _objectVector) {<br>
+      obj->setOrdinal(ordinal++);<br>
+      mf->addAtom(*obj);<br>
+    }<br>
   }<br>
<br>
 private:<br>
@@ -176,18 +181,30 @@ private:<br>
   /// \brief Map Atoms to their PLT entries.<br>
   llvm::DenseMap<const Atom *, PLTAtom *> _pltMap;<br>
<br>
+  /// \brief Map Atoms to their Object entries.<br>
+  llvm::DenseMap<const Atom *, ObjectAtom *> _objectMap;<br>
+<br>
   /// \brief the list of PLT atoms.<br>
   std::vector<PLTAtom *> _pltVector;<br>
<br>
   /// \brief the list of GOTPLT atoms.<br>
   std::vector<GOTAtom *> _gotpltVector;<br>
<br>
+  /// \brief the list of Object entries.<br>
+  std::vector<ObjectAtom *> _objectVector;<br>
+<br>
   /// \brief Handle a specific reference.<br>
   void handleReference(const DefinedAtom &atom, const Reference &ref) {<br>
     if (ref.kindNamespace() != lld::Reference::KindNamespace::ELF)<br>
       return;<br>
     assert(ref.kindArch() == Reference::KindArch::Mips);<br>
     switch (ref.kindValue()) {<br>
+    case R_MIPS_32:<br>
+    case R_MIPS_HI16:<br>
+    case R_MIPS_LO16:<br>
+      // FIXME (simon): Handle dynamic/static linking differently.<br>
+      handlePlain(ref);<br>
+      break;<br>
     case R_MIPS_26:<br>
       handlePLT(ref);<br>
       break;<br>
@@ -204,6 +221,14 @@ private:<br>
     return false;<br>
   }<br>
<br>
+  void handlePlain(const Reference &ref) {<br>
+    if (!ref.target())<br>
+      return;<br>
+    auto sla = dyn_cast<SharedLibraryAtom>(ref.target());<br>
+    if (sla && sla->type() == SharedLibraryAtom::Type::Data)<br>
+      const_cast<Reference &>(ref).setTarget(getObjectEntry(sla));<br>
+  }<br>
+<br>
   void handlePLT(const Reference &ref) {<br>
     if (ref.kindValue() == R_MIPS_26 && !isLocal(ref.target()))<br>
       const_cast<Reference &>(ref).setKindValue(LLD_R_MIPS_GLOBAL_26);<br>
@@ -332,6 +357,22 @@ private:<br>
<br>
     return pa;<br>
   }<br>
+<br>
+  const ObjectAtom *getObjectEntry(const SharedLibraryAtom *a) {<br>
+    auto obj = _objectMap.find(a);<br>
+    if (obj != _objectMap.end())<br>
+      return obj->second;<br>
+<br>
+    auto oa = new (_file._alloc) ObjectAtom(_file);<br>
+    oa->addReferenceELF_Mips(R_MIPS_COPY, 0, oa, 0);<br>
+    oa->_name = a->name();<br>
+    oa->_size = a->size();<br>
+<br>
+    _objectMap[a] = oa;<br>
+    _objectVector.push_back(oa);<br>
+<br>
+    return oa;<br>
+  }<br>
 };<br>
<br>
 } // end anon namespace<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp?rev=201129&r1=201128&r2=201129&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp?rev=201129&r1=201128&r2=201129&view=diff</a><br>

==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp Mon Feb 10 23:34:02 2014<br>
@@ -56,6 +56,7 @@ const Registry::KindStrings MipsTargetHa<br>
   LLD_KIND_STRING_ENTRY(R_MIPS_GOT16),<br>
   LLD_KIND_STRING_ENTRY(R_MIPS_CALL16),<br>
   LLD_KIND_STRING_ENTRY(R_MIPS_JALR),<br>
+  LLD_KIND_STRING_ENTRY(R_MIPS_COPY),<br>
   LLD_KIND_STRING_ENTRY(R_MIPS_JUMP_SLOT),<br>
   LLD_KIND_STRING_ENTRY(LLD_R_MIPS_GLOBAL_GOT),<br>
   LLD_KIND_STRING_ENTRY(LLD_R_MIPS_GLOBAL_GOT16),<br>
<br>
Added: lld/trunk/test/elf/Mips/rcopy.test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/Mips/rcopy.test?rev=201129&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/Mips/rcopy.test?rev=201129&view=auto</a><br>

==============================================================================<br>
--- lld/trunk/test/elf/Mips/rcopy.test (added)<br>
+++ lld/trunk/test/elf/Mips/rcopy.test Mon Feb 10 23:34:02 2014<br>
@@ -0,0 +1,42 @@<br>
+# Check handling of R_MIPS_COPY relocation<br>
+<br>
+# Build shared library<br>
+# RUN: llvm-mc -triple=mipsel -filetype=obj -relocation-model=pic \<br>
+# RUN:         -o=%t-obj %p/Inputs/ext.s<br>
+# RUN: lld -flavor gnu -target mipsel -shared -o %t-so %t-obj<br>
+<br>
+# Build executable<br>
+# RUN: llvm-mc -triple=mipsel -filetype=obj -o=%t-obj %s<br>
+# RUN: lld -flavor gnu -target mipsel -e glob -o %t %t-obj %t-so<br>
+# RUN: llvm-readobj -relocations -symbols -dyn-symbols %t | FileCheck %s<br>
+<br>
+# CHECK:       Relocations [<br>
+# CHECK-NEXT:    Section ({{[0-9]+}}) .rel.dyn {<br>
+# CHECK-NEXT:      0x{{[1-9A-F][0-9A-F]*}} R_MIPS_COPY data1 0x0<br>
+# CHECK-NEXT:    }<br>
+# CHECK-NEXT:  ]<br>
+<br>
+# CHECK:         Name: data1 ({{[0-9]+}}<br>
+# CHECK-NEXT:    Value: 0x{{[1-9A-F][0-9A-F]*}}<br>
+# CHECK-NEXT:    Size: 4<br>
+# CHECK-NEXT:    Binding: Global (0x1)<br>
+# CHECK-NEXT:    Type: Object (0x1)<br>
+# CHECK-NEXT:    Other: 0<br>
+# CHECK-NEXT:    Section: .bss (0x{{[1-9A-F][0-9A-F]*}})<br>
+<br>
+# CHECK:         Name: data1@ ({{[0-9]+}}<br>
+# CHECK-NEXT:    Value: 0x{{[1-9A-F][0-9A-F]*}}<br>
+# CHECK-NEXT:    Size: 4<br>
+# CHECK-NEXT:    Binding: Global (0x1)<br>
+# CHECK-NEXT:    Type: Object (0x1)<br>
+# CHECK-NEXT:    Other: 0<br>
+# CHECK-NEXT:    Section: .bss (0x{{[1-9A-F][0-9A-F]*}})<br>
+<br>
+    .abicalls<br>
+    .option pic0<br>
+    .global glob<br>
+    .ent    glob<br>
+glob:<br>
+    lui     $2,%hi(data1)<br>
+    lw      $2,%lo(data1)($2)<br>
+    .end    glob<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>