+static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
+ const MCSymbol *S = &Symbol;
+ while (S->isVariable()) {
+ const MCExpr *Value = S->getVariableValue();
+ if (Value->getKind() != MCExpr::SymbolRef)
+ return *S;
+ const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
+ S = &Ref->getSymbol();
+ }
+ return *S;
+}
+
+void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
+ // The presence of symbol versions causes undefined symbols and
+ // versions declared with @@@ to be renamed.
+
+ for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
+ ie = Asm.symbol_end(); it != ie; ++it) {
+ const MCSymbol &Alias = it->getSymbol();
+ const MCSymbol &Symbol = AliasedSymbol(Alias);
+ MCSymbolData &SD = Asm.getSymbolData(Symbol);
+
+ // Undefined symbols are global, but this is the first place we
+ // are able to set it.
+ if (Symbol.isUndefined() && !Symbol.isVariable()) {
+ if (GetBinding(SD) == ELF::STB_LOCAL) {
+ SetBinding(SD, ELF::STB_GLOBAL);
+ SetBinding(*it, ELF::STB_GLOBAL);
+ }
+ }
+
+ // Not an alias.
+ if (&Symbol == &Alias)
+ continue;
+
+ StringRef AliasName = Alias.getName();
+ size_t Pos = AliasName.find('@');
+ if (Pos == StringRef::npos)
+ continue;
+
+ // Aliases defined with .symvar copy the binding from the symbol they alias.
+ // This is the first place we are able to copy this information.
+ it->setExternal(SD.isExternal());
+ SetBinding(*it, GetBinding(SD));
+
+ StringRef Rest = AliasName.substr(Pos);
+ if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
+ continue;
+
+ // FIXME: produce a better error message.
+ if (Symbol.isUndefined() && Rest.startswith("@@") &&
+ !Rest.startswith("@@@"))
+ report_fatal_error("A @@ version cannot be undefined");
+
+ Renames.insert(std::make_pair(&Symbol, &Alias));
+ }
+}
+
+void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *SymtabF,
+ MCDataFragment *ShndxF,
+ ELFSymbolData &MSD,