| 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 "llvm/ObjCopy/wasm/WasmObjcopy.h" |
| 10 | #include "WasmObject.h" |
| 11 | #include "WasmReader.h" |
| 12 | #include "WasmWriter.h" |
| 13 | #include "llvm/ObjCopy/CommonConfig.h" |
| 14 | #include "llvm/Support/Errc.h" |
| 15 | #include "llvm/Support/FileOutputBuffer.h" |
| 16 | |
| 17 | namespace llvm { |
| 18 | namespace objcopy { |
| 19 | namespace wasm { |
| 20 | |
| 21 | using namespace object; |
| 22 | using SectionPred = std::function<bool(const Section &Sec)>; |
| 23 | |
| 24 | static bool isDebugSection(const Section &Sec) { |
| 25 | return Sec.Name.starts_with(Prefix: ".debug" ) || Sec.Name.starts_with(Prefix: "reloc..debug" ); |
| 26 | } |
| 27 | |
| 28 | static bool isLinkerSection(const Section &Sec) { |
| 29 | return Sec.Name.starts_with(Prefix: "reloc." ) || Sec.Name == "linking" ; |
| 30 | } |
| 31 | |
| 32 | static bool isNameSection(const Section &Sec) { return Sec.Name == "name" ; } |
| 33 | |
| 34 | // Sections which are known to be "comments" or informational and do not affect |
| 35 | // program semantics. |
| 36 | static bool (const Section &Sec) { |
| 37 | return Sec.Name == "producers" ; |
| 38 | } |
| 39 | |
| 40 | static Error dumpSectionToFile(StringRef SecName, StringRef Filename, |
| 41 | StringRef InputFilename, Object &Obj) { |
| 42 | for (const Section &Sec : Obj.Sections) { |
| 43 | if (Sec.Name == SecName) { |
| 44 | ArrayRef<uint8_t> Contents = Sec.Contents; |
| 45 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 46 | FileOutputBuffer::create(FilePath: Filename, Size: Contents.size()); |
| 47 | if (!BufferOrErr) |
| 48 | return createFileError(F: Filename, E: BufferOrErr.takeError()); |
| 49 | std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr); |
| 50 | llvm::copy(Range&: Contents, Out: Buf->getBufferStart()); |
| 51 | if (Error E = Buf->commit()) |
| 52 | return createFileError(F: Filename, E: std::move(E)); |
| 53 | return Error::success(); |
| 54 | } |
| 55 | } |
| 56 | return createFileError(F: Filename, EC: errc::invalid_argument, |
| 57 | Fmt: "section '%s' not found" , Vals: SecName.str().c_str()); |
| 58 | } |
| 59 | |
| 60 | static void removeSections(const CommonConfig &Config, Object &Obj) { |
| 61 | SectionPred RemovePred = [](const Section &) { return false; }; |
| 62 | |
| 63 | // Explicitly-requested sections. |
| 64 | if (!Config.ToRemove.empty()) { |
| 65 | RemovePred = [&Config](const Section &Sec) { |
| 66 | return Config.ToRemove.matches(S: Sec.Name); |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | if (Config.StripDebug) { |
| 71 | RemovePred = [RemovePred](const Section &Sec) { |
| 72 | return RemovePred(Sec) || isDebugSection(Sec); |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | if (Config.StripAll) { |
| 77 | RemovePred = [RemovePred](const Section &Sec) { |
| 78 | return RemovePred(Sec) || isDebugSection(Sec) || isLinkerSection(Sec) || |
| 79 | isNameSection(Sec) || isCommentSection(Sec); |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | if (Config.OnlyKeepDebug) { |
| 84 | RemovePred = [&Config](const Section &Sec) { |
| 85 | // Keep debug sections, unless explicitly requested to remove. |
| 86 | // Remove everything else, including known sections. |
| 87 | return Config.ToRemove.matches(S: Sec.Name) || !isDebugSection(Sec); |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | if (!Config.OnlySection.empty()) { |
| 92 | RemovePred = [&Config](const Section &Sec) { |
| 93 | // Explicitly keep these sections regardless of previous removes. |
| 94 | // Remove everything else, inluding known sections. |
| 95 | return !Config.OnlySection.matches(S: Sec.Name); |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | if (!Config.KeepSection.empty()) { |
| 100 | RemovePred = [&Config, RemovePred](const Section &Sec) { |
| 101 | // Explicitly keep these sections regardless of previous removes. |
| 102 | if (Config.KeepSection.matches(S: Sec.Name)) |
| 103 | return false; |
| 104 | // Otherwise defer to RemovePred. |
| 105 | return RemovePred(Sec); |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | Obj.removeSections(ToRemove: RemovePred); |
| 110 | } |
| 111 | |
| 112 | static Error handleArgs(const CommonConfig &Config, Object &Obj) { |
| 113 | // Only support AddSection, DumpSection, RemoveSection for now. |
| 114 | for (StringRef Flag : Config.DumpSection) { |
| 115 | StringRef SecName; |
| 116 | StringRef FileName; |
| 117 | std::tie(args&: SecName, args&: FileName) = Flag.split(Separator: "=" ); |
| 118 | if (Error E = |
| 119 | dumpSectionToFile(SecName, Filename: FileName, InputFilename: Config.InputFilename, Obj)) |
| 120 | return E; |
| 121 | } |
| 122 | |
| 123 | removeSections(Config, Obj); |
| 124 | |
| 125 | for (const NewSectionInfo &NewSection : Config.AddSection) { |
| 126 | Section Sec; |
| 127 | Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM; |
| 128 | Sec.Name = NewSection.SectionName; |
| 129 | |
| 130 | llvm::StringRef InputData = |
| 131 | llvm::StringRef(NewSection.SectionData->getBufferStart(), |
| 132 | NewSection.SectionData->getBufferSize()); |
| 133 | std::unique_ptr<MemoryBuffer> BufferCopy = MemoryBuffer::getMemBufferCopy( |
| 134 | InputData, BufferName: NewSection.SectionData->getBufferIdentifier()); |
| 135 | Sec.Contents = ArrayRef<uint8_t>( |
| 136 | reinterpret_cast<const uint8_t *>(BufferCopy->getBufferStart()), |
| 137 | BufferCopy->getBufferSize()); |
| 138 | |
| 139 | Obj.addSectionWithOwnedContents(NewSection: Sec, Content: std::move(BufferCopy)); |
| 140 | } |
| 141 | |
| 142 | return Error::success(); |
| 143 | } |
| 144 | |
| 145 | Error executeObjcopyOnBinary(const CommonConfig &Config, const WasmConfig &, |
| 146 | object::WasmObjectFile &In, raw_ostream &Out) { |
| 147 | Reader TheReader(In); |
| 148 | Expected<std::unique_ptr<Object>> ObjOrErr = TheReader.create(); |
| 149 | if (!ObjOrErr) |
| 150 | return createFileError(F: Config.InputFilename, E: ObjOrErr.takeError()); |
| 151 | Object *Obj = ObjOrErr->get(); |
| 152 | assert(Obj && "Unable to deserialize Wasm object" ); |
| 153 | if (Error E = handleArgs(Config, Obj&: *Obj)) |
| 154 | return E; |
| 155 | Writer TheWriter(*Obj, Out); |
| 156 | if (Error E = TheWriter.write()) |
| 157 | return createFileError(F: Config.OutputFilename, E: std::move(E)); |
| 158 | return Error::success(); |
| 159 | } |
| 160 | |
| 161 | } // end namespace wasm |
| 162 | } // end namespace objcopy |
| 163 | } // end namespace llvm |
| 164 | |