]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-objcopy/Buffer.cpp
Merge ^/head r352537 through r352586.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-objcopy / Buffer.cpp
1 //===- Buffer.cpp ---------------------------------------------------------===//
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 #include "Buffer.h"
10 #include "llvm/Support/FileOutputBuffer.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/Support/Process.h"
14 #include <memory>
15
16 namespace llvm {
17 namespace objcopy {
18
19 Buffer::~Buffer() {}
20
21 static Error createEmptyFile(StringRef FileName) {
22   // Create an empty tempfile and atomically swap it in place with the desired
23   // output file.
24   Expected<sys::fs::TempFile> Temp =
25       sys::fs::TempFile::create(FileName + ".temp-empty-%%%%%%%");
26   return Temp ? Temp->keep(FileName) : Temp.takeError();
27 }
28
29 Error FileBuffer::allocate(size_t Size) {
30   // When a 0-sized file is requested, skip allocation but defer file
31   // creation/truncation until commit() to avoid side effects if something
32   // happens between allocate() and commit().
33   if (Size == 0) {
34     EmptyFile = true;
35     return Error::success();
36   }
37
38   Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
39       FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable);
40   // FileOutputBuffer::create() returns an Error that is just a wrapper around
41   // std::error_code. Wrap it in FileError to include the actual filename.
42   if (!BufferOrErr)
43     return createFileError(getName(), BufferOrErr.takeError());
44   Buf = std::move(*BufferOrErr);
45   return Error::success();
46 }
47
48 Error FileBuffer::commit() {
49   if (EmptyFile)
50     return createEmptyFile(getName());
51
52   assert(Buf && "allocate() not called before commit()!");
53   Error Err = Buf->commit();
54   // FileOutputBuffer::commit() returns an Error that is just a wrapper around
55   // std::error_code. Wrap it in FileError to include the actual filename.
56   return Err ? createFileError(getName(), std::move(Err)) : std::move(Err);
57 }
58
59 uint8_t *FileBuffer::getBufferStart() {
60   return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
61 }
62
63 Error MemBuffer::allocate(size_t Size) {
64   Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
65   return Error::success();
66 }
67
68 Error MemBuffer::commit() { return Error::success(); }
69
70 uint8_t *MemBuffer::getBufferStart() {
71   return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
72 }
73
74 std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
75   return std::move(Buf);
76 }
77
78 } // end namespace objcopy
79 } // end namespace llvm