blob: f71fc7830129a672a222e1d1f61aa799cf2ab4e9 [file] [log] [blame]
Tim Northover53d32512014-03-29 07:34:53 +00001//===-- 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"
Pete Cooper885bd8a2015-03-04 01:24:24 +000012#include "llvm/MC/MCAssembler.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000013#include "llvm/MC/MCMachObjectWriter.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000014#include "llvm/Support/LEB128.h"
Tim Northover53d32512014-03-29 07:34:53 +000015
16using namespace llvm;
17
Quentin Colombet3c2b13b2014-04-02 01:02:28 +000018// 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.
Jim Grosbach249af2a2015-06-01 23:55:06 +000026void MCLOHDirective::emit_impl(raw_ostream &OutStream,
Tim Northover53d32512014-03-29 07:34:53 +000027 const MachObjectWriter &ObjWriter,
28 const MCAsmLayout &Layout) const {
Tim Northover53d32512014-03-29 07:34:53 +000029 encodeULEB128(Kind, OutStream);
30 encodeULEB128(Args.size(), OutStream);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +000031 for (const MCSymbol *Arg : Args)
32 encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream);
Tim Northover53d32512014-03-29 07:34:53 +000033}
Reid Kleckner858239d2016-06-22 23:23:08 +000034
35void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
36 const MCAsmLayout &Layout) const {
37 raw_ostream &OutStream = ObjWriter.getStream();
38 emit_impl(OutStream, ObjWriter, Layout);
39}
40
41uint64_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}