FileWriter.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //===- FileWriter.cpp -------------------------------------------*- C++ -*-===//
  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/DebugInfo/GSYM/FileWriter.h"
  9. #include "llvm/Support/LEB128.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. #include <cassert>
  12. using namespace llvm;
  13. using namespace gsym;
  14. FileWriter::~FileWriter() { OS.flush(); }
  15. void FileWriter::writeSLEB(int64_t S) {
  16. uint8_t Bytes[32];
  17. auto Length = encodeSLEB128(S, Bytes);
  18. assert(Length < sizeof(Bytes));
  19. OS.write(reinterpret_cast<const char *>(Bytes), Length);
  20. }
  21. void FileWriter::writeULEB(uint64_t U) {
  22. uint8_t Bytes[32];
  23. auto Length = encodeULEB128(U, Bytes);
  24. assert(Length < sizeof(Bytes));
  25. OS.write(reinterpret_cast<const char *>(Bytes), Length);
  26. }
  27. void FileWriter::writeU8(uint8_t U) {
  28. OS.write(reinterpret_cast<const char *>(&U), sizeof(U));
  29. }
  30. void FileWriter::writeU16(uint16_t U) {
  31. const uint16_t Swapped = support::endian::byte_swap(U, ByteOrder);
  32. OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
  33. }
  34. void FileWriter::writeU32(uint32_t U) {
  35. const uint32_t Swapped = support::endian::byte_swap(U, ByteOrder);
  36. OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
  37. }
  38. void FileWriter::writeU64(uint64_t U) {
  39. const uint64_t Swapped = support::endian::byte_swap(U, ByteOrder);
  40. OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
  41. }
  42. void FileWriter::fixup32(uint32_t U, uint64_t Offset) {
  43. const uint32_t Swapped = support::endian::byte_swap(U, ByteOrder);
  44. OS.pwrite(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped),
  45. Offset);
  46. }
  47. void FileWriter::writeData(llvm::ArrayRef<uint8_t> Data) {
  48. OS.write(reinterpret_cast<const char *>(Data.data()), Data.size());
  49. }
  50. void FileWriter::writeNullTerminated(llvm::StringRef Str) {
  51. OS << Str << '\0';
  52. }
  53. uint64_t FileWriter::tell() {
  54. return OS.tell();
  55. }
  56. void FileWriter::alignTo(size_t Align) {
  57. off_t Offset = OS.tell();
  58. off_t AlignedOffset = (Offset + Align - 1) / Align * Align;
  59. if (AlignedOffset == Offset)
  60. return;
  61. off_t PadCount = AlignedOffset - Offset;
  62. OS.write_zeros(PadCount);
  63. }