xref: /llvm-project/llvm/lib/MC/MCObjectWriter.cpp (revision 3a90c27385e9a8b50e40a307822906a1303c310b)
1 //===- lib/MC/MCObjectWriter.cpp - MCObjectWriter implementation ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCObjectWriter.h"
10 #include "llvm/MC/MCExpr.h"
11 #include "llvm/MC/MCFragment.h"
12 #include "llvm/MC/MCSymbol.h"
13 namespace llvm {
14 class MCSection;
15 }
16 
17 using namespace llvm;
18 
19 MCObjectWriter::~MCObjectWriter() = default;
20 
21 bool MCObjectWriter::isSymbolRefDifferenceFullyResolved(
22     const MCAssembler &Asm, const MCSymbolRefExpr *A, const MCSymbolRefExpr *B,
23     bool InSet) const {
24   // Modified symbol references cannot be resolved.
25   if (A->getKind() != MCSymbolRefExpr::VK_None ||
26       B->getKind() != MCSymbolRefExpr::VK_None)
27     return false;
28 
29   const MCSymbol &SA = A->getSymbol();
30   const MCSymbol &SB = B->getSymbol();
31   assert(!SA.isUndefined() && !SB.isUndefined());
32   MCFragment *FB = SB.getFragment();
33   if (!FB || !SA.getFragment())
34     return false;
35 
36   return isSymbolRefDifferenceFullyResolvedImpl(Asm, SA, *FB, InSet, /*IsPCRel=*/false);
37 }
38 
39 bool MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
40     const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
41     bool InSet, bool IsPCRel) const {
42   const MCSection &SecA = SymA.getSection();
43   const MCSection &SecB = *FB.getParent();
44   // On ELF and COFF  A - B is absolute if A and B are in the same section.
45   return &SecA == &SecB;
46 }
47