[llvm] r349120 - [llvm-xray] Support for PIE

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 13 20:22:02 PST 2018


Hi Petr,

The test you added in this commit is failing on the Windows PS4 bot:

http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast/builds/22197/steps/test/logs/stdio:

FAIL: LLVM :: tools/llvm-xray/X86/extract-instrmap-pie.ll (43559 of 45567)
******************** TEST 'LLVM :: tools/llvm-xray/X86/extract-instrmap-pie.ll' FAILED ********************
Script:
--
: 'RUN: at line 4';   C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\llvm-xray.EXE extract C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\test\tools\llvm-xray\X86/Inputs/elf64-pie.bin -s | C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\FileCheck.EXE C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\test\tools\llvm-xray\X86\extract-instrmap-pie.ll
--
Exit Code: 1

Command Output (stdout):
--
$ ":" "RUN: at line 4"
$ "C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\llvm-xray.EXE" "extract" "C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\test\tools\llvm-xray\X86/Inputs/elf64-pie.bin" "-s"
$ "C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\FileCheck.EXE" "C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\test\tools\llvm-xray\X86\extract-instrmap-pie.ll"
# command stderr:
C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\test\tools\llvm-xray\X86\extract-instrmap-pie.ll:7:15: error: CHECK-NEXT: expected string not found in input

; CHECK-NEXT: - { id: 1, address: 0x00000000000299C0, function: 0x00000000000299C0, kind: function-enter, always-instrument: true, function-name: {{.*foo.*}} }

              ^

<stdin>:2:1: note: scanning from here

- { id: 1, address: 0x00000000000299C0, function: 0x0000000000000000, kind: function-enter, always-instrument: true, function-name: __libc_start_main }

^


error: command failed with exit status: 1

--

If it helps, when I run the llvm-xray command on my machine, here is the output it produces:

- { id: 1, address: 0x00000000000299C0, function: 0x0000000000000000, kind: function-enter, always-instrument: true, function-name: __libc_start_main }
- { id: 1, address: 0x00000000000299C0, function: 0x0000000000000000, kind: function-exit, always-instrument: true, function-name: __libc_start_main }
- { id: 1, address: 0x00000000000299E0, function: 0x0000000000000000, kind: function-enter, always-instrument: true, function-name: __libc_start_main }
- { id: 1, address: 0x00000000000299E0, function: 0x0000000000000000, kind: function-exit, always-instrument: true, function-name: __libc_start_main }
...

Can you please take a look?

Douglas Yung

-----Original Message-----
From: llvm-commits <llvm-commits-bounces at lists.llvm.org> On Behalf Of Petr Hosek via llvm-commits
Sent: Thursday, December 13, 2018 17:38
To: llvm-commits at lists.llvm.org
Subject: [llvm] r349120 - [llvm-xray] Support for PIE

Author: phosek
Date: Thu Dec 13 17:37:56 2018
New Revision: 349120

URL: http://llvm.org/viewvc/llvm-project?rev=349120&view=rev
Log:
[llvm-xray] Support for PIE

When the instrumented binary is linked as PIE, we need to apply the relative relocations to sleds. This is handled by the dynamic linker at runtime, but when processing the file we have to do it ourselves.

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

Added:
    llvm/trunk/test/tools/llvm-xray/X86/Inputs/elf64-pie.bin   (with props)
    llvm/trunk/test/tools/llvm-xray/X86/extract-instrmap-pie.ll
Modified:
    llvm/trunk/lib/XRay/InstrumentationMap.cpp

Modified: llvm/trunk/lib/XRay/InstrumentationMap.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/XRay/InstrumentationMap.cpp?rev=349120&r1=349119&r2=349120&view=diff
==============================================================================
--- llvm/trunk/lib/XRay/InstrumentationMap.cpp (original)
+++ llvm/trunk/lib/XRay/InstrumentationMap.cpp Thu Dec 13 17:37:56 2018
@@ -12,12 +12,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/XRay/InstrumentationMap.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/None.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/DataExtractor.h"
 #include "llvm/Support/Error.h"
@@ -46,6 +48,8 @@ Optional<uint64_t> InstrumentationMap::g
   return None;
 }
 
+using RelocMap = DenseMap<uint64_t, uint64_t>;
+
 static Error
 loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
           InstrumentationMap::SledContainer &Sleds, @@ -79,6 +83,31 @@ loadObj(StringRef Filename, object::Owni
     return errorCodeToError(
         std::make_error_code(std::errc::executable_format_error));
 
+  RelocMap Relocs;
+  if (ObjFile.getBinary()->isELF()) {
+    uint32_t RelrRelocationType = [](object::ObjectFile *ObjFile) {
+      if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
+        return ELFObj->getELFFile()->getRelrRelocationType();
+      else if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(ObjFile))
+        return ELFObj->getELFFile()->getRelrRelocationType();
+      else if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(ObjFile))
+        return ELFObj->getELFFile()->getRelrRelocationType();
+      else if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(ObjFile))
+        return ELFObj->getELFFile()->getRelrRelocationType();
+      else
+        return static_cast<uint32_t>(0);
+    }(ObjFile.getBinary());
+
+    for (const object::SectionRef &Section : Sections) {
+      for (const object::RelocationRef &Reloc : Section.relocations()) {
+        if (Reloc.getType() != RelrRelocationType)
+          continue;
+        if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
+          Relocs.insert({Reloc.getOffset(), *AddendOrErr});
+      }
+    }
+  }
+
   // Copy the instrumentation map data into the Sleds data structure.
   auto C = Contents.bytes_begin();
   static constexpr size_t ELF64SledEntrySize = 32; @@ -89,6 +118,16 @@ loadObj(StringRef Filename, object::Owni
               "an XRay sled entry in ELF64."),
         std::make_error_code(std::errc::executable_format_error));
 
+  auto RelocateOrElse = [&](uint32_t Offset, uint64_t Address) {
+    if (!Address) {
+      uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
+      RelocMap::const_iterator R = Relocs.find(A);
+      if (R != Relocs.end())
+        return R->second;
+    }
+    return Address;
+  };
+
   int32_t FuncId = 1;
   uint64_t CurFn = 0;
   for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) { @@ -98,8 +137,8 @@ loadObj(StringRef Filename, object::Owni
     Sleds.push_back({});
     auto &Entry = Sleds.back();
     uint32_t OffsetPtr = 0;
-    Entry.Address = Extractor.getU64(&OffsetPtr);
-    Entry.Function = Extractor.getU64(&OffsetPtr);
+    Entry.Address = RelocateOrElse(OffsetPtr, Extractor.getU64(&OffsetPtr));
+    Entry.Function = RelocateOrElse(OffsetPtr, 
+ Extractor.getU64(&OffsetPtr));
     auto Kind = Extractor.getU8(&OffsetPtr);
     static constexpr SledEntry::FunctionKinds Kinds[] = {
         SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,

Added: llvm/trunk/test/tools/llvm-xray/X86/Inputs/elf64-pie.bin
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-xray/X86/Inputs/elf64-pie.bin?rev=349120&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/llvm-xray/X86/Inputs/elf64-pie.bin
------------------------------------------------------------------------------
    svn:executable = *

Propchange: llvm/trunk/test/tools/llvm-xray/X86/Inputs/elf64-pie.bin
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: llvm/trunk/test/tools/llvm-xray/X86/extract-instrmap-pie.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-xray/X86/extract-instrmap-pie.ll?rev=349120&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-xray/X86/extract-instrmap-pie.ll (added)
+++ llvm/trunk/test/tools/llvm-xray/X86/extract-instrmap-pie.ll Thu Dec 
+++ 13 17:37:56 2018
@@ -0,0 +1,11 @@
+; This test makes sure we can extract the instrumentation map from an ; 
+XRay-instrumented PIE file.
+;
+; RUN: llvm-xray extract %S/Inputs/elf64-pie.bin -s | FileCheck %s
+
+; CHECK:      ---
+; CHECK-NEXT: - { id: 1, address: 0x00000000000299C0, function: 
+0x00000000000299C0, kind: function-enter, always-instrument: true, 
+function-name: {{.*foo.*}} } ; CHECK-NEXT: - { id: 1, address: 
+0x00000000000299D0, function: 0x00000000000299C0, kind: function-exit, 
+always-instrument: true, function-name: {{.*foo.*}} } ; CHECK-NEXT: - { id: 2, address: 0x00000000000299E0, function: 0x00000000000299E0, kind: function-enter, always-instrument: true, function-name: {{.*bar.*}} } ; CHECK-NEXT: - { id: 2, address: 0x00000000000299F6, function: 0x00000000000299E0, kind: function-exit, always-instrument: true, function-name: {{.*bar.*}} } ; CHECK-NEXT: ...


_______________________________________________
llvm-commits mailing list
llvm-commits at lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits


More information about the llvm-commits mailing list