Derek Schuff | f2af060 | 2020-01-29 17:30:57 -0800 | [diff] [blame] | 1 | //===- 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 | |
| 18 | namespace llvm { |
| 19 | namespace objcopy { |
| 20 | namespace wasm { |
| 21 | |
| 22 | using namespace object; |
| 23 | |
Derek Schuff | b2c44de | 2020-02-11 15:13:40 -0800 | [diff] [blame] | 24 | static Error dumpSectionToFile(StringRef SecName, StringRef Filename, |
| 25 | Object &Obj) { |
| 26 | for (const Section &Sec : Obj.Sections) { |
| 27 | if (Sec.Name == SecName) { |
| 28 | ArrayRef<uint8_t> Contents = Sec.Contents; |
| 29 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 30 | FileOutputBuffer::create(Filename, Contents.size()); |
| 31 | if (!BufferOrErr) |
| 32 | return BufferOrErr.takeError(); |
| 33 | std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr); |
| 34 | std::copy(Contents.begin(), Contents.end(), Buf->getBufferStart()); |
| 35 | if (Error E = Buf->commit()) |
| 36 | return E; |
| 37 | return Error::success(); |
| 38 | } |
| 39 | } |
| 40 | return createStringError(errc::invalid_argument, "section '%s' not found", |
| 41 | SecName.str().c_str()); |
| 42 | } |
Derek Schuff | f2af060 | 2020-01-29 17:30:57 -0800 | [diff] [blame] | 43 | static Error handleArgs(const CopyConfig &Config, Object &Obj) { |
Derek Schuff | b2c44de | 2020-02-11 15:13:40 -0800 | [diff] [blame] | 44 | // Only support AddSection, DumpSection, RemoveSection for now. |
| 45 | for (StringRef Flag : Config.DumpSection) { |
| 46 | StringRef SecName; |
| 47 | StringRef FileName; |
| 48 | std::tie(SecName, FileName) = Flag.split("="); |
| 49 | if (Error E = dumpSectionToFile(SecName, FileName, Obj)) |
| 50 | return createFileError(FileName, std::move(E)); |
| 51 | } |
| 52 | |
| 53 | Obj.removeSections([&Config](const Section &Sec) { |
| 54 | if (Config.ToRemove.matches(Sec.Name)) |
| 55 | return true; |
| 56 | return false; |
| 57 | }); |
| 58 | |
| 59 | for (StringRef Flag : Config.AddSection) { |
| 60 | StringRef SecName, FileName; |
| 61 | std::tie(SecName, FileName) = Flag.split("="); |
| 62 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = |
| 63 | MemoryBuffer::getFile(FileName); |
| 64 | if (!BufOrErr) |
| 65 | return createFileError(FileName, errorCodeToError(BufOrErr.getError())); |
| 66 | Section Sec; |
| 67 | Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM; |
| 68 | Sec.Name = SecName; |
| 69 | std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr); |
| 70 | Sec.Contents = makeArrayRef<uint8_t>( |
| 71 | reinterpret_cast<const uint8_t *>(Buf->getBufferStart()), |
| 72 | Buf->getBufferSize()); |
| 73 | Obj.addSectionWithOwnedContents(Sec, std::move(Buf)); |
| 74 | } |
| 75 | |
Derek Schuff | f2af060 | 2020-01-29 17:30:57 -0800 | [diff] [blame] | 76 | if (!Config.AddGnuDebugLink.empty() || !Config.BuildIdLinkDir.empty() || |
| 77 | Config.BuildIdLinkInput || Config.BuildIdLinkOutput || |
| 78 | Config.ExtractPartition || !Config.SplitDWO.empty() || |
| 79 | !Config.SymbolsPrefix.empty() || !Config.AllocSectionsPrefix.empty() || |
| 80 | Config.DiscardMode != DiscardType::None || Config.NewSymbolVisibility || |
| 81 | !Config.SymbolsToAdd.empty() || !Config.RPathToAdd.empty() || |
| 82 | !Config.OnlySection.empty() || !Config.SymbolsToGlobalize.empty() || |
| 83 | !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() || |
| 84 | !Config.SymbolsToRemove.empty() || |
| 85 | !Config.UnneededSymbolsToRemove.empty() || |
| 86 | !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() || |
| 87 | !Config.SectionsToRename.empty() || !Config.SetSectionAlignment.empty() || |
Derek Schuff | b2c44de | 2020-02-11 15:13:40 -0800 | [diff] [blame] | 88 | !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty()) { |
Derek Schuff | f2af060 | 2020-01-29 17:30:57 -0800 | [diff] [blame] | 89 | return createStringError( |
| 90 | llvm::errc::invalid_argument, |
Derek Schuff | b2c44de | 2020-02-11 15:13:40 -0800 | [diff] [blame] | 91 | "only add-section, dump-section, and remove-section are supported"); |
Derek Schuff | f2af060 | 2020-01-29 17:30:57 -0800 | [diff] [blame] | 92 | } |
| 93 | return Error::success(); |
| 94 | } |
| 95 | |
| 96 | Error executeObjcopyOnBinary(const CopyConfig &Config, |
| 97 | object::WasmObjectFile &In, Buffer &Out) { |
| 98 | Reader TheReader(In); |
| 99 | Expected<std::unique_ptr<Object>> ObjOrErr = TheReader.create(); |
| 100 | if (!ObjOrErr) |
| 101 | return createFileError(Config.InputFilename, ObjOrErr.takeError()); |
| 102 | Object *Obj = ObjOrErr->get(); |
| 103 | assert(Obj && "Unable to deserialize Wasm object"); |
| 104 | if (Error E = handleArgs(Config, *Obj)) |
Derek Schuff | b2c44de | 2020-02-11 15:13:40 -0800 | [diff] [blame] | 105 | return E; |
Derek Schuff | f2af060 | 2020-01-29 17:30:57 -0800 | [diff] [blame] | 106 | Writer TheWriter(*Obj, Out); |
| 107 | if (Error E = TheWriter.write()) |
| 108 | return createFileError(Config.OutputFilename, std::move(E)); |
| 109 | return Error::success(); |
| 110 | } |
| 111 | |
| 112 | } // end namespace wasm |
| 113 | } // end namespace objcopy |
| 114 | } // end namespace llvm |