LLVM  4.0.0
MCLinkerOptimizationHint.cpp
Go to the documentation of this file.
1 //===-- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling -*- C++ -*-===//
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 
11 #include "llvm/MC/MCAsmLayout.h"
12 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/Support/LEB128.h"
15 
16 using namespace llvm;
17 
18 // Each LOH is composed by, in this order (each field is encoded using ULEB128):
19 // - Its kind.
20 // - Its number of arguments (let say N).
21 // - Its arg1.
22 // - ...
23 // - Its argN.
24 // <arg1> to <argN> are absolute addresses in the object file, i.e.,
25 // relative addresses from the beginning of the object file.
26 void MCLOHDirective::emit_impl(raw_ostream &OutStream,
27  const MachObjectWriter &ObjWriter,
28  const MCAsmLayout &Layout) const {
29  encodeULEB128(Kind, OutStream);
30  encodeULEB128(Args.size(), OutStream);
31  for (const MCSymbol *Arg : Args)
32  encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream);
33 }
34 
36  const MCAsmLayout &Layout) const {
37  raw_ostream &OutStream = ObjWriter.getStream();
38  emit_impl(OutStream, ObjWriter, Layout);
39 }
40 
42  const MCAsmLayout &Layout) const {
43  class raw_counting_ostream : public raw_ostream {
44  uint64_t Count;
45 
46  void write_impl(const char *, size_t size) override { Count += size; }
47 
48  uint64_t current_pos() const override { return Count; }
49 
50  public:
51  raw_counting_ostream() : Count(0) {}
52  ~raw_counting_ostream() override { flush(); }
53  };
54 
55  raw_counting_ostream OutStream;
56  emit_impl(OutStream, ObjWriter, Layout);
57  return OutStream.tell();
58 }
raw_pwrite_stream & getStream()
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Emit this directive as: <kind, numArgs, addr1, ..., addrN>
uint64_t getEmitSize(const MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Get the size in bytes of this directive if emitted in ObjWriter with the given Layout.
const unsigned Kind
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned Padding=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:38
uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const