blob: 18887397ab617eaa6f17685218b74b1b03e7130e [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 Espindolafea753b2010-12-24 21:22:02 +000010#include "llvm/MC/MCAssembler.h"
Rafael Espindola31327802010-12-18 06:27:54 +000011#include "llvm/MC/MCExpr.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000012#include "llvm/MC/MCObjectWriter.h"
Rafael Espindola31327802010-12-18 06:27:54 +000013#include "llvm/MC/MCSymbol.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000014
15using namespace llvm;
16
17MCObjectWriter::~MCObjectWriter() {
18}
Kevin Enderbyc0957932010-09-30 16:52:03 +000019
20/// Utility function to encode a SLEB128 value.
21void 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.
Benjamin Kramerc25c9082011-11-05 11:52:44 +000036void MCObjectWriter::EncodeULEB128(uint64_t Value, raw_ostream &OS,
37 unsigned Padding) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000038 do {
39 uint8_t Byte = Value & 0x7f;
40 Value >>= 7;
Benjamin Kramerc25c9082011-11-05 11:52:44 +000041 if (Value != 0 || Padding != 0)
Kevin Enderbyc0957932010-09-30 16:52:03 +000042 Byte |= 0x80; // Mark this byte that that more bytes will follow.
43 OS << char(Byte);
44 } while (Value != 0);
Benjamin Kramerc25c9082011-11-05 11:52:44 +000045
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 }
Kevin Enderbyc0957932010-09-30 16:52:03 +000052}
Rafael Espindola31327802010-12-18 06:27:54 +000053
54bool
55MCObjectWriter::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();
Rafael Espindolafea753b2010-12-24 21:22:02 +000066 if (SA.AliasedSymbol().isUndefined() || SB.AliasedSymbol().isUndefined())
Rafael Espindola31327802010-12-18 06:27:54 +000067 return false;
68
Rafael Espindolafea753b2010-12-24 21:22:02 +000069 const MCSymbolData &DataA = Asm.getSymbolData(SA);
70 const MCSymbolData &DataB = Asm.getSymbolData(SB);
71
72 return IsSymbolRefDifferenceFullyResolvedImpl(Asm, DataA,
73 *DataB.getFragment(),
74 InSet,
75 false);
Rafael Espindola31327802010-12-18 06:27:54 +000076}
Rafael Espindola908159b2011-02-16 03:25:55 +000077
78bool
79MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
80 const MCSymbolData &DataA,
81 const MCFragment &FB,
82 bool InSet,
83 bool IsPCRel) const {
84 const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
85 const MCSection &SecB = FB.getParent()->getSection();
86 // On ELF and COFF A - B is absolute if A and B are in the same section.
87 return &SecA == &SecB;
88}