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 
10 #include "llvm/MC/MCLinkerOptimizationHint.h"
11 #include "llvm/MC/MCAsmLayout.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCMachObjectWriter.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 
35 void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
36                           const MCAsmLayout &Layout) const {
37   raw_ostream &OutStream = ObjWriter.getStream();
38   emit_impl(OutStream, ObjWriter, Layout);
39 }
40 
41 uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
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 }
59