1 //===- FileWriter.cpp -------------------------------------------*- 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 #include "llvm/DebugInfo/GSYM/FileWriter.h"
10 #include "llvm/Support/LEB128.h"
11 #include "llvm/Support/raw_ostream.h"
17 FileWriter::~FileWriter() { OS
.flush(); }
19 void FileWriter::writeSLEB(int64_t S
) {
21 auto Length
= encodeSLEB128(S
, Bytes
);
22 assert(Length
< sizeof(Bytes
));
23 OS
.write(reinterpret_cast<const char *>(Bytes
), Length
);
26 void FileWriter::writeULEB(uint64_t U
) {
28 auto Length
= encodeULEB128(U
, Bytes
);
29 assert(Length
< sizeof(Bytes
));
30 OS
.write(reinterpret_cast<const char *>(Bytes
), Length
);
33 void FileWriter::writeU8(uint8_t U
) {
34 OS
.write(reinterpret_cast<const char *>(&U
), sizeof(U
));
37 void FileWriter::writeU16(uint16_t U
) {
38 const uint16_t Swapped
= support::endian::byte_swap(U
, ByteOrder
);
39 OS
.write(reinterpret_cast<const char *>(&Swapped
), sizeof(Swapped
));
42 void FileWriter::writeU32(uint32_t U
) {
43 const uint32_t Swapped
= support::endian::byte_swap(U
, ByteOrder
);
44 OS
.write(reinterpret_cast<const char *>(&Swapped
), sizeof(Swapped
));
47 void FileWriter::writeU64(uint64_t U
) {
48 const uint64_t Swapped
= support::endian::byte_swap(U
, ByteOrder
);
49 OS
.write(reinterpret_cast<const char *>(&Swapped
), sizeof(Swapped
));
52 void FileWriter::fixup32(uint32_t U
, uint64_t Offset
) {
53 const uint32_t Swapped
= support::endian::byte_swap(U
, ByteOrder
);
54 OS
.pwrite(reinterpret_cast<const char *>(&Swapped
), sizeof(Swapped
),
58 void FileWriter::writeData(llvm::ArrayRef
<uint8_t> Data
) {
59 OS
.write(reinterpret_cast<const char *>(Data
.data()), Data
.size());
62 void FileWriter::writeNullTerminated(llvm::StringRef Str
) {
66 uint64_t FileWriter::tell() {
70 void FileWriter::alignTo(size_t Align
) {
71 off_t Offset
= OS
.tell();
72 off_t AlignedOffset
= (Offset
+ Align
- 1) / Align
* Align
;
73 if (AlignedOffset
== Offset
)
75 off_t PadCount
= AlignedOffset
- Offset
;
76 OS
.write_zeros(PadCount
);