1*0b57cec5SDimitry Andric //===- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling ------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "llvm/MC/MCLinkerOptimizationHint.h" 10*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmLayout.h" 11*0b57cec5SDimitry Andric #include "llvm/MC/MCMachObjectWriter.h" 12*0b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 13*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 14*0b57cec5SDimitry Andric #include <cstddef> 15*0b57cec5SDimitry Andric #include <cstdint> 16*0b57cec5SDimitry Andric 17*0b57cec5SDimitry Andric using namespace llvm; 18*0b57cec5SDimitry Andric 19*0b57cec5SDimitry Andric // Each LOH is composed by, in this order (each field is encoded using ULEB128): 20*0b57cec5SDimitry Andric // - Its kind. 21*0b57cec5SDimitry Andric // - Its number of arguments (let say N). 22*0b57cec5SDimitry Andric // - Its arg1. 23*0b57cec5SDimitry Andric // - ... 24*0b57cec5SDimitry Andric // - Its argN. 25*0b57cec5SDimitry Andric // <arg1> to <argN> are absolute addresses in the object file, i.e., 26*0b57cec5SDimitry Andric // relative addresses from the beginning of the object file. 27*0b57cec5SDimitry Andric void MCLOHDirective::emit_impl(raw_ostream &OutStream, 28*0b57cec5SDimitry Andric const MachObjectWriter &ObjWriter, 29*0b57cec5SDimitry Andric const MCAsmLayout &Layout) const { 30*0b57cec5SDimitry Andric encodeULEB128(Kind, OutStream); 31*0b57cec5SDimitry Andric encodeULEB128(Args.size(), OutStream); 32*0b57cec5SDimitry Andric for (const MCSymbol *Arg : Args) 33*0b57cec5SDimitry Andric encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream); 34*0b57cec5SDimitry Andric } 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric void MCLOHDirective::emit(MachObjectWriter &ObjWriter, 37*0b57cec5SDimitry Andric const MCAsmLayout &Layout) const { 38*0b57cec5SDimitry Andric raw_ostream &OutStream = ObjWriter.W.OS; 39*0b57cec5SDimitry Andric emit_impl(OutStream, ObjWriter, Layout); 40*0b57cec5SDimitry Andric } 41*0b57cec5SDimitry Andric 42*0b57cec5SDimitry Andric uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter, 43*0b57cec5SDimitry Andric const MCAsmLayout &Layout) const { 44*0b57cec5SDimitry Andric class raw_counting_ostream : public raw_ostream { 45*0b57cec5SDimitry Andric uint64_t Count = 0; 46*0b57cec5SDimitry Andric 47*0b57cec5SDimitry Andric void write_impl(const char *, size_t size) override { Count += size; } 48*0b57cec5SDimitry Andric 49*0b57cec5SDimitry Andric uint64_t current_pos() const override { return Count; } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric public: 52*0b57cec5SDimitry Andric raw_counting_ostream() = default; 53*0b57cec5SDimitry Andric ~raw_counting_ostream() override { flush(); } 54*0b57cec5SDimitry Andric }; 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric raw_counting_ostream OutStream; 57*0b57cec5SDimitry Andric emit_impl(OutStream, ObjWriter, Layout); 58*0b57cec5SDimitry Andric return OutStream.tell(); 59*0b57cec5SDimitry Andric } 60