Path: blob/main/contrib/llvm-project/llvm/lib/MC/MCLinkerOptimizationHint.cpp
35233 views
//===- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling ------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/MC/MCLinkerOptimizationHint.h"9#include "llvm/MC/MCMachObjectWriter.h"10#include "llvm/Support/LEB128.h"11#include "llvm/Support/raw_ostream.h"12#include <cstddef>13#include <cstdint>1415using namespace llvm;1617// Each LOH is composed by, in this order (each field is encoded using ULEB128):18// - Its kind.19// - Its number of arguments (let say N).20// - Its arg1.21// - ...22// - Its argN.23// <arg1> to <argN> are absolute addresses in the object file, i.e.,24// relative addresses from the beginning of the object file.25void MCLOHDirective::emit_impl(const MCAssembler &Asm, raw_ostream &OutStream,26const MachObjectWriter &ObjWriter2728) const {29encodeULEB128(Kind, OutStream);30encodeULEB128(Args.size(), OutStream);31for (const MCSymbol *Arg : Args)32encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Asm), OutStream);33}3435void MCLOHDirective::emit(const MCAssembler &Asm,36MachObjectWriter &ObjWriter) const {37raw_ostream &OutStream = ObjWriter.W.OS;38emit_impl(Asm, OutStream, ObjWriter);39}4041uint64_t MCLOHDirective::getEmitSize(const MCAssembler &Asm,42const MachObjectWriter &ObjWriter) const {43class raw_counting_ostream : public raw_ostream {44uint64_t Count = 0;4546void write_impl(const char *, size_t size) override { Count += size; }4748uint64_t current_pos() const override { return Count; }4950public:51raw_counting_ostream() = default;52~raw_counting_ostream() override { flush(); }53};5455raw_counting_ostream OutStream;56emit_impl(Asm, OutStream, ObjWriter);57return OutStream.tell();58}596061