MCLinkerOptimizationHint.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //===- llvm/MC/MCLinkerOptimizationHint.cpp ----- LOH handling ------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/MC/MCLinkerOptimizationHint.h"
  9. #include "llvm/MC/MCAsmLayout.h"
  10. #include "llvm/MC/MCMachObjectWriter.h"
  11. #include "llvm/Support/LEB128.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include <cstddef>
  14. #include <cstdint>
  15. using namespace llvm;
  16. // Each LOH is composed by, in this order (each field is encoded using ULEB128):
  17. // - Its kind.
  18. // - Its number of arguments (let say N).
  19. // - Its arg1.
  20. // - ...
  21. // - Its argN.
  22. // <arg1> to <argN> are absolute addresses in the object file, i.e.,
  23. // relative addresses from the beginning of the object file.
  24. void MCLOHDirective::emit_impl(raw_ostream &OutStream,
  25. const MachObjectWriter &ObjWriter,
  26. const MCAsmLayout &Layout) const {
  27. encodeULEB128(Kind, OutStream);
  28. encodeULEB128(Args.size(), OutStream);
  29. for (const MCSymbol *Arg : Args)
  30. encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream);
  31. }
  32. void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
  33. const MCAsmLayout &Layout) const {
  34. raw_ostream &OutStream = ObjWriter.W.OS;
  35. emit_impl(OutStream, ObjWriter, Layout);
  36. }
  37. uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
  38. const MCAsmLayout &Layout) const {
  39. class raw_counting_ostream : public raw_ostream {
  40. uint64_t Count = 0;
  41. void write_impl(const char *, size_t size) override { Count += size; }
  42. uint64_t current_pos() const override { return Count; }
  43. public:
  44. raw_counting_ostream() = default;
  45. ~raw_counting_ostream() override { flush(); }
  46. };
  47. raw_counting_ostream OutStream;
  48. emit_impl(OutStream, ObjWriter, Layout);
  49. return OutStream.tell();
  50. }