blob: 2f8581470ea6ba758f110a133057d0779e3c0ffd [file] [log] [blame]
Eugene Zelenkoa700a602017-02-11 00:27:28 +00001//===- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling ------------===//
Tim Northover1330ee32014-03-29 07:34:53 +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
10#include "llvm/MC/MCLinkerOptimizationHint.h"
11#include "llvm/MC/MCAsmLayout.h"
Reid Kleckner26f1dde2016-06-22 23:23:08 +000012#include "llvm/MC/MCMachObjectWriter.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000013#include "llvm/Support/LEB128.h"
Eugene Zelenkoa700a602017-02-11 00:27:28 +000014#include "llvm/Support/raw_ostream.h"
15#include <cstddef>
16#include <cstdint>
Tim Northover1330ee32014-03-29 07:34:53 +000017
18using namespace llvm;
19
Quentin Colombet91b97cc2014-04-02 01:02:28 +000020// Each LOH is composed by, in this order (each field is encoded using ULEB128):
21// - Its kind.
22// - Its number of arguments (let say N).
23// - Its arg1.
24// - ...
25// - Its argN.
26// <arg1> to <argN> are absolute addresses in the object file, i.e.,
27// relative addresses from the beginning of the object file.
Jim Grosbach398f1752015-06-01 23:55:06 +000028void MCLOHDirective::emit_impl(raw_ostream &OutStream,
Tim Northover1330ee32014-03-29 07:34:53 +000029 const MachObjectWriter &ObjWriter,
30 const MCAsmLayout &Layout) const {
Tim Northover1330ee32014-03-29 07:34:53 +000031 encodeULEB128(Kind, OutStream);
32 encodeULEB128(Args.size(), OutStream);
Benjamin Kramerdd1d7a42016-06-26 14:49:00 +000033 for (const MCSymbol *Arg : Args)
34 encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream);
Tim Northover1330ee32014-03-29 07:34:53 +000035}
Reid Kleckner26f1dde2016-06-22 23:23:08 +000036
37void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
38 const MCAsmLayout &Layout) const {
Peter Collingbourne8e93a952018-05-21 18:17:42 +000039 raw_ostream &OutStream = ObjWriter.W.OS;
Reid Kleckner26f1dde2016-06-22 23:23:08 +000040 emit_impl(OutStream, ObjWriter, Layout);
41}
42
43uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
44 const MCAsmLayout &Layout) const {
45 class raw_counting_ostream : public raw_ostream {
Eugene Zelenkoa700a602017-02-11 00:27:28 +000046 uint64_t Count = 0;
Reid Kleckner26f1dde2016-06-22 23:23:08 +000047
48 void write_impl(const char *, size_t size) override { Count += size; }
49
50 uint64_t current_pos() const override { return Count; }
51
52 public:
Eugene Zelenkoa700a602017-02-11 00:27:28 +000053 raw_counting_ostream() = default;
Reid Kleckner26f1dde2016-06-22 23:23:08 +000054 ~raw_counting_ostream() override { flush(); }
55 };
56
57 raw_counting_ostream OutStream;
58 emit_impl(OutStream, ObjWriter, Layout);
59 return OutStream.tell();
60}