Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 1 | //===-- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling -*- C++ -*-===// |
| 2 | // |
| 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" |
Pete Cooper | 885bd8a | 2015-03-04 01:24:24 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAssembler.h" |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCMachObjectWriter.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 14 | #include "llvm/Support/LEB128.h" |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
Quentin Colombet | 3c2b13b | 2014-04-02 01:02:28 +0000 | [diff] [blame] | 18 | // Each LOH is composed by, in this order (each field is encoded using ULEB128): |
| 19 | // - Its kind. |
| 20 | // - Its number of arguments (let say N). |
| 21 | // - Its arg1. |
| 22 | // - ... |
| 23 | // - Its argN. |
| 24 | // <arg1> to <argN> are absolute addresses in the object file, i.e., |
| 25 | // relative addresses from the beginning of the object file. |
Jim Grosbach | 249af2a | 2015-06-01 23:55:06 +0000 | [diff] [blame] | 26 | void MCLOHDirective::emit_impl(raw_ostream &OutStream, |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 27 | const MachObjectWriter &ObjWriter, |
| 28 | const MCAsmLayout &Layout) const { |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 29 | encodeULEB128(Kind, OutStream); |
| 30 | encodeULEB128(Args.size(), OutStream); |
Benjamin Kramer | 7b4658f | 2016-06-26 14:49:00 +0000 | [diff] [blame^] | 31 | for (const MCSymbol *Arg : Args) |
| 32 | encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream); |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 33 | } |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 34 | |
| 35 | void MCLOHDirective::emit(MachObjectWriter &ObjWriter, |
| 36 | const MCAsmLayout &Layout) const { |
| 37 | raw_ostream &OutStream = ObjWriter.getStream(); |
| 38 | emit_impl(OutStream, ObjWriter, Layout); |
| 39 | } |
| 40 | |
| 41 | uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter, |
| 42 | const MCAsmLayout &Layout) const { |
| 43 | class raw_counting_ostream : public raw_ostream { |
| 44 | uint64_t Count; |
| 45 | |
| 46 | void write_impl(const char *, size_t size) override { Count += size; } |
| 47 | |
| 48 | uint64_t current_pos() const override { return Count; } |
| 49 | |
| 50 | public: |
| 51 | raw_counting_ostream() : Count(0) {} |
| 52 | ~raw_counting_ostream() override { flush(); } |
| 53 | }; |
| 54 | |
| 55 | raw_counting_ostream OutStream; |
| 56 | emit_impl(OutStream, ObjWriter, Layout); |
| 57 | return OutStream.tell(); |
| 58 | } |