[lld] 860e862 - [lld/mac] Simplify encodeDylibOrdinal() a bit

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 28 06:17:01 PST 2021


Author: Nico Weber
Date: 2021-02-28T09:16:45-05:00
New Revision: 860e862f34ce17f6a5ddeee472fe4e305370da18

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

LOG: [lld/mac] Simplify encodeDylibOrdinal() a bit

Only one of the two callers used the lastBinding parameter, so
do that work at that one call site. Extract a ordinalForDylibSymbol()
helper to make this tidy.

No behavior change.

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

Added: 
    

Modified: 
    lld/MachO/SyntheticSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 7fd7a4918c1a..6e219862bb04 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -279,28 +279,21 @@ static void encodeBinding(const Symbol *sym, const OutputSection *osec,
 }
 
 // Non-weak bindings need to have their dylib ordinal encoded as well.
-static void encodeDylibOrdinal(const DylibSymbol *dysym, Binding *lastBinding,
-                               raw_svector_ostream &os) {
-  using namespace llvm::MachO;
+static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) {
+  return dysym.isDynamicLookup() ? MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP
+                                 : dysym.getFile()->ordinal;
+}
 
-  int16_t ordinal = dysym->isDynamicLookup() ? BIND_SPECIAL_DYLIB_FLAT_LOOKUP
-                                             : dysym->getFile()->ordinal;
-
-  if (lastBinding == nullptr ||
-      lastBinding->ordinal != ordinal) {
-    if (ordinal <= 0) {
-      os << static_cast<uint8_t>(
-          BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
-          (ordinal & BIND_IMMEDIATE_MASK));
-    } else if (ordinal <= BIND_IMMEDIATE_MASK) {
-      os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
-                                 ordinal);
-    } else {
-      os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
-      encodeULEB128(ordinal, os);
-    }
-    if (lastBinding != nullptr)
-      lastBinding->ordinal = ordinal;
+static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) {
+  using namespace llvm::MachO;
+  if (ordinal <= 0) {
+    os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
+                               (ordinal & BIND_IMMEDIATE_MASK));
+  } else if (ordinal <= BIND_IMMEDIATE_MASK) {
+    os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal);
+  } else {
+    os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
+    encodeULEB128(ordinal, os);
   }
 }
 
@@ -336,7 +329,11 @@ void BindingSection::finalizeContents() {
     return a.target.getVA() < b.target.getVA();
   });
   for (const BindingEntry &b : bindings) {
-    encodeDylibOrdinal(b.dysym, &lastBinding, os);
+    int16_t ordinal = ordinalForDylibSymbol(*b.dysym);
+    if (ordinal != lastBinding.ordinal) {
+      encodeDylibOrdinal(ordinal, os);
+      lastBinding.ordinal = ordinal;
+    }
     if (auto *isec = b.target.section.dyn_cast<const InputSection *>()) {
       encodeBinding(b.dysym, isec->parent, isec->outSecOff + b.target.offset,
                     b.addend, /*isWeakBinding=*/false, lastBinding, os);
@@ -558,7 +555,7 @@ uint32_t LazyBindingSection::encode(const DylibSymbol &sym) {
   uint64_t offset = in.lazyPointers->addr - dataSeg->firstSection()->addr +
                     sym.stubsIndex * WordSize;
   encodeULEB128(offset, os);
-  encodeDylibOrdinal(&sym, nullptr, os);
+  encodeDylibOrdinal(ordinalForDylibSymbol(sym), os);
 
   uint8_t flags = MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
   if (sym.isWeakRef())


        


More information about the llvm-commits mailing list