2017-02-11 08:27:28 +08:00
|
|
|
//===- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling ------------===//
|
2014-03-29 15:34:53 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-03-29 15:34:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCLinkerOptimizationHint.h"
|
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
2016-06-23 07:23:08 +08:00
|
|
|
#include "llvm/MC/MCMachObjectWriter.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2014-03-29 15:34:53 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-02 09:02:28 +08:00
|
|
|
// Each LOH is composed by, in this order (each field is encoded using ULEB128):
|
|
|
|
// - Its kind.
|
|
|
|
// - Its number of arguments (let say N).
|
|
|
|
// - Its arg1.
|
|
|
|
// - ...
|
|
|
|
// - Its argN.
|
|
|
|
// <arg1> to <argN> are absolute addresses in the object file, i.e.,
|
|
|
|
// relative addresses from the beginning of the object file.
|
2015-06-02 07:55:06 +08:00
|
|
|
void MCLOHDirective::emit_impl(raw_ostream &OutStream,
|
2014-03-29 15:34:53 +08:00
|
|
|
const MachObjectWriter &ObjWriter,
|
|
|
|
const MCAsmLayout &Layout) const {
|
|
|
|
encodeULEB128(Kind, OutStream);
|
|
|
|
encodeULEB128(Args.size(), OutStream);
|
2016-06-26 22:49:00 +08:00
|
|
|
for (const MCSymbol *Arg : Args)
|
|
|
|
encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream);
|
2014-03-29 15:34:53 +08:00
|
|
|
}
|
2016-06-23 07:23:08 +08:00
|
|
|
|
|
|
|
void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
|
|
|
|
const MCAsmLayout &Layout) const {
|
2018-05-22 02:17:42 +08:00
|
|
|
raw_ostream &OutStream = ObjWriter.W.OS;
|
2016-06-23 07:23:08 +08:00
|
|
|
emit_impl(OutStream, ObjWriter, Layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
|
|
|
|
const MCAsmLayout &Layout) const {
|
|
|
|
class raw_counting_ostream : public raw_ostream {
|
2017-02-11 08:27:28 +08:00
|
|
|
uint64_t Count = 0;
|
2016-06-23 07:23:08 +08:00
|
|
|
|
|
|
|
void write_impl(const char *, size_t size) override { Count += size; }
|
|
|
|
|
|
|
|
uint64_t current_pos() const override { return Count; }
|
|
|
|
|
|
|
|
public:
|
2017-02-11 08:27:28 +08:00
|
|
|
raw_counting_ostream() = default;
|
2016-06-23 07:23:08 +08:00
|
|
|
~raw_counting_ostream() override { flush(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
raw_counting_ostream OutStream;
|
|
|
|
emit_impl(OutStream, ObjWriter, Layout);
|
|
|
|
return OutStream.tell();
|
|
|
|
}
|