Home | History | Annotate | Download | only in MC
      1 //===- lib/MC/MCObjectWriter.cpp - MCObjectWriter implementation ----------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "llvm/MC/MCAssembler.h"
     11 #include "llvm/MC/MCExpr.h"
     12 #include "llvm/MC/MCObjectWriter.h"
     13 #include "llvm/MC/MCSymbol.h"
     14 
     15 using namespace llvm;
     16 
     17 MCObjectWriter::~MCObjectWriter() {
     18 }
     19 
     20 /// Utility function to encode a SLEB128 value.
     21 void MCObjectWriter::EncodeSLEB128(int64_t Value, raw_ostream &OS) {
     22   bool More;
     23   do {
     24     uint8_t Byte = Value & 0x7f;
     25     // NOTE: this assumes that this signed shift is an arithmetic right shift.
     26     Value >>= 7;
     27     More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
     28               ((Value == -1) && ((Byte & 0x40) != 0))));
     29     if (More)
     30       Byte |= 0x80; // Mark this byte that that more bytes will follow.
     31     OS << char(Byte);
     32   } while (More);
     33 }
     34 
     35 /// Utility function to encode a ULEB128 value.
     36 void MCObjectWriter::EncodeULEB128(uint64_t Value, raw_ostream &OS,
     37                                    unsigned Padding) {
     38   do {
     39     uint8_t Byte = Value & 0x7f;
     40     Value >>= 7;
     41     if (Value != 0 || Padding != 0)
     42       Byte |= 0x80; // Mark this byte that that more bytes will follow.
     43     OS << char(Byte);
     44   } while (Value != 0);
     45 
     46   // Pad with 0x80 and emit a null byte at the end.
     47   if (Padding != 0) {
     48     for (; Padding != 1; --Padding)
     49       OS << '\x80';
     50     OS << '\x00';
     51   }
     52 }
     53 
     54 bool
     55 MCObjectWriter::IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
     56                                                    const MCSymbolRefExpr *A,
     57                                                    const MCSymbolRefExpr *B,
     58                                                    bool InSet) const {
     59   // Modified symbol references cannot be resolved.
     60   if (A->getKind() != MCSymbolRefExpr::VK_None ||
     61       B->getKind() != MCSymbolRefExpr::VK_None)
     62     return false;
     63 
     64   const MCSymbol &SA = A->getSymbol();
     65   const MCSymbol &SB = B->getSymbol();
     66   if (SA.AliasedSymbol().isUndefined() || SB.AliasedSymbol().isUndefined())
     67     return false;
     68 
     69   const MCSymbolData &DataA = Asm.getSymbolData(SA);
     70   const MCSymbolData &DataB = Asm.getSymbolData(SB);
     71   if(!DataA.getFragment() || !DataB.getFragment())
     72     return false;
     73 
     74   return IsSymbolRefDifferenceFullyResolvedImpl(Asm, DataA,
     75                                                 *DataB.getFragment(),
     76                                                 InSet,
     77                                                 false);
     78 }
     79 
     80 bool
     81 MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
     82                                                       const MCSymbolData &DataA,
     83                                                       const MCFragment &FB,
     84                                                       bool InSet,
     85                                                       bool IsPCRel) const {
     86   const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
     87   const MCSection &SecB = FB.getParent()->getSection();
     88   // On ELF and COFF  A - B is absolute if A and B are in the same section.
     89   return &SecA == &SecB;
     90 }
     91