Esempio n. 1
0
static char getSymbolNMTypeChar(const GlobalValue &GV) {
  if (GV.getValueType()->isFunctionTy())
    return 't';
  // FIXME: should we print 'b'? At the IR level we cannot be sure if this
  // will be in bss or not, but we could approximate.
  return 'd';
}
Esempio n. 2
0
bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
                                        const GlobalValue &Dest,
                                        const GlobalValue &Src) {

  // Should we unconditionally use the Src?
  if (shouldOverrideFromSrc()) {
    LinkFromSrc = true;
    return false;
  }

  // We always have to add Src if it has appending linkage.
  if (Src.hasAppendingLinkage()) {
    // Should have prevented importing for appending linkage in linkIfNeeded.
    assert(!isPerformingImport());
    LinkFromSrc = true;
    return false;
  }

  if (isPerformingImport()) {
    // LinkFromSrc iff this is a global requested for importing.
    LinkFromSrc = GlobalsToImport->count(&Src);
    return false;
  }

  bool SrcIsDeclaration = Src.isDeclarationForLinker();
  bool DestIsDeclaration = Dest.isDeclarationForLinker();

  if (SrcIsDeclaration) {
    // If Src is external or if both Src & Dest are external..  Just link the
    // external globals, we aren't adding anything.
    if (Src.hasDLLImportStorageClass()) {
      // If one of GVs is marked as DLLImport, result should be dllimport'ed.
      LinkFromSrc = DestIsDeclaration;
      return false;
    }
    // If the Dest is weak, use the source linkage.
    if (Dest.hasExternalWeakLinkage()) {
      LinkFromSrc = true;
      return false;
    }
    // Link an available_externally over a declaration.
    LinkFromSrc = !Src.isDeclaration() && Dest.isDeclaration();
    return false;
  }

  if (DestIsDeclaration) {
    // If Dest is external but Src is not:
    LinkFromSrc = true;
    return false;
  }

  if (Src.hasCommonLinkage()) {
    if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
      LinkFromSrc = true;
      return false;
    }

    if (!Dest.hasCommonLinkage()) {
      LinkFromSrc = false;
      return false;
    }

    const DataLayout &DL = Dest.getParent()->getDataLayout();
    uint64_t DestSize = DL.getTypeAllocSize(Dest.getValueType());
    uint64_t SrcSize = DL.getTypeAllocSize(Src.getValueType());
    LinkFromSrc = SrcSize > DestSize;
    return false;
  }

  if (Src.isWeakForLinker()) {
    assert(!Dest.hasExternalWeakLinkage());
    assert(!Dest.hasAvailableExternallyLinkage());

    if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
      LinkFromSrc = true;
      return false;
    }

    LinkFromSrc = false;
    return false;
  }

  if (Dest.isWeakForLinker()) {
    assert(Src.hasExternalLinkage());
    LinkFromSrc = true;
    return false;
  }

  assert(!Src.hasExternalWeakLinkage());
  assert(!Dest.hasExternalWeakLinkage());
  assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
         "Unexpected linkage type!");
  return emitError("Linking globals named '" + Src.getName() +
                   "': symbol multiply defined!");
}
Esempio n. 3
0
/// Loop over all of the linked values to compute type mappings.  For example,
/// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
/// types 'Foo' but one got renamed when the module was loaded into the same
/// LLVMContext.
void IRLinker::computeTypeMapping() {
  for (GlobalValue &SGV : SrcM->globals()) {
    GlobalValue *DGV = getLinkedToGlobal(&SGV);
    if (!DGV)
      continue;

    if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
      continue;
    }

    // Unify the element type of appending arrays.
    ArrayType *DAT = cast<ArrayType>(DGV->getValueType());
    ArrayType *SAT = cast<ArrayType>(SGV.getValueType());
    TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
  }

  for (GlobalValue &SGV : *SrcM)
    if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());

  for (GlobalValue &SGV : SrcM->aliases())
    if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());

  // Incorporate types by name, scanning all the types in the source module.
  // At this point, the destination module may have a type "%foo = { i32 }" for
  // example.  When the source module got loaded into the same LLVMContext, if
  // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
  std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
  for (StructType *ST : Types) {
    if (!ST->hasName())
      continue;

    // Check to see if there is a dot in the name followed by a digit.
    size_t DotPos = ST->getName().rfind('.');
    if (DotPos == 0 || DotPos == StringRef::npos ||
        ST->getName().back() == '.' ||
        !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
      continue;

    // Check to see if the destination module has a struct with the prefix name.
    StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos));
    if (!DST)
      continue;

    // Don't use it if this actually came from the source module. They're in
    // the same LLVMContext after all. Also don't use it unless the type is
    // actually used in the destination module. This can happen in situations
    // like this:
    //
    //      Module A                         Module B
    //      --------                         --------
    //   %Z = type { %A }                %B = type { %C.1 }
    //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
    //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
    //   %C = type { i8* }               %B.3 = type { %C.1 }
    //
    // When we link Module B with Module A, the '%B' in Module B is
    // used. However, that would then use '%C.1'. But when we process '%C.1',
    // we prefer to take the '%C' version. So we are then left with both
    // '%C.1' and '%C' being used for the same types. This leads to some
    // variables using one type and some using the other.
    if (TypeMap.DstStructTypesSet.hasType(DST))
      TypeMap.addTypeMapping(DST, ST);
  }

  // Now that we have discovered all of the type equivalences, get a body for
  // any 'opaque' types in the dest module that are now resolved.
  TypeMap.linkDefinedTypeBodies();
}
Esempio n. 4
0
bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
                                        const GlobalValue &Dest,
                                        const GlobalValue &Src) {

    // Should we unconditionally use the Src?
    if (shouldOverrideFromSrc()) {
        LinkFromSrc = true;
        return false;
    }

    // We always have to add Src if it has appending linkage.
    if (Src.hasAppendingLinkage()) {
        // Should have prevented importing for appending linkage in linkIfNeeded.
        assert(!isPerformingImport());
        LinkFromSrc = true;
        return false;
    }

    bool SrcIsDeclaration = Src.isDeclarationForLinker();
    bool DestIsDeclaration = Dest.isDeclarationForLinker();

    if (isPerformingImport()) {
        if (isa<Function>(&Src)) {
            // For functions, LinkFromSrc iff this is a function requested
            // for importing. For variables, decide below normally.
            LinkFromSrc = GlobalsToImport->count(&Src);
            return false;
        }

        // Check if this is an alias with an already existing definition
        // in Dest, which must have come from a prior importing pass from
        // the same Src module. Unlike imported function and variable
        // definitions, which are imported as available_externally and are
        // not definitions for the linker, that is not a valid linkage for
        // imported aliases which must be definitions. Simply use the existing
        // Dest copy.
        if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
            assert(isa<GlobalAlias>(&Dest));
            LinkFromSrc = false;
            return false;
        }
    }

    if (SrcIsDeclaration) {
        // If Src is external or if both Src & Dest are external..  Just link the
        // external globals, we aren't adding anything.
        if (Src.hasDLLImportStorageClass()) {
            // If one of GVs is marked as DLLImport, result should be dllimport'ed.
            LinkFromSrc = DestIsDeclaration;
            return false;
        }
        // If the Dest is weak, use the source linkage.
        if (Dest.hasExternalWeakLinkage()) {
            LinkFromSrc = true;
            return false;
        }
        // Link an available_externally over a declaration.
        LinkFromSrc = !Src.isDeclaration() && Dest.isDeclaration();
        return false;
    }

    if (DestIsDeclaration) {
        // If Dest is external but Src is not:
        LinkFromSrc = true;
        return false;
    }

    if (Src.hasCommonLinkage()) {
        if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
            LinkFromSrc = true;
            return false;
        }

        if (!Dest.hasCommonLinkage()) {
            LinkFromSrc = false;
            return false;
        }

        const DataLayout &DL = Dest.getParent()->getDataLayout();
        uint64_t DestSize = DL.getTypeAllocSize(Dest.getValueType());
        uint64_t SrcSize = DL.getTypeAllocSize(Src.getValueType());
        LinkFromSrc = SrcSize > DestSize;
        return false;
    }

    if (Src.isWeakForLinker()) {
        assert(!Dest.hasExternalWeakLinkage());
        assert(!Dest.hasAvailableExternallyLinkage());

        if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
            LinkFromSrc = true;
            return false;
        }

        LinkFromSrc = false;
        return false;
    }

    if (Dest.isWeakForLinker()) {
        assert(Src.hasExternalLinkage());
        LinkFromSrc = true;
        return false;
    }

    assert(!Src.hasExternalWeakLinkage());
    assert(!Dest.hasExternalWeakLinkage());
    assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
           "Unexpected linkage type!");
    return emitError("Linking globals named '" + Src.getName() +
                     "': symbol multiply defined!");
}