@@ -46,35 +46,6 @@ MCAssembler *MCObjectStreamer::getAssemblerPtr() {
4646 return nullptr ;
4747}
4848
49- // When fixup's offset is a forward declared label, e.g.:
50- //
51- // .reloc 1f, R_MIPS_JALR, foo
52- // 1: nop
53- //
54- // postpone adding it to Fixups vector until the label is defined and its offset
55- // is known.
56- void MCObjectStreamer::resolvePendingFixups () {
57- for (PendingMCFixup &PendingFixup : PendingFixups) {
58- if (!PendingFixup.Sym || PendingFixup.Sym ->isUndefined ()) {
59- getContext ().reportError (PendingFixup.Fixup .getLoc (),
60- " unresolved relocation offset" );
61- continue ;
62- }
63- PendingFixup.Fixup .setOffset (PendingFixup.Sym ->getOffset () +
64- PendingFixup.Fixup .getOffset ());
65-
66- // If the location symbol to relocate is in MCEncodedFragment,
67- // put the Fixup into location symbol's fragment. Otherwise
68- // put into PendingFixup.DF
69- MCFragment *F = PendingFixup.Sym ->getFragment ();
70- if (F->isEncoded ())
71- F->addFixup (PendingFixup.Fixup );
72- else
73- PendingFixup.DF ->addFixup (PendingFixup.Fixup );
74- }
75- PendingFixups.clear ();
76- }
77-
7849// As a compile-time optimization, avoid allocating and evaluating an MCExpr
7950// tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment's fixed
8051// part.
@@ -607,76 +578,14 @@ void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
607578 insert (getContext ().allocFragment <MCOrgFragment>(*Offset, Value, Loc));
608579}
609580
610- static std::optional<std::pair<bool , std::string>>
611- getOffsetAndDataFragment (const MCSymbol &Symbol, uint32_t &RelocOffset,
612- MCFragment *&DF) {
613- if (Symbol.isVariable ()) {
614- const MCExpr *SymbolExpr = Symbol.getVariableValue ();
615- MCValue OffsetVal;
616- if (!SymbolExpr->evaluateAsRelocatable (OffsetVal, nullptr ))
617- return std::make_pair (false ,
618- std::string (" symbol in .reloc offset is not "
619- " relocatable" ));
620- if (OffsetVal.isAbsolute ()) {
621- RelocOffset = OffsetVal.getConstant ();
622- MCFragment *Fragment = Symbol.getFragment ();
623- // FIXME Support symbols with no DF. For example:
624- // .reloc .data, ENUM_VALUE, <some expr>
625- if (!Fragment || Fragment->getKind () != MCFragment::FT_Data)
626- return std::make_pair (false ,
627- std::string (" symbol in offset has no data "
628- " fragment" ));
629- DF = cast<MCFragment>(Fragment);
630- return std::nullopt ;
631- }
632-
633- if (OffsetVal.getSubSym ())
634- return std::make_pair (false ,
635- std::string (" .reloc symbol offset is not "
636- " representable" ));
637-
638- const MCSymbol &SA = *OffsetVal.getAddSym ();
639- if (!SA.isDefined ())
640- return std::make_pair (false ,
641- std::string (" symbol used in the .reloc offset is "
642- " not defined" ));
643-
644- if (SA.isVariable ())
645- return std::make_pair (false ,
646- std::string (" symbol used in the .reloc offset is "
647- " variable" ));
648-
649- MCFragment *Fragment = SA.getFragment ();
650- // FIXME Support symbols with no DF. For example:
651- // .reloc .data, ENUM_VALUE, <some expr>
652- if (!Fragment || Fragment->getKind () != MCFragment::FT_Data)
653- return std::make_pair (false ,
654- std::string (" symbol in offset has no data "
655- " fragment" ));
656- RelocOffset = SA.getOffset () + OffsetVal.getConstant ();
657- DF = cast<MCFragment>(Fragment);
658- } else {
659- RelocOffset = Symbol.getOffset ();
660- MCFragment *Fragment = Symbol.getFragment ();
661- // FIXME Support symbols with no DF. For example:
662- // .reloc .data, ENUM_VALUE, <some expr>
663- if (!Fragment || Fragment->getKind () != MCFragment::FT_Data)
664- return std::make_pair (false ,
665- std::string (" symbol in offset has no data "
666- " fragment" ));
667- DF = cast<MCFragment>(Fragment);
668- }
669- return std::nullopt ;
670- }
671-
672- std::optional<std::pair<bool , std::string>>
673- MCObjectStreamer::emitRelocDirective (const MCExpr &Offset, StringRef Name,
674- const MCExpr *Expr, SMLoc Loc,
675- const MCSubtargetInfo &STI) {
581+ void MCObjectStreamer::emitRelocDirective (const MCExpr &Offset, StringRef Name,
582+ const MCExpr *Expr, SMLoc Loc) {
676583 std::optional<MCFixupKind> MaybeKind =
677584 Assembler->getBackend ().getFixupKind (Name);
678- if (!MaybeKind)
679- return std::make_pair (true , std::string (" unknown relocation name" ));
585+ if (!MaybeKind) {
586+ getContext ().reportError (Loc, " unknown relocation name" );
587+ return ;
588+ }
680589
681590 MCFixupKind Kind = *MaybeKind;
682591 if (Expr)
@@ -685,38 +594,14 @@ MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
685594 Expr =
686595 MCSymbolRefExpr::create (getContext ().createTempSymbol (), getContext ());
687596
688- MCFragment *DF = getOrCreateDataFragment (&STI);
689- MCValue OffsetVal;
690- if (!Offset.evaluateAsRelocatable (OffsetVal, nullptr ))
691- return std::make_pair (false ,
692- std::string (" .reloc offset is not relocatable" ));
693- if (OffsetVal.isAbsolute ()) {
694- if (OffsetVal.getConstant () < 0 )
695- return std::make_pair (false , std::string (" .reloc offset is negative" ));
696- DF->addFixup (MCFixup::create (OffsetVal.getConstant (), Expr, Kind));
697- return std::nullopt ;
698- }
699- if (OffsetVal.getSubSym ())
700- return std::make_pair (false ,
701- std::string (" .reloc offset is not representable" ));
702-
703- const MCSymbol &Symbol = *OffsetVal.getAddSym ();
704- if (Symbol.isDefined ()) {
705- uint32_t SymbolOffset = 0 ;
706- std::optional<std::pair<bool , std::string>> Error =
707- getOffsetAndDataFragment (Symbol, SymbolOffset, DF);
708-
709- if (Error != std::nullopt )
710- return Error;
711-
712- DF->addFixup (
713- MCFixup::create (SymbolOffset + OffsetVal.getConstant (), Expr, Kind));
714- return std::nullopt ;
597+ auto *O = &Offset;
598+ int64_t Val;
599+ if (Offset.evaluateAsAbsolute (Val, nullptr )) {
600+ auto *SecSym = getCurrentSectionOnly ()->getBeginSymbol ();
601+ O = MCBinaryExpr::createAdd (MCSymbolRefExpr::create (SecSym, getContext ()),
602+ O, getContext (), Loc);
715603 }
716-
717- PendingFixups.emplace_back (
718- &Symbol, DF, MCFixup::create (OffsetVal.getConstant (), Expr, Kind));
719- return std::nullopt ;
604+ getAssembler ().addRelocDirective ({*O, Expr, Kind});
720605}
721606
722607void MCObjectStreamer::emitFill (const MCExpr &NumBytes, uint64_t FillValue,
@@ -799,6 +684,5 @@ void MCObjectStreamer::finishImpl() {
799684 // Emit pseudo probes for the current module.
800685 MCPseudoProbeTable::emit (this );
801686
802- resolvePendingFixups ();
803687 getAssembler ().Finish ();
804688}
0 commit comments