Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 1 | //===- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling ------------===// |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/MC/MCLinkerOptimizationHint.h" |
| 10 | #include "llvm/MC/MCAsmLayout.h" |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCMachObjectWriter.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 12 | #include "llvm/Support/LEB128.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
| 14 | #include <cstddef> |
| 15 | #include <cstdint> |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | |
Quentin Colombet | 3c2b13b | 2014-04-02 01:02:28 +0000 | [diff] [blame] | 19 | // 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 Grosbach | 249af2a | 2015-06-01 23:55:06 +0000 | [diff] [blame] | 27 | void MCLOHDirective::emit_impl(raw_ostream &OutStream, |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 28 | const MachObjectWriter &ObjWriter, |
| 29 | const MCAsmLayout &Layout) const { |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 30 | encodeULEB128(Kind, OutStream); |
| 31 | encodeULEB128(Args.size(), OutStream); |
Benjamin Kramer | 7b4658f | 2016-06-26 14:49:00 +0000 | [diff] [blame] | 32 | for (const MCSymbol *Arg : Args) |
| 33 | encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream); |
Tim Northover | 53d3251 | 2014-03-29 07:34:53 +0000 | [diff] [blame] | 34 | } |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 35 | |
| 36 | void MCLOHDirective::emit(MachObjectWriter &ObjWriter, |
| 37 | const MCAsmLayout &Layout) const { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 38 | raw_ostream &OutStream = ObjWriter.W.OS; |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 39 | emit_impl(OutStream, ObjWriter, Layout); |
| 40 | } |
| 41 | |
| 42 | uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter, |
| 43 | const MCAsmLayout &Layout) const { |
| 44 | class raw_counting_ostream : public raw_ostream { |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 45 | uint64_t Count = 0; |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 46 | |
| 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 Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 52 | raw_counting_ostream() = default; |
Reid Kleckner | 858239d | 2016-06-22 23:23:08 +0000 | [diff] [blame] | 53 | ~raw_counting_ostream() override { flush(); } |
| 54 | }; |
| 55 | |
| 56 | raw_counting_ostream OutStream; |
| 57 | emit_impl(OutStream, ObjWriter, Layout); |
| 58 | return OutStream.tell(); |
| 59 | } |