Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 1 | //===- Buffer.cpp ---------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Buffer.h" |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 10 | #include "llvm/Support/FileOutputBuffer.h" |
Jordan Rupprecht | b2702d6 | 2019-01-28 15:02:40 +0000 | [diff] [blame] | 11 | #include "llvm/Support/FileSystem.h" |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 12 | #include "llvm/Support/MemoryBuffer.h" |
Jordan Rupprecht | b2702d6 | 2019-01-28 15:02:40 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Process.h" |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
| 16 | namespace llvm { |
| 17 | namespace objcopy { |
| 18 | |
| 19 | Buffer::~Buffer() {} |
| 20 | |
Jordan Rupprecht | b2702d6 | 2019-01-28 15:02:40 +0000 | [diff] [blame] | 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 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 29 | Error FileBuffer::allocate(size_t Size) { |
Jordan Rupprecht | b2702d6 | 2019-01-28 15:02:40 +0000 | [diff] [blame] | 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 | |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 38 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 39 | FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 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()); |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 44 | Buf = std::move(*BufferOrErr); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 45 | return Error::success(); |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 48 | Error FileBuffer::commit() { |
Jordan Rupprecht | b2702d6 | 2019-01-28 15:02:40 +0000 | [diff] [blame] | 49 | if (EmptyFile) |
| 50 | return createEmptyFile(getName()); |
| 51 | |
| 52 | assert(Buf && "allocate() not called before commit()!"); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 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 | } |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 58 | |
| 59 | uint8_t *FileBuffer::getBufferStart() { |
| 60 | return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); |
| 61 | } |
| 62 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 63 | Error MemBuffer::allocate(size_t Size) { |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 64 | Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName()); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 65 | return Error::success(); |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 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 |