blob: e5f5f703289afee318b7ddd291d20a0247d3d138 [file] [log] [blame]
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001//===- lib/MC/MCObjectWriter.cpp - MCObjectWriter implementation ----------===//
Daniel Dunbar53b23382010-03-19 09:28:59 +00002//
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
Rafael Espindola31327802010-12-18 06:27:54 +000010#include "llvm/MC/MCExpr.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000011#include "llvm/MC/MCObjectWriter.h"
Rafael Espindola31327802010-12-18 06:27:54 +000012#include "llvm/MC/MCSymbol.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000013
14using namespace llvm;
15
16MCObjectWriter::~MCObjectWriter() {
17}
Kevin Enderbyc0957932010-09-30 16:52:03 +000018
19/// Utility function to encode a SLEB128 value.
20void MCObjectWriter::EncodeSLEB128(int64_t Value, raw_ostream &OS) {
21 bool More;
22 do {
23 uint8_t Byte = Value & 0x7f;
24 // NOTE: this assumes that this signed shift is an arithmetic right shift.
25 Value >>= 7;
26 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
27 ((Value == -1) && ((Byte & 0x40) != 0))));
28 if (More)
29 Byte |= 0x80; // Mark this byte that that more bytes will follow.
30 OS << char(Byte);
31 } while (More);
32}
33
34/// Utility function to encode a ULEB128 value.
35void MCObjectWriter::EncodeULEB128(uint64_t Value, raw_ostream &OS) {
36 do {
37 uint8_t Byte = Value & 0x7f;
38 Value >>= 7;
39 if (Value != 0)
40 Byte |= 0x80; // Mark this byte that that more bytes will follow.
41 OS << char(Byte);
42 } while (Value != 0);
43}
Rafael Espindola31327802010-12-18 06:27:54 +000044
45bool
46MCObjectWriter::IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
47 const MCSymbolRefExpr *A,
48 const MCSymbolRefExpr *B,
49 bool InSet) const {
50 // Modified symbol references cannot be resolved.
51 if (A->getKind() != MCSymbolRefExpr::VK_None ||
52 B->getKind() != MCSymbolRefExpr::VK_None)
53 return false;
54
55 const MCSymbol &SA = A->getSymbol();
56 const MCSymbol &SB = B->getSymbol();
57 if (SA.isUndefined() || SB.isUndefined())
58 return false;
59
60 // On ELF and COFF A - B is absolute if A and B are in the same section.
61 return &SA.getSection() == &SB.getSection();
62}