blob: 41816a0b08ad2cf94fd93c5d72bb34552c0f1e76 [file] [log] [blame]
Derek Schufff2af0602020-01-29 17:30:57 -08001//===- WasmObjcopy.cpp ----------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "WasmObjcopy.h"
10#include "Buffer.h"
11#include "CopyConfig.h"
12#include "Object.h"
13#include "Reader.h"
14#include "Writer.h"
15#include "llvm-objcopy.h"
16#include "llvm/Support/Errc.h"
17
18namespace llvm {
19namespace objcopy {
20namespace wasm {
21
22using namespace object;
23
24static Error handleArgs(const CopyConfig &Config, Object &Obj) {
25 if (!Config.AddGnuDebugLink.empty() || !Config.BuildIdLinkDir.empty() ||
26 Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
27 Config.ExtractPartition || !Config.SplitDWO.empty() ||
28 !Config.SymbolsPrefix.empty() || !Config.AllocSectionsPrefix.empty() ||
29 Config.DiscardMode != DiscardType::None || Config.NewSymbolVisibility ||
30 !Config.SymbolsToAdd.empty() || !Config.RPathToAdd.empty() ||
31 !Config.OnlySection.empty() || !Config.SymbolsToGlobalize.empty() ||
32 !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
33 !Config.SymbolsToRemove.empty() ||
34 !Config.UnneededSymbolsToRemove.empty() ||
35 !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
36 !Config.SectionsToRename.empty() || !Config.SetSectionAlignment.empty() ||
37 !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty() ||
38 !Config.ToRemove.empty() || !Config.DumpSection.empty() ||
39 !Config.AddSection.empty()) {
40 return createStringError(
41 llvm::errc::invalid_argument,
42 "no flags are supported yet, only basic copying is allowed");
43 }
44 return Error::success();
45}
46
47Error executeObjcopyOnBinary(const CopyConfig &Config,
48 object::WasmObjectFile &In, Buffer &Out) {
49 Reader TheReader(In);
50 Expected<std::unique_ptr<Object>> ObjOrErr = TheReader.create();
51 if (!ObjOrErr)
52 return createFileError(Config.InputFilename, ObjOrErr.takeError());
53 Object *Obj = ObjOrErr->get();
54 assert(Obj && "Unable to deserialize Wasm object");
55 if (Error E = handleArgs(Config, *Obj))
56 return createFileError(Config.InputFilename, std::move(E));
57 Writer TheWriter(*Obj, Out);
58 if (Error E = TheWriter.write())
59 return createFileError(Config.OutputFilename, std::move(E));
60 return Error::success();
61}
62
63} // end namespace wasm
64} // end namespace objcopy
65} // end namespace llvm