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