2018-10-16 07:40:18 +02:00
|
|
|
//===- Buffer.h -------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-16 07:40:18 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_OBJCOPY_BUFFER_H
|
|
|
|
#define LLVM_TOOLS_OBJCOPY_BUFFER_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace objcopy {
|
|
|
|
|
|
|
|
// The class Buffer abstracts out the common interface of FileOutputBuffer and
|
|
|
|
// WritableMemoryBuffer so that the hierarchy of Writers depends on this
|
|
|
|
// abstract interface and doesn't depend on a particular implementation.
|
|
|
|
// TODO: refactor the buffer classes in LLVM to enable us to use them here
|
|
|
|
// directly.
|
|
|
|
class Buffer {
|
|
|
|
StringRef Name;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~Buffer();
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-23 00:49:16 +01:00
|
|
|
virtual Error allocate(size_t Size) = 0;
|
2018-10-16 07:40:18 +02:00
|
|
|
virtual uint8_t *getBufferStart() = 0;
|
|
|
|
virtual Error commit() = 0;
|
|
|
|
|
|
|
|
explicit Buffer(StringRef Name) : Name(Name) {}
|
|
|
|
StringRef getName() const { return Name; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileBuffer : public Buffer {
|
|
|
|
std::unique_ptr<FileOutputBuffer> Buf;
|
[llvm-objcopy] Fix crash when writing empty binary output
Summary: When using llvm-objcopy -O binary and the resulting file will be empty (e.g. removing the only section that would be written, or using --only-keep with a section that doesn't exist/isn't SHF_ALLOC), we crash because FileOutputBuffer expects Size > 0. Add a regression test, and change Buffer to open/truncate the output file in this case.
Reviewers: alexshap, jhenderson, jakehehrlich, espindola
Reviewed By: alexshap, jhenderson
Subscribers: jfb, llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56806
llvm-svn: 352371
2019-01-28 16:02:40 +01:00
|
|
|
// Indicates that allocate(0) was called, and commit() should create or
|
|
|
|
// truncate a file instead of using a FileOutputBuffer.
|
|
|
|
bool EmptyFile = false;
|
2018-10-16 07:40:18 +02:00
|
|
|
|
|
|
|
public:
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-23 00:49:16 +01:00
|
|
|
Error allocate(size_t Size) override;
|
2018-10-16 07:40:18 +02:00
|
|
|
uint8_t *getBufferStart() override;
|
|
|
|
Error commit() override;
|
|
|
|
|
|
|
|
explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MemBuffer : public Buffer {
|
|
|
|
std::unique_ptr<WritableMemoryBuffer> Buf;
|
|
|
|
|
|
|
|
public:
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-23 00:49:16 +01:00
|
|
|
Error allocate(size_t Size) override;
|
2018-10-16 07:40:18 +02:00
|
|
|
uint8_t *getBufferStart() override;
|
|
|
|
Error commit() override;
|
|
|
|
|
|
|
|
explicit MemBuffer(StringRef Name) : Buffer(Name) {}
|
|
|
|
|
|
|
|
std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace objcopy
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_OBJCOPY_BUFFER_H
|