FileWriter.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- FileWriter.h ---------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_GSYM_FILEWRITER_H
  14. #define LLVM_DEBUGINFO_GSYM_FILEWRITER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/Support/Endian.h"
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. namespace llvm {
  21. class raw_pwrite_stream;
  22. namespace gsym {
  23. /// A simplified binary data writer class that doesn't require targets, target
  24. /// definitions, architectures, or require any other optional compile time
  25. /// libraries to be enabled via the build process. This class needs the ability
  26. /// to seek to different spots in the binary stream that is produces to fixup
  27. /// offsets and sizes.
  28. class FileWriter {
  29. llvm::raw_pwrite_stream &OS;
  30. llvm::support::endianness ByteOrder;
  31. public:
  32. FileWriter(llvm::raw_pwrite_stream &S, llvm::support::endianness B)
  33. : OS(S), ByteOrder(B) {}
  34. ~FileWriter();
  35. /// Write a single uint8_t value into the stream at the current file
  36. /// position.
  37. ///
  38. /// \param Value The value to write into the stream.
  39. void writeU8(uint8_t Value);
  40. /// Write a single uint16_t value into the stream at the current file
  41. /// position. The value will be byte swapped if needed to match the byte
  42. /// order specified during construction.
  43. ///
  44. /// \param Value The value to write into the stream.
  45. void writeU16(uint16_t Value);
  46. /// Write a single uint32_t value into the stream at the current file
  47. /// position. The value will be byte swapped if needed to match the byte
  48. /// order specified during construction.
  49. ///
  50. /// \param Value The value to write into the stream.
  51. void writeU32(uint32_t Value);
  52. /// Write a single uint64_t value into the stream at the current file
  53. /// position. The value will be byte swapped if needed to match the byte
  54. /// order specified during construction.
  55. ///
  56. /// \param Value The value to write into the stream.
  57. void writeU64(uint64_t Value);
  58. /// Write the value into the stream encoded using signed LEB128 at the
  59. /// current file position.
  60. ///
  61. /// \param Value The value to write into the stream.
  62. void writeSLEB(int64_t Value);
  63. /// Write the value into the stream encoded using unsigned LEB128 at the
  64. /// current file position.
  65. ///
  66. /// \param Value The value to write into the stream.
  67. void writeULEB(uint64_t Value);
  68. /// Write an array of uint8_t values into the stream at the current file
  69. /// position.
  70. ///
  71. /// \param Data An array of values to write into the stream.
  72. void writeData(llvm::ArrayRef<uint8_t> Data);
  73. /// Write a NULL terminated C string into the stream at the current file
  74. /// position. The entire contents of Str will be written into the steam at
  75. /// the current file position and then an extra NULL termation byte will be
  76. /// written. It is up to the user to ensure that Str doesn't contain any NULL
  77. /// characters unless the additional NULL characters are desired.
  78. ///
  79. /// \param Str The value to write into the stream.
  80. void writeNullTerminated(llvm::StringRef Str);
  81. /// Fixup a uint32_t value at the specified offset in the stream. This
  82. /// function will save the current file position, seek to the specified
  83. /// offset, overwrite the data using Value, and then restore the file
  84. /// position to the previous file position.
  85. ///
  86. /// \param Value The value to write into the stream.
  87. /// \param Offset The offset at which to write the Value within the stream.
  88. void fixup32(uint32_t Value, uint64_t Offset);
  89. /// Pad with zeroes at the current file position until the current file
  90. /// position matches the specified alignment.
  91. ///
  92. /// \param Align An integer speciying the desired alignment. This does not
  93. /// need to be a power of two.
  94. void alignTo(size_t Align);
  95. /// Return the current offset within the file.
  96. ///
  97. /// \return The unsigned offset from the start of the file of the current
  98. /// file position.
  99. uint64_t tell();
  100. llvm::raw_pwrite_stream &get_stream() {
  101. return OS;
  102. }
  103. private:
  104. FileWriter(const FileWriter &rhs) = delete;
  105. void operator=(const FileWriter &rhs) = delete;
  106. };
  107. } // namespace gsym
  108. } // namespace llvm
  109. #endif // #ifndef LLVM_DEBUGINFO_GSYM_FILEWRITER_H
  110. #ifdef __GNUC__
  111. #pragma GCC diagnostic pop
  112. #endif