blob: 06b2a20a762f8310855a8ef4ab7e9203a88a9551 [file] [log] [blame]
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +00001//===- Buffer.cpp ---------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Buffer.h"
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000010#include "llvm/Support/FileOutputBuffer.h"
Jordan Rupprechtb2702d62019-01-28 15:02:40 +000011#include "llvm/Support/FileSystem.h"
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000012#include "llvm/Support/MemoryBuffer.h"
Jordan Rupprechtb2702d62019-01-28 15:02:40 +000013#include "llvm/Support/Process.h"
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000014#include <memory>
15
16namespace llvm {
17namespace objcopy {
18
19Buffer::~Buffer() {}
20
Jordan Rupprechtb2702d62019-01-28 15:02:40 +000021static 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
Jordan Rupprecht881cae72019-01-22 23:49:16 +000029Error FileBuffer::allocate(size_t Size) {
Jordan Rupprechtb2702d62019-01-28 15:02:40 +000030 // 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
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000038 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
39 FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable);
Jordan Rupprecht881cae72019-01-22 23:49:16 +000040 // 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());
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000044 Buf = std::move(*BufferOrErr);
Jordan Rupprecht881cae72019-01-22 23:49:16 +000045 return Error::success();
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000046}
47
Jordan Rupprecht881cae72019-01-22 23:49:16 +000048Error FileBuffer::commit() {
Jordan Rupprechtb2702d62019-01-28 15:02:40 +000049 if (EmptyFile)
50 return createEmptyFile(getName());
51
52 assert(Buf && "allocate() not called before commit()!");
Jordan Rupprecht881cae72019-01-22 23:49:16 +000053 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}
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000058
59uint8_t *FileBuffer::getBufferStart() {
60 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
61}
62
Jordan Rupprecht881cae72019-01-22 23:49:16 +000063Error MemBuffer::allocate(size_t Size) {
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000064 Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
Jordan Rupprecht881cae72019-01-22 23:49:16 +000065 return Error::success();
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000066}
67
68Error MemBuffer::commit() { return Error::success(); }
69
70uint8_t *MemBuffer::getBufferStart() {
71 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
72}
73
74std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
75 return std::move(Buf);
76}
77
78} // end namespace objcopy
79} // end namespace llvm