Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:42

0001 //===- FileWriter.h ---------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DEBUGINFO_GSYM_FILEWRITER_H
0010 #define LLVM_DEBUGINFO_GSYM_FILEWRITER_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/Support/Endian.h"
0014 
0015 #include <stddef.h>
0016 #include <stdint.h>
0017 #include <sys/types.h>
0018 
0019 namespace llvm {
0020 class raw_pwrite_stream;
0021 
0022 namespace gsym {
0023 
0024 /// A simplified binary data writer class that doesn't require targets, target
0025 /// definitions, architectures, or require any other optional compile time
0026 /// libraries to be enabled via the build process. This class needs the ability
0027 /// to seek to different spots in the binary stream that is produces to fixup
0028 /// offsets and sizes.
0029 class FileWriter {
0030   llvm::raw_pwrite_stream &OS;
0031   llvm::endianness ByteOrder;
0032 
0033 public:
0034   FileWriter(llvm::raw_pwrite_stream &S, llvm::endianness B)
0035       : OS(S), ByteOrder(B) {}
0036   ~FileWriter();
0037   /// Write a single uint8_t value into the stream at the current file
0038   /// position.
0039   ///
0040   /// \param   Value The value to write into the stream.
0041   void writeU8(uint8_t Value);
0042 
0043   /// Write a single uint16_t value into the stream at the current file
0044   /// position. The value will be byte swapped if needed to match the byte
0045   /// order specified during construction.
0046   ///
0047   /// \param   Value The value to write into the stream.
0048   void writeU16(uint16_t Value);
0049 
0050   /// Write a single uint32_t value into the stream at the current file
0051   /// position. The value will be byte swapped if needed to match the byte
0052   /// order specified during construction.
0053   ///
0054   /// \param   Value The value to write into the stream.
0055   void writeU32(uint32_t Value);
0056 
0057   /// Write a single uint64_t value into the stream at the current file
0058   /// position. The value will be byte swapped if needed to match the byte
0059   /// order specified during construction.
0060   ///
0061   /// \param   Value The value to write into the stream.
0062   void writeU64(uint64_t Value);
0063 
0064   /// Write the value into the stream encoded using signed LEB128 at the
0065   /// current file position.
0066   ///
0067   /// \param   Value The value to write into the stream.
0068   void writeSLEB(int64_t Value);
0069 
0070   /// Write the value into the stream encoded using unsigned LEB128 at the
0071   /// current file position.
0072   ///
0073   /// \param   Value The value to write into the stream.
0074   void writeULEB(uint64_t Value);
0075 
0076   /// Write an array of uint8_t values into the stream at the current file
0077   /// position.
0078   ///
0079   /// \param   Data An array of values to write into the stream.
0080   void writeData(llvm::ArrayRef<uint8_t> Data);
0081 
0082   /// Write a NULL terminated C string into the stream at the current file
0083   /// position. The entire contents of Str will be written into the steam at
0084   /// the current file position and then an extra NULL termation byte will be
0085   /// written. It is up to the user to ensure that Str doesn't contain any NULL
0086   /// characters unless the additional NULL characters are desired.
0087   ///
0088   /// \param   Str The value to write into the stream.
0089   void writeNullTerminated(llvm::StringRef Str);
0090 
0091   /// Fixup a uint32_t value at the specified offset in the stream. This
0092   /// function will save the current file position, seek to the specified
0093   /// offset, overwrite the data using Value, and then restore the file
0094   /// position to the previous file position.
0095   ///
0096   /// \param   Value The value to write into the stream.
0097   /// \param   Offset The offset at which to write the Value within the stream.
0098   void fixup32(uint32_t Value, uint64_t Offset);
0099 
0100   /// Pad with zeroes at the current file position until the current file
0101   /// position matches the specified alignment.
0102   ///
0103   /// \param  Align An integer speciying the desired alignment. This does not
0104   ///         need to be a power of two.
0105   void alignTo(size_t Align);
0106 
0107   /// Return the current offset within the file.
0108   ///
0109   /// \return The unsigned offset from the start of the file of the current
0110   ///         file position.
0111   uint64_t tell();
0112 
0113   llvm::raw_pwrite_stream &get_stream() {
0114     return OS;
0115   }
0116 
0117   llvm::endianness getByteOrder() const { return ByteOrder; }
0118 
0119 private:
0120   FileWriter(const FileWriter &rhs) = delete;
0121   void operator=(const FileWriter &rhs) = delete;
0122 };
0123 
0124 } // namespace gsym
0125 } // namespace llvm
0126 
0127 #endif // LLVM_DEBUGINFO_GSYM_FILEWRITER_H