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