Alexey Lapshin 4f16e177e1 [llvm-objcopy][NFC] replace class Buffer/MemBuffer/FileBuffer with streams.
During D88827 it was requested to remove the local implementation
of Memory/File Buffers:

// TODO: refactor the buffer classes in LLVM to enable us to use them here
// directly.

This patch uses raw_ostream instead of Buffers. Generally, using streams
could allow us to reduce memory usages. No need to load all data into the
memory - the data could be streamed through a smaller buffer.
Thus, this patch uses raw_ostream as an interface for output data:

Error executeObjcopyOnBinary(CopyConfig &Config,
                             object::Binary &In,
                             raw_ostream &Out);

Note 1. This patch does not change the implementation of Writers
so that data would be directly stored into raw_ostream.
This is assumed to be done later.

Note 2. It would be better if Writers would be implemented in a such way
that data could be streamed without seeking/updating. If that would be
inconvenient then raw_ostream could be replaced with raw_pwrite_stream
to have a possibility to seek back and update file headers.
This is assumed to be done later if necessary.

Note 3. Current FileOutputBuffer allows using a memory-mapped file.
The raw_fd_ostream (which could be used if data should be stored in the file)
does not allow us to use a memory-mapped file. Memory map functionality
could be implemented for raw_fd_ostream:

It is possible to add resize() method into raw_ostream.

class raw_ostream {
  void resize(uint64_t size);
}

That method, implemented for raw_fd_ostream, could create a memory-mapped file.
The streamed data would be written into that memory file then.
Thus we would be able to use memory-mapped files with raw_fd_ostream.
This is assumed to be done later if necessary.

Differential Revision: https://reviews.llvm.org/D91028
2021-03-10 23:50:04 +03:00

113 lines
4.2 KiB
C++

//===- WasmObjcopy.cpp ----------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "WasmObjcopy.h"
#include "CopyConfig.h"
#include "Object.h"
#include "Reader.h"
#include "Writer.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileOutputBuffer.h"
namespace llvm {
namespace objcopy {
namespace wasm {
using namespace object;
static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
Object &Obj) {
for (const Section &Sec : Obj.Sections) {
if (Sec.Name == SecName) {
ArrayRef<uint8_t> Contents = Sec.Contents;
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
FileOutputBuffer::create(Filename, Contents.size());
if (!BufferOrErr)
return BufferOrErr.takeError();
std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
std::copy(Contents.begin(), Contents.end(), Buf->getBufferStart());
if (Error E = Buf->commit())
return E;
return Error::success();
}
}
return createStringError(errc::invalid_argument, "section '%s' not found",
SecName.str().c_str());
}
static Error handleArgs(const CopyConfig &Config, Object &Obj) {
// Only support AddSection, DumpSection, RemoveSection for now.
for (StringRef Flag : Config.DumpSection) {
StringRef SecName;
StringRef FileName;
std::tie(SecName, FileName) = Flag.split("=");
if (Error E = dumpSectionToFile(SecName, FileName, Obj))
return createFileError(FileName, std::move(E));
}
Obj.removeSections([&Config](const Section &Sec) {
if (Config.ToRemove.matches(Sec.Name))
return true;
return false;
});
for (StringRef Flag : Config.AddSection) {
StringRef SecName, FileName;
std::tie(SecName, FileName) = Flag.split("=");
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
MemoryBuffer::getFile(FileName);
if (!BufOrErr)
return createFileError(FileName, errorCodeToError(BufOrErr.getError()));
Section Sec;
Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM;
Sec.Name = SecName;
std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
Sec.Contents = makeArrayRef<uint8_t>(
reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
Buf->getBufferSize());
Obj.addSectionWithOwnedContents(Sec, std::move(Buf));
}
if (!Config.AddGnuDebugLink.empty() || Config.ExtractPartition ||
!Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() ||
!Config.AllocSectionsPrefix.empty() ||
Config.DiscardMode != DiscardType::None || Config.NewSymbolVisibility ||
!Config.SymbolsToAdd.empty() || !Config.RPathToAdd.empty() ||
!Config.OnlySection.empty() || !Config.SymbolsToGlobalize.empty() ||
!Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
!Config.SymbolsToRemove.empty() ||
!Config.UnneededSymbolsToRemove.empty() ||
!Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
!Config.SectionsToRename.empty() || !Config.SetSectionAlignment.empty() ||
!Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty()) {
return createStringError(
llvm::errc::invalid_argument,
"only add-section, dump-section, and remove-section are supported");
}
return Error::success();
}
Error executeObjcopyOnBinary(const CopyConfig &Config,
object::WasmObjectFile &In, raw_ostream &Out) {
Reader TheReader(In);
Expected<std::unique_ptr<Object>> ObjOrErr = TheReader.create();
if (!ObjOrErr)
return createFileError(Config.InputFilename, ObjOrErr.takeError());
Object *Obj = ObjOrErr->get();
assert(Obj && "Unable to deserialize Wasm object");
if (Error E = handleArgs(Config, *Obj))
return E;
Writer TheWriter(*Obj, Out);
if (Error E = TheWriter.write())
return createFileError(Config.OutputFilename, std::move(E));
return Error::success();
}
} // end namespace wasm
} // end namespace objcopy
} // end namespace llvm