<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Jan 26, 2014 at 8:06 PM, Shankar Easwaran <span dir="ltr"><<a href="mailto:shankare@codeaurora.org" target="_blank">shankare@codeaurora.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Rui,<br>
<br>
You might want to subclass the writer for 32bit and 64bit.<br>
<br>
On ELF, we consider X86/X86_64 as two separate targets.<br></blockquote><div><br></div><div>I intentionally did not separate them because they are so similar that such abstraction wouldn't worth it. PE32+ is not actually 64 bit file format, but it's a 32 bit file format that allows one to use just 64 bit address space. One cannot create an executable larger than 2GB with PE32+, for example. Unlike ELF, most data structures in the PE32+ file remains the same as PE32.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Thanks<br>
<br>
Shankar Easwaran<div class=""><div class="h5"><br>
<br>
On 1/26/2014 9:53 PM, Rui Ueyama wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Author: ruiu<br>
Date: Sun Jan 26 21:53:30 2014<br>
New Revision: 200185<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=200185&view=rev" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project?rev=200185&view=rev</a><br>
Log:<br>
[PECOFF] Implement relocations for x86-64.<br>
<br>
Added:<br>
lld/trunk/test/pecoff/Inputs/<u></u>reloc64-1.asm<br>
lld/trunk/test/pecoff/Inputs/<u></u>reloc64-1.obj<br>
lld/trunk/test/pecoff/Inputs/<u></u>reloc64-2.asm<br>
lld/trunk/test/pecoff/Inputs/<u></u>reloc64-2.obj<br>
lld/trunk/test/pecoff/reloc64.<u></u>test<br>
Modified:<br>
lld/trunk/lib/ReaderWriter/<u></u>PECOFF/WriterPECOFF.cpp<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/<u></u>PECOFF/WriterPECOFF.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=200185&r1=200184&r2=200185&view=diff" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project/lld/trunk/lib/<u></u>ReaderWriter/PECOFF/<u></u>WriterPECOFF.cpp?rev=200185&<u></u>r1=200184&r2=200185&view=diff</a><br>
==============================<u></u>==============================<u></u>==================<br>
--- lld/trunk/lib/ReaderWriter/<u></u>PECOFF/WriterPECOFF.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/<u></u>PECOFF/WriterPECOFF.cpp Sun Jan 26 21:53:30 2014<br>
@@ -22,6 +22,7 @@<br>
#define DEBUG_TYPE "WriterPECOFF"<br>
#include <algorithm><br>
+#include <cstdlib><br>
#include <map><br>
#include <time.h><br>
#include <vector><br>
@@ -209,10 +210,16 @@ public:<br>
void appendAtom(const DefinedAtom *atom);<br>
void buildAtomRvaMap(std::map<const Atom *, uint64_t> &atomRva) const;<br>
- void applyRelocations(uint8_t *buffer,<br>
- std::map<const Atom *, uint64_t> &atomRva,<br>
- std::vector<uint64_t> §ionRva,<br>
- uint64_t imageBaseAddress);<br>
+<br>
+ void applyRelocations32(uint8_t *buffer,<br>
+ std::map<const Atom *, uint64_t> &atomRva,<br>
+ std::vector<uint64_t> §ionRva,<br>
+ uint64_t imageBaseAddress);<br>
+ void applyRelocations64(uint8_t *buffer,<br>
+ std::map<const Atom *, uint64_t> &atomRva,<br>
+ std::vector<uint64_t> §ionRva,<br>
+ uint64_t imageBaseAddress);<br>
+<br>
void printAtomAddresses(uint64_t baseAddr) const;<br>
void addBaseRelocations(std::<u></u>vector<uint64_t> &relocSites) const;<br>
@@ -460,10 +467,10 @@ AtomChunk::buildAtomRvaMap(<u></u>std::map<cons<br>
atomRva[layout->_atom] = layout->_virtualAddr;<br>
}<br>
-void AtomChunk::applyRelocations(<u></u>uint8_t *buffer,<br>
- std::map<const Atom *, uint64_t> &atomRva,<br>
- std::vector<uint64_t> §ionRva,<br>
- uint64_t imageBaseAddress) {<br>
+void AtomChunk::applyRelocations32(<u></u>uint8_t *buffer,<br>
+ std::map<const Atom *, uint64_t> &atomRva,<br>
+ std::vector<uint64_t> §ionRva,<br>
+ uint64_t imageBaseAddress) {<br>
buffer += _fileOffset;<br>
for (const auto *layout : _atomLayouts) {<br>
const DefinedAtom *atom = cast<DefinedAtom>(layout->_<u></u>atom);<br>
@@ -527,6 +534,49 @@ void AtomChunk::applyRelocations(<u></u>uint8_t<br>
}<br>
}<br>
+void AtomChunk::applyRelocations64(<u></u>uint8_t *buffer,<br>
+ std::map<const Atom *, uint64_t> &atomRva,<br>
+ std::vector<uint64_t> §ionRva,<br>
+ uint64_t imageBase) {<br>
+ buffer += _fileOffset;<br>
+ for (const auto *layout : _atomLayouts) {<br>
+ const DefinedAtom *atom = cast<DefinedAtom>(layout->_<u></u>atom);<br>
+ for (const Reference *ref : *atom) {<br>
+ if (ref->kindNamespace() != Reference::KindNamespace::<u></u>COFF)<br>
+ continue;<br>
+<br>
+ auto relocSite32 = reinterpret_cast<ulittle32_t *>(<br>
+ buffer + layout->_fileOffset + ref->offsetInAtom());<br>
+ uint64_t targetAddr = atomRva[ref->target()];<br>
+<br>
+ switch (ref->kindValue()) {<br>
+ case llvm::COFF::IMAGE_REL_AMD64_<u></u>ADDR32NB:<br>
+ *relocSite32 = targetAddr - imageBase;<br>
+ break;<br>
+ case llvm::COFF::IMAGE_REL_AMD64_<u></u>REL32:<br>
+ *relocSite32 = targetAddr - atomRva[atom] + ref->offsetInAtom() + 4;<br>
+ break;<br>
+<br>
+#define REL32(x) \<br>
+ case llvm::COFF::IMAGE_REL_AMD64_<u></u>REL32_ ## x: { \<br>
+ uint32_t off = targetAddr - atomRva[atom] + ref->offsetInAtom() + 4; \<br>
+ *relocSite32 = off + x; \<br>
+ }<br>
+ REL32(1);<br>
+ REL32(2);<br>
+ REL32(3);<br>
+ REL32(4);<br>
+ REL32(5);<br>
+#undef CASE<br>
+<br>
+ default:<br>
+ llvm::errs() << "Kind: " << (int)ref->kindValue() << "\n";<br>
+ llvm_unreachable("Unsupported relocation kind");<br>
+ }<br>
+ }<br>
+ }<br>
+}<br>
+<br>
/// Print atom VAs. Used only for debugging.<br>
void AtomChunk::printAtomAddresses(<u></u>uint64_t baseAddr) const {<br>
for (const auto *layout : _atomLayouts) {<br>
@@ -965,10 +1015,16 @@ void PECOFFWriter::<u></u>applyAllRelocations(u<br>
chunk->buildAtomRvaMap(<u></u>atomRva);<br>
// Pass 2<br>
- for (auto &cp : _chunks)<br>
- if (AtomChunk *chunk = dyn_cast<AtomChunk>(&*cp))<br>
- chunk->applyRelocations(<u></u>bufferStart, atomRva, sectionRva,<br>
- _ctx.getBaseAddress());<br>
+ uint64_t base = _ctx.getBaseAddress();<br>
+ for (auto &cp : _chunks) {<br>
+ if (AtomChunk *chunk = dyn_cast<AtomChunk>(&*cp)) {<br>
+ if (_ctx.is64Bit()) {<br>
+ chunk->applyRelocations64(<u></u>bufferStart, atomRva, sectionRva, base);<br>
+ } else {<br>
+ chunk->applyRelocations32(<u></u>bufferStart, atomRva, sectionRva, base);<br>
+ }<br>
+ }<br>
+ }<br>
}<br>
/// Print atom VAs. Used only for debugging.<br>
<br>
Added: lld/trunk/test/pecoff/Inputs/<u></u>reloc64-1.asm<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/reloc64-1.asm?rev=200185&view=auto" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project/lld/trunk/test/pecoff/<u></u>Inputs/reloc64-1.asm?rev=<u></u>200185&view=auto</a><br>
==============================<u></u>==============================<u></u>==================<br>
--- lld/trunk/test/pecoff/Inputs/<u></u>reloc64-1.asm (added)<br>
+++ lld/trunk/test/pecoff/Inputs/<u></u>reloc64-1.asm Sun Jan 26 21:53:30 2014<br>
@@ -0,0 +1,8 @@<br>
+extern fn2 : PROC<br>
+<br>
+.code<br>
+fn1 PROC<br>
+ call fn2<br>
+ ret<br>
+fn1 ENDP<br>
+End<br>
<br>
Added: lld/trunk/test/pecoff/Inputs/<u></u>reloc64-1.obj<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/reloc64-1.obj?rev=200185&view=auto" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project/lld/trunk/test/pecoff/<u></u>Inputs/reloc64-1.obj?rev=<u></u>200185&view=auto</a><br>
==============================<u></u>==============================<u></u>==================<br>
Binary files lld/trunk/test/pecoff/Inputs/<u></u>reloc64-1.obj (added) and lld/trunk/test/pecoff/Inputs/<u></u>reloc64-1.obj Sun Jan 26 21:53:30 2014 differ<br>
<br>
Added: lld/trunk/test/pecoff/Inputs/<u></u>reloc64-2.asm<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/reloc64-2.asm?rev=200185&view=auto" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project/lld/trunk/test/pecoff/<u></u>Inputs/reloc64-2.asm?rev=<u></u>200185&view=auto</a><br>
==============================<u></u>==============================<u></u>==================<br>
--- lld/trunk/test/pecoff/Inputs/<u></u>reloc64-2.asm (added)<br>
+++ lld/trunk/test/pecoff/Inputs/<u></u>reloc64-2.asm Sun Jan 26 21:53:30 2014<br>
@@ -0,0 +1,5 @@<br>
+.code<br>
+fn2 PROC<br>
+ ret<br>
+fn2 ENDP<br>
+End<br>
<br>
Added: lld/trunk/test/pecoff/Inputs/<u></u>reloc64-2.obj<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/reloc64-2.obj?rev=200185&view=auto" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project/lld/trunk/test/pecoff/<u></u>Inputs/reloc64-2.obj?rev=<u></u>200185&view=auto</a><br>
==============================<u></u>==============================<u></u>==================<br>
Binary files lld/trunk/test/pecoff/Inputs/<u></u>reloc64-2.obj (added) and lld/trunk/test/pecoff/Inputs/<u></u>reloc64-2.obj Sun Jan 26 21:53:30 2014 differ<br>
<br>
Added: lld/trunk/test/pecoff/reloc64.<u></u>test<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/reloc64.test?rev=200185&view=auto" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project/lld/trunk/test/pecoff/<u></u>reloc64.test?rev=200185&view=<u></u>auto</a><br>
==============================<u></u>==============================<u></u>==================<br>
--- lld/trunk/test/pecoff/reloc64.<u></u>test (added)<br>
+++ lld/trunk/test/pecoff/reloc64.<u></u>test Sun Jan 26 21:53:30 2014<br>
@@ -0,0 +1,7 @@<br>
+# RUN: lld -flavor link /out:%t.exe /subsystem:console /machine:x64 \<br>
+# RUN: /entry:fn1 -- %p/Inputs/reloc64-1.obj %p/Inputs/reloc64-2.obj<br>
+# RUN: llvm-objdump -d %t.exe | FileCheck %s<br>
+<br>
+CHECK: Disassembly of section .text:<br>
+CHECK-NEXT: .text:<br>
+CHECK-NEXT: 2000: e8 15 00 00 00 callq 21<br>
<br>
<br>
______________________________<u></u>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">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/<u></u>mailman/listinfo/llvm-commits</a><br>
<br>
<br>
</blockquote>
<br>
<br></div></div><span class=""><font color="#888888">
-- <br>
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by the Linux Foundation<br>
<br>
</font></span></blockquote></div><br></div></div>