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