blob: 9ab321872b1135f791cb1c79436e757ccaf72e70 [file] [log] [blame]
Eugene Zelenkod3a6c892017-02-11 00:27:28 +00001//===- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling ------------===//
Tim Northover53d32512014-03-29 07:34:53 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tim Northover53d32512014-03-29 07:34:53 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/MC/MCLinkerOptimizationHint.h"
10#include "llvm/MC/MCAsmLayout.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000011#include "llvm/MC/MCMachObjectWriter.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000012#include "llvm/Support/LEB128.h"
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000013#include "llvm/Support/raw_ostream.h"
14#include <cstddef>
15#include <cstdint>
Tim Northover53d32512014-03-29 07:34:53 +000016
17using namespace llvm;
18
Quentin Colombet3c2b13b2014-04-02 01:02:28 +000019// Each LOH is composed by, in this order (each field is encoded using ULEB128):
20// - Its kind.
21// - Its number of arguments (let say N).
22// - Its arg1.
23// - ...
24// - Its argN.
25// <arg1> to <argN> are absolute addresses in the object file, i.e.,
26// relative addresses from the beginning of the object file.
Jim Grosbach249af2a2015-06-01 23:55:06 +000027void MCLOHDirective::emit_impl(raw_ostream &OutStream,
Tim Northover53d32512014-03-29 07:34:53 +000028 const MachObjectWriter &ObjWriter,
29 const MCAsmLayout &Layout) const {
Tim Northover53d32512014-03-29 07:34:53 +000030 encodeULEB128(Kind, OutStream);
31 encodeULEB128(Args.size(), OutStream);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +000032 for (const MCSymbol *Arg : Args)
33 encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream);
Tim Northover53d32512014-03-29 07:34:53 +000034}
Reid Kleckner858239d2016-06-22 23:23:08 +000035
36void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
37 const MCAsmLayout &Layout) const {
Peter Collingbournef17b1492018-05-21 18:17:42 +000038 raw_ostream &OutStream = ObjWriter.W.OS;
Reid Kleckner858239d2016-06-22 23:23:08 +000039 emit_impl(OutStream, ObjWriter, Layout);
40}
41
42uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
43 const MCAsmLayout &Layout) const {
44 class raw_counting_ostream : public raw_ostream {
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000045 uint64_t Count = 0;
Reid Kleckner858239d2016-06-22 23:23:08 +000046
47 void write_impl(const char *, size_t size) override { Count += size; }
48
49 uint64_t current_pos() const override { return Count; }
50
51 public:
Eugene Zelenkod3a6c892017-02-11 00:27:28 +000052 raw_counting_ostream() = default;
Reid Kleckner858239d2016-06-22 23:23:08 +000053 ~raw_counting_ostream() override { flush(); }
54 };
55
56 raw_counting_ostream OutStream;
57 emit_impl(OutStream, ObjWriter, Layout);
58 return OutStream.tell();
59}