Index: llvm/trunk/lib/MC/WasmObjectWriter.cpp =================================================================== --- llvm/trunk/lib/MC/WasmObjectWriter.cpp +++ llvm/trunk/lib/MC/WasmObjectWriter.cpp @@ -129,15 +129,15 @@ // Information about a single relocation. struct WasmRelocationEntry { - uint64_t Offset; // Where is the relocation. - const MCSymbolWasm *Symbol; // The symbol to relocate with. - int64_t Addend; // A value to add to the symbol. - unsigned Type; // The type of the relocation. - MCSectionWasm *FixupSection;// The section the relocation is targeting. + uint64_t Offset; // Where is the relocation. + const MCSymbolWasm *Symbol; // The symbol to relocate with. + int64_t Addend; // A value to add to the symbol. + unsigned Type; // The type of the relocation. + const MCSectionWasm *FixupSection;// The section the relocation is targeting. WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol, int64_t Addend, unsigned Type, - MCSectionWasm *FixupSection) + const MCSectionWasm *FixupSection) : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type), FixupSection(FixupSection) {} @@ -350,7 +350,7 @@ const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) { - MCSectionWasm &FixupSection = cast(*Fragment->getParent()); + const auto &FixupSection = cast(*Fragment->getParent()); uint64_t C = Target.getConstant(); uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); MCContext &Ctx = Asm.getContext(); @@ -477,8 +477,7 @@ if (!Sym->isDefined(/*SetUsed=*/false)) return UINT32_MAX; - MCSectionWasm &Section = - cast(RelEntry.Symbol->getSection(false)); + const auto &Section = cast(RelEntry.Symbol->getSection(false)); uint64_t Address = Section.getSectionOffset() + RelEntry.Addend; // Ignore overflow. LLVM allows address arithmetic to silently wrap. @@ -766,8 +765,7 @@ encodeULEB128(Functions.size(), getStream()); for (const WasmFunction &Func : Functions) { - MCSectionWasm &FuncSection = - static_cast(Func.Sym->getSection()); + auto &FuncSection = static_cast(Func.Sym->getSection()); int64_t Size = 0; if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout)) @@ -775,8 +773,7 @@ encodeULEB128(Size, getStream()); - FuncSection.setSectionOffset(getStream().tell() - - Section.ContentsOffset); + FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset); Asm.writeSectionData(&FuncSection, Layout); } @@ -1003,7 +1000,7 @@ const MCFragment &Frag = *GlobalVars->begin(); if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) report_fatal_error("only data supported in .global_variables"); - const MCDataFragment &DataFrag = cast(Frag); + const auto &DataFrag = cast(Frag); if (!DataFrag.getFixups().empty()) report_fatal_error("fixups not supported in .global_variables"); const SmallVectorImpl &Contents = DataFrag.getContents(); @@ -1059,7 +1056,7 @@ const MCFragment &Frag = *StackPtr->begin(); if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) report_fatal_error("only data supported in .stack_pointer"); - const MCDataFragment &DataFrag = cast(Frag); + const auto &DataFrag = cast(Frag); if (!DataFrag.getFixups().empty()) report_fatal_error("fixups not supported in .stack_pointer"); const SmallVectorImpl &Contents = DataFrag.getContents(); @@ -1093,8 +1090,6 @@ unsigned Index; - // << " function=" << S.isFunction() - if (WS.isFunction()) { // Prepare the function's type, if we haven't seen it yet. WasmFunctionType F; @@ -1142,68 +1137,65 @@ if (WS.isTemporary() && !WS.getSize()) continue; - if (WS.isDefined(/*SetUsed=*/false)) { - if (WS.getOffset() != 0) - report_fatal_error("data sections must contain one variable each: " + - WS.getName()); - if (!WS.getSize()) - report_fatal_error("data symbols must have a size set with .size: " + - WS.getName()); - - int64_t Size = 0; - if (!WS.getSize()->evaluateAsAbsolute(Size, Layout)) - report_fatal_error(".size expression must be evaluatable"); - - MCSectionWasm &DataSection = - static_cast(WS.getSection()); - - if (uint64_t(Size) != Layout.getSectionFileSize(&DataSection)) - report_fatal_error("data sections must contain at most one variable"); - - DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment())); - - DataSection.setSectionOffset(DataBytes.size()); - - for (MCSection::iterator I = DataSection.begin(), E = DataSection.end(); - I != E; ++I) { - const MCFragment &Frag = *I; - if (Frag.hasInstructions()) - report_fatal_error("only data supported in data sections"); - - if (const MCAlignFragment *Align = dyn_cast(&Frag)) { - if (Align->getValueSize() != 1) - report_fatal_error("only byte values supported for alignment"); - // If nops are requested, use zeros, as this is the data section. - uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue(); - uint64_t Size = std::min(alignTo(DataBytes.size(), - Align->getAlignment()), - DataBytes.size() + - Align->getMaxBytesToEmit()); - DataBytes.resize(Size, Value); - } else if (const MCFillFragment *Fill = - dyn_cast(&Frag)) { - DataBytes.insert(DataBytes.end(), Size, Fill->getValue()); - } else { - const MCDataFragment &DataFrag = cast(Frag); - const SmallVectorImpl &Contents = DataFrag.getContents(); + if (!WS.isDefined(/*SetUsed=*/false)) + continue; - DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end()); - } - } + if (WS.getOffset() != 0) + report_fatal_error("data sections must contain one variable each: " + + WS.getName()); + if (!WS.getSize()) + report_fatal_error("data symbols must have a size set with .size: " + + WS.getName()); + + int64_t Size = 0; + if (!WS.getSize()->evaluateAsAbsolute(Size, Layout)) + report_fatal_error(".size expression must be evaluatable"); + + auto &DataSection = static_cast(WS.getSection()); + + if (uint64_t(Size) != Layout.getSectionFileSize(&DataSection)) + report_fatal_error("data sections must contain at most one variable"); + + DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment())); + + DataSection.setSectionOffset(DataBytes.size()); + + for (const MCFragment &Frag : DataSection) { + if (Frag.hasInstructions()) + report_fatal_error("only data supported in data sections"); + + if (auto *Align = dyn_cast(&Frag)) { + if (Align->getValueSize() != 1) + report_fatal_error("only byte values supported for alignment"); + // If nops are requested, use zeros, as this is the data section. + uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue(); + uint64_t Size = std::min(alignTo(DataBytes.size(), + Align->getAlignment()), + DataBytes.size() + + Align->getMaxBytesToEmit()); + DataBytes.resize(Size, Value); + } else if (auto *Fill = dyn_cast(&Frag)) { + DataBytes.insert(DataBytes.end(), Size, Fill->getValue()); + } else { + const auto &DataFrag = cast(Frag); + const SmallVectorImpl &Contents = DataFrag.getContents(); - // For each global, prepare a corresponding wasm global holding its - // address. For externals these will also be named exports. - Index = NumGlobalImports + Globals.size(); - - WasmGlobal Global; - Global.Type = PtrType; - Global.IsMutable = false; - Global.HasImport = false; - Global.InitialValue = DataSection.getSectionOffset(); - Global.ImportIndex = 0; - SymbolIndices[&WS] = Index; - Globals.push_back(Global); + DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end()); + } } + + // For each global, prepare a corresponding wasm global holding its + // address. For externals these will also be named exports. + Index = NumGlobalImports + Globals.size(); + + WasmGlobal Global; + Global.Type = PtrType; + Global.IsMutable = false; + Global.HasImport = false; + Global.InitialValue = DataSection.getSectionOffset(); + Global.ImportIndex = 0; + SymbolIndices[&WS] = Index; + Globals.push_back(Global); } // If the symbol is visible outside this translation unit, export it. @@ -1231,7 +1223,7 @@ // Find the target symbol of this weak alias const MCExpr *Expr = WS.getVariableValue(); auto *Inner = dyn_cast(Expr); - const MCSymbolWasm *ResolvedSym = cast(&Inner->getSymbol()); + const auto *ResolvedSym = cast(&Inner->getSymbol()); uint32_t Index = SymbolIndices.find(ResolvedSym)->second; DEBUG(dbgs() << "Weak alias: '" << WS << "' -> '" << ResolvedSym << "' = " << Index << "\n"); SymbolIndices[&WS] = Index;