reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
//===- FileWriter.h ---------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_DEBUGINFO_GSYM_FILEWRITER_H
#define LLVM_DEBUGINFO_GSYM_FILEWRITER_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Endian.h"

#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>

namespace llvm {
class raw_pwrite_stream;

namespace gsym {

/// A simplified binary data writer class that doesn't require targets, target
/// definitions, architectures, or require any other optional compile time
/// libraries to be enabled via the build process. This class needs the ability
/// to seek to different spots in the binary stream that is produces to fixup
/// offsets and sizes.
class FileWriter {
  llvm::raw_pwrite_stream &OS;
  llvm::support::endianness ByteOrder;
public:
  FileWriter(llvm::raw_pwrite_stream &S, llvm::support::endianness B)
      : OS(S), ByteOrder(B) {}
  ~FileWriter();
  /// Write a single uint8_t value into the stream at the current file
  /// position.
  ///
  /// \param   Value The value to write into the stream.
  void writeU8(uint8_t Value);

  /// Write a single uint16_t value into the stream at the current file
  /// position. The value will be byte swapped if needed to match the byte
  /// order specified during construction.
  ///
  /// \param   Value The value to write into the stream.
  void writeU16(uint16_t Value);

  /// Write a single uint32_t value into the stream at the current file
  /// position. The value will be byte swapped if needed to match the byte
  /// order specified during construction.
  ///
  /// \param   Value The value to write into the stream.
  void writeU32(uint32_t Value);

  /// Write a single uint64_t value into the stream at the current file
  /// position. The value will be byte swapped if needed to match the byte
  /// order specified during construction.
  ///
  /// \param   Value The value to write into the stream.
  void writeU64(uint64_t Value);

  /// Write the value into the stream encoded using signed LEB128 at the
  /// current file position.
  ///
  /// \param   Value The value to write into the stream.
  void writeSLEB(int64_t Value);

  /// Write the value into the stream encoded using unsigned LEB128 at the
  /// current file position.
  ///
  /// \param   Value The value to write into the stream.
  void writeULEB(uint64_t Value);

  /// Write an array of uint8_t values into the stream at the current file
  /// position.
  ///
  /// \param   Data An array of values to write into the stream.
  void writeData(llvm::ArrayRef<uint8_t> Data);

  /// Write a NULL terminated C string into the stream at the current file
  /// position. The entire contents of Str will be written into the steam at
  /// the current file position and then an extra NULL termation byte will be
  /// written. It is up to the user to ensure that Str doesn't contain any NULL
  /// characters unless the additional NULL characters are desired.
  ///
  /// \param   Str The value to write into the stream.
  void writeNullTerminated(llvm::StringRef Str);

  /// Fixup a uint32_t value at the specified offset in the stream. This
  /// function will save the current file position, seek to the specified
  /// offset, overwrite the data using Value, and then restore the file
  /// position to the previous file position.
  ///
  /// \param   Value The value to write into the stream.
  /// \param   Offset The offset at which to write the Value within the stream.
  void fixup32(uint32_t Value, uint64_t Offset);

  /// Pad with zeroes at the current file position until the current file
  /// position matches the specified alignment.
  ///
  /// \param  Align An integer speciying the desired alignment. This does not
  ///         need to be a power of two.
  void alignTo(size_t Align);

  /// Return the current offset within the file.
  ///
  /// \return The unsigned offset from the start of the file of the current
  ///         file position.
  uint64_t tell();

  llvm::raw_pwrite_stream &get_stream() {
    return OS;
  }

private:
  FileWriter(const FileWriter &rhs) = delete;
  void operator=(const FileWriter &rhs) = delete;
};

} // namespace gsym
} // namespace llvm

#endif // #ifndef LLVM_DEBUGINFO_GSYM_FILEWRITER_H