1 //===- FileWriter.h ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_DEBUGINFO_GSYM_FILEWRITER_H
10 #define LLVM_DEBUGINFO_GSYM_FILEWRITER_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/Support/Endian.h"
17 #include <sys/types.h>
20 class raw_pwrite_stream
;
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.
30 llvm::raw_pwrite_stream
&OS
;
31 llvm::support::endianness ByteOrder
;
33 FileWriter(llvm::raw_pwrite_stream
&S
, llvm::support::endianness B
)
34 : OS(S
), ByteOrder(B
) {}
36 /// Write a single uint8_t value into the stream at the current file
39 /// \param Value The value to write into the stream.
40 void writeU8(uint8_t Value
);
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.
46 /// \param Value The value to write into the stream.
47 void writeU16(uint16_t Value
);
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.
53 /// \param Value The value to write into the stream.
54 void writeU32(uint32_t Value
);
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.
60 /// \param Value The value to write into the stream.
61 void writeU64(uint64_t Value
);
63 /// Write the value into the stream encoded using signed LEB128 at the
64 /// current file position.
66 /// \param Value The value to write into the stream.
67 void writeSLEB(int64_t Value
);
69 /// Write the value into the stream encoded using unsigned LEB128 at the
70 /// current file position.
72 /// \param Value The value to write into the stream.
73 void writeULEB(uint64_t Value
);
75 /// Write an array of uint8_t values into the stream at the current file
78 /// \param Data An array of values to write into the stream.
79 void writeData(llvm::ArrayRef
<uint8_t> Data
);
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.
87 /// \param Str The value to write into the stream.
88 void writeNullTerminated(llvm::StringRef Str
);
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.
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
);
99 /// Pad with zeroes at the current file position until the current file
100 /// position matches the specified alignment.
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
);
106 /// Return the current offset within the file.
108 /// \return The unsigned offset from the start of the file of the current
112 llvm::raw_pwrite_stream
&get_stream() {
117 FileWriter(const FileWriter
&rhs
) = delete;
118 void operator=(const FileWriter
&rhs
) = delete;
124 #endif // #ifndef LLVM_DEBUGINFO_GSYM_FILEWRITER_H