[lld] df92377 - [lld-macho] Have Symbol::getVA() return a non-relative virtual address

Shoaib Meenai via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 29 15:52:36 PDT 2020


Author: Jez Ng
Date: 2020-04-29T15:44:50-07:00
New Revision: df92377823d153ee3049060560771a2199712a9c

URL: https://github.com/llvm/llvm-project/commit/df92377823d153ee3049060560771a2199712a9c
DIFF: https://github.com/llvm/llvm-project/commit/df92377823d153ee3049060560771a2199712a9c.diff

LOG: [lld-macho] Have Symbol::getVA() return a non-relative virtual address

Currently, getVA() returns a virtual address with the assumption that
the ImageBase is zero. As I understand, this is what lld-ELF is doing.
However, under our current design, it seems like an awkward setup --
I'm finding that I have to add and subtract ImageBase in several places
to make things work out.

As such, I think it's simpler to have getVA() return a non-relative VA,
but I'm not sure if I'm missing something. Would love to hear more from
folks familiar with lld-ELF.

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

Added: 
    

Modified: 
    lld/MachO/ExportTrie.cpp
    lld/MachO/InputSection.cpp
    lld/MachO/Symbols.h
    lld/MachO/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/ExportTrie.cpp b/lld/MachO/ExportTrie.cpp
index cb3c52e28381..2f352bc94278 100644
--- a/lld/MachO/ExportTrie.cpp
+++ b/lld/MachO/ExportTrie.cpp
@@ -196,7 +196,7 @@ void TrieBuilder::sortAndBuild(MutableArrayRef<const Symbol *> vec,
 
   if (isTerminal) {
     assert(j - i == 1); // no duplicate symbols
-    node->info = {pivotSymbol->getVA() + ImageBase};
+    node->info = {pivotSymbol->getVA()};
   } else {
     // This is the tail-call-optimized version of the following:
     // sortAndBuild(vec.slice(i, j - i), node, lastPos, pos + 1);

diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 8c4a50b6820e..146c6e7aba1c 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -33,7 +33,7 @@ void InputSection::writeTo(uint8_t *buf) {
     uint64_t va = 0;
     if (auto *s = r.target.dyn_cast<Symbol *>()) {
       if (auto *dylibSymbol = dyn_cast<DylibSymbol>(s)) {
-        va = in.got->addr - ImageBase + dylibSymbol->gotIndex * WordSize;
+        va = in.got->addr + dylibSymbol->gotIndex * WordSize;
       } else {
         va = s->getVA();
       }
@@ -45,7 +45,7 @@ void InputSection::writeTo(uint8_t *buf) {
 
     uint64_t val = va + r.addend;
     if (1) // TODO: handle non-pcrel relocations
-      val -= addr - ImageBase + r.offset;
+      val -= addr + r.offset;
     target->relocateOne(buf + r.offset, r.type, val);
   }
 }

diff  --git a/lld/MachO/Symbols.h b/lld/MachO/Symbols.h
index 65c54feb5372..c9d9527a7b0a 100644
--- a/lld/MachO/Symbols.h
+++ b/lld/MachO/Symbols.h
@@ -81,7 +81,7 @@ class DylibSymbol : public Symbol {
 
 inline uint64_t Symbol::getVA() const {
   if (auto *d = dyn_cast<Defined>(this))
-    return d->isec->addr + d->value - ImageBase;
+    return d->isec->addr + d->value;
   return 0;
 }
 

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index de6c2c8d22c3..5499529a3107 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -162,7 +162,7 @@ class LCMain : public LoadCommand {
     auto *c = reinterpret_cast<entry_point_command *>(buf);
     c->cmd = LC_MAIN;
     c->cmdsize = getSize();
-    c->entryoff = config->entry->getVA();
+    c->entryoff = config->entry->getVA() - ImageBase;
     c->stacksize = 0;
   }
 };


        


More information about the llvm-commits mailing list