diff --git a/llvm/test/tools/llvm-objcopy/Wasm/add-section.test b/llvm/test/tools/llvm-objcopy/Wasm/add-section.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/Wasm/add-section.test @@ -0,0 +1,73 @@ +# This actually tests dumping, removal, and addition +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --dump-section=producers=%t.sec %t +# RUN: llvm-objcopy -R producers %t %t2 +# RUN: llvm-objcopy --add-section=producers=%t.sec %t2 %t3 +# RUN: obj2yaml %t3 | FileCheck %s + +--- !WASM +FileHeader: + Version: 0x00000001 +Sections: + - Type: TYPE + Signatures: + - Index: 0 + ParamTypes: + - I32 + ReturnTypes: + - F32 + - Index: 1 + ParamTypes: + - I32 + - I64 + ReturnTypes: [] + - Type: FUNCTION + FunctionTypes: + - 0 + - 1 + - Type: CODE + Relocations: + - Type: R_WASM_TABLE_INDEX_SLEB + Index: 0 + Offset: 0x00000006 + - Type: R_WASM_FUNCTION_INDEX_LEB + Index: 1 + Offset: 0x00000025 + Functions: + - Index: 0 + Locals: + - Type: I32 + Count: 3 + Body: 418080808000210020002101200111808080800000210220020F0B + - Index: 1 + Locals: + - Type: I32 + Count: 1 + Body: 108180808000210020000F0B + - Type: CUSTOM + Name: linking + Version: 2 + SymbolTable: + - Index: 0 + Kind: FUNCTION + Name: func1 + Flags: [ ] + Function: 0 + - Index: 1 + Kind: FUNCTION + Name: func2 + Flags: [ ] + Function: 1 + - Type: CUSTOM + Name: producers + Tools: + - Name: clang + Version: 9.0.0 + +... + +# Check that the producers section has been added back unchanged. +CHECK: Name: producers +CHECK-NEXT: Tools: +CHECK-NEXT: - Name: clang +CHECK-NEXT: Version: 9.0.0 \ No newline at end of file diff --git a/llvm/test/tools/llvm-objcopy/Wasm/basic-archive-copy.test b/llvm/test/tools/llvm-objcopy/Wasm/basic-archive-copy.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/Wasm/basic-archive-copy.test @@ -0,0 +1,117 @@ +# RUN: yaml2obj %s > %t + +# RUN: rm -f %t.a +# RUN: llvm-ar crs %t.a %t +# RUN: cp %t.a %t.copy.a +# RUN: llvm-objcopy %t.a %t2.a +# RUN: llvm-objcopy %t %t2 +# RUN: llvm-ar p %t2.a > %t3 +# RUN: cmp %t2 %t3 + + +# RUN: llvm-readobj --sections %t2 | FileCheck %s +# RUN: llvm-nm --print-armap %t.a | FileCheck --check-prefix=INDEX-TABLE %s +# RUN: llvm-nm --print-armap %t2.a | FileCheck --check-prefix=INDEX-TABLE %s +# Verify that llvm-objcopy has not modifed the input. +# RUN: cmp %t.copy.a %t.a + +# INDEX-TABLE: Archive map +# INDEX-TABLE-NEXT: func1 in + +# RUN: rm -f %t.no.index.a +# RUN: llvm-ar crS %t.no.index.a %t +# RUN: llvm-objcopy %t.no.index.a %t2.no.index.a +# RUN: llvm-ar p %t2.no.index.a > %t4 + +# RUN: llvm-nm --print-armap %t.no.index.a | FileCheck --check-prefix=NO-INDEX-TABLE %s +# RUN: llvm-nm --print-armap %t2.no.index.a | FileCheck --check-prefix=NO-INDEX-TABLE %s +# RUN: cmp %t2 %t4 + +# NO-INDEX-TABLE-NOT: Archive map +# NO-INDEX-TABLE-NOT: func1 in + +--- !WASM +FileHeader: + Version: 0x00000001 +Sections: + - Type: TYPE + Signatures: + - Index: 0 + ParamTypes: + - I32 + ReturnTypes: + - F32 + - Index: 1 + ParamTypes: + - I32 + - I64 + ReturnTypes: [] + - Type: FUNCTION + FunctionTypes: + - 0 + - 1 + - Type: CODE + Relocations: + - Type: R_WASM_TABLE_INDEX_SLEB + Index: 0 + Offset: 0x00000006 + - Type: R_WASM_FUNCTION_INDEX_LEB + Index: 1 + Offset: 0x00000025 + Functions: + - Index: 0 + Locals: + - Type: I32 + Count: 3 + Body: 418080808000210020002101200111808080800000210220020F0B + - Index: 1 + Locals: + - Type: I32 + Count: 1 + Body: 108180808000210020000F0B + - Type: CUSTOM + Name: linking + Version: 2 + SymbolTable: + - Index: 0 + Kind: FUNCTION + Name: func1 + Flags: [ ] + Function: 0 + - Index: 1 + Kind: FUNCTION + Name: func2 + Flags: [ ] + Function: 1 +... + + +CHECK: Arch: wasm32 +CHECK: Sections [ +CHECK-NEXT: Section { +CHECK-NEXT: Type: TYPE (0x1) +CHECK-NEXT: Size: 11 +CHECK-NEXT: Offset: 8 +CHECK-NEXT: } +CHECK-NEXT: Section { +CHECK-NEXT: Type: FUNCTION (0x3) +CHECK-NEXT: Size: 3 +CHECK-NEXT: Offset: 25 +CHECK-NEXT: } +CHECK-NEXT: Section { +CHECK-NEXT: Type: CODE (0xA) +CHECK-NEXT: Size: 48 +CHECK-NEXT: Offset: 34 +CHECK-NEXT: } +CHECK-NEXT: Section { +CHECK-NEXT: Type: CUSTOM (0x0) +CHECK-NEXT: Size: 22 +CHECK-NEXT: Offset: 88 +CHECK-NEXT: Name: linking +CHECK-NEXT: } +CHECK-NEXT: Section { +CHECK-NEXT: Type: CUSTOM (0x0) +CHECK-NEXT: Size: 8 +CHECK-NEXT: Offset: 124 +CHECK-NEXT: Name: reloc.CODE +CHECK-NEXT: } diff --git a/llvm/test/tools/llvm-objcopy/Wasm/basic-copy.test b/llvm/test/tools/llvm-objcopy/Wasm/basic-copy.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/Wasm/basic-copy.test @@ -0,0 +1,65 @@ +# Test that the copied object has the same yaml conversion as the original. +# RUN: yaml2obj %s > %t.o +# RUN: llvm-objcopy %t.o %t2.o +# RUN: obj2yaml %t.o > %t.yaml +# RUN: obj2yaml %t2.o > %t2.yaml +# RUN: diff %t.yaml %t2.yaml +# Currently the copied object is not bit-identical to the yaml2obj-generated +# one, as yaml2obj uses 5-byte LEBs for section sizes (unlike objcopy/clang). +# NORUN : cmp %t.o %t2.o + + +--- !WASM +FileHeader: + Version: 0x00000001 +Sections: + - Type: TYPE + Signatures: + - Index: 0 + ParamTypes: + - I32 + ReturnTypes: + - F32 + - Index: 1 + ParamTypes: + - I32 + - I64 + ReturnTypes: [] + - Type: FUNCTION + FunctionTypes: + - 0 + - 1 + - Type: CODE + Relocations: + - Type: R_WASM_TABLE_INDEX_SLEB + Index: 0 + Offset: 0x00000006 + - Type: R_WASM_FUNCTION_INDEX_LEB + Index: 1 + Offset: 0x00000025 + Functions: + - Index: 0 + Locals: + - Type: I32 + Count: 3 + Body: 418080808000210020002101200111808080800000210220020F0B + - Index: 1 + Locals: + - Type: I32 + Count: 1 + Body: 108180808000210020000F0B + - Type: CUSTOM + Name: linking + Version: 2 + SymbolTable: + - Index: 0 + Kind: FUNCTION + Name: func1 + Flags: [ ] + Function: 0 + - Index: 1 + Kind: FUNCTION + Name: func2 + Flags: [ ] + Function: 1 +... diff --git a/llvm/tools/llvm-objcopy/CMakeLists.txt b/llvm/tools/llvm-objcopy/CMakeLists.txt --- a/llvm/tools/llvm-objcopy/CMakeLists.txt +++ b/llvm/tools/llvm-objcopy/CMakeLists.txt @@ -33,6 +33,8 @@ MachO/MachOWriter.cpp MachO/MachOLayoutBuilder.cpp MachO/Object.cpp + Wasm/Object.cpp + Wasm/WasmObjcopy.cpp DEPENDS ObjcopyOptsTableGen InstallNameToolOptsTableGen diff --git a/llvm/tools/llvm-objcopy/Wasm/Object.h b/llvm/tools/llvm-objcopy/Wasm/Object.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-objcopy/Wasm/Object.h @@ -0,0 +1,98 @@ +//===- Object.h -------------------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_OBJCOPY_WASM_OBJECT_H +#define LLVM_TOOLS_OBJCOPY_WASM_OBJECT_H + +#include "Buffer.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/iterator_range.h" +#include "llvm/BinaryFormat/Wasm.h" +#include "llvm/Object/Wasm.h" +#include +#include + +namespace llvm { +namespace objcopy { +namespace wasm { + +class Section { +public: + uint8_t SectionType; + StringRef Name; + + ArrayRef getContents() const { + if (!OwnedContents.empty()) + return OwnedContents; + return ContentsRef; + } + + void setContentsRef(ArrayRef Data) { + OwnedContents.clear(); + ContentsRef = Data; + } + + void setOwnedContents(std::vector &&Data) { + ContentsRef = ArrayRef(); + OwnedContents = std::move(Data); + } + + void clearContents() { + ContentsRef = ArrayRef(); + OwnedContents.clear(); + } + +private: + // For now, only an opaque binary blob. + ArrayRef ContentsRef; + std::vector OwnedContents; +}; + +class Object { +public: + ::llvm::wasm::WasmObjectHeader Header; + // TODO: Support synbols, relocations, debug info, etc + // Stuff for sections + ArrayRef
getSections() { return Sections; }; + void addSections(ArrayRef
NewSections); + void removeSections(function_ref ToRemove); + +private: + // Consists of magic, version, and sections. For now don't discriminate + // between kinds of sections. + std::vector
Sections; +}; + +class Reader { +public: + explicit Reader(const object::WasmObjectFile &O) : WasmObj(O) {} + Expected> create() const; + +private: + const object::WasmObjectFile &WasmObj; +}; + +class Writer { +public: + Writer(Object &Obj, Buffer &Buf) : Obj(Obj), Buf(Buf) {} + Error write(); + +private: + Object &Obj; + Buffer &Buf; + std::vector> SectionHeaders; + + size_t finalize(); +}; +} // end namespace wasm +} // end namespace objcopy +} // end namespace llvm + +#endif // LLVM_TOOLS_OBJCOPY_WASM_OBJECT_H diff --git a/llvm/tools/llvm-objcopy/Wasm/Object.cpp b/llvm/tools/llvm-objcopy/Wasm/Object.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-objcopy/Wasm/Object.cpp @@ -0,0 +1,91 @@ +//===- Object.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 "Object.h" + +#include "llvm/Support/LEB128.h" +#include "llvm/Support/raw_ostream.h" + +namespace llvm { +namespace objcopy { +namespace wasm { + +using namespace object; +void Object::addSections(ArrayRef
NewSections) { + for (const Section &S : NewSections) { + Sections.emplace_back(S); + } +} + +void Object::removeSections(function_ref ToRemove) { + // TODO: remove reloc sections for the removed section, handle symbols, etc + Sections.erase( + std::remove_if(std::begin(Sections), std::end(Sections), ToRemove), + std::end(Sections)); +} + +Expected> Reader::create() const { + auto Obj = std::make_unique(); + Obj->Header = WasmObj.getHeader(); + std::vector
Sections; + for (const SectionRef &Sec : WasmObj.sections()) { + const WasmSection &WS = WasmObj.getWasmSection(Sec); + Sections.emplace_back(Section()); + Section &OS = Sections.back(); + OS.SectionType = WS.Type; + OS.Name = WS.Name; + OS.setContentsRef(WS.Content); + } + Obj->addSections(Sections); + return std::move(Obj); +} + +size_t Writer::finalize() { + size_t ObjectSize = 8; // Size of wasm header + SectionHeaders.reserve(Obj.getSections().size()); + // Finalize the headers of each section so we know the total size. + for (const auto &S : Obj.getSections()) { + SectionHeaders.emplace_back(); + auto &Header = SectionHeaders.back(); + raw_svector_ostream OS(Header); + encodeULEB128(S.SectionType, OS); + bool HasName = S.SectionType == 0; + size_t SectionSize = S.getContents().size(); + if (HasName) + SectionSize += getULEB128Size(S.Name.size()) + S.Name.size(); + encodeULEB128(SectionSize, OS, 5); + if (HasName) { + encodeULEB128(S.Name.size(), OS); + OS << S.Name; + } + ObjectSize += SectionSize + 1 + 5; + } + return ObjectSize; +} + +Error Writer::write() { + size_t FileSize = finalize(); + if (Error E = Buf.allocate(FileSize)) + return E; + // write header + uint8_t *Ptr = Buf.getBufferStart(); + Ptr = std::copy(Obj.Header.Magic.begin(), Obj.Header.Magic.end(), Ptr); + memcpy(Ptr, &Obj.Header.Version, sizeof(Obj.Header.Version)); + Ptr += sizeof(Obj.Header.Version); + // write sections + for (size_t i = 0; i < SectionHeaders.size(); ++i) { + Ptr = std::copy(SectionHeaders[i].begin(), SectionHeaders[i].end(), Ptr); + const auto &Contents = Obj.getSections()[i].getContents(); + Ptr = std::copy(Contents.begin(), Contents.end(), Ptr); + } + return Buf.commit(); +} + +} // end namespace wasm +} // end namespace objcopy +} // end namespace llvm diff --git a/llvm/tools/llvm-objcopy/Wasm/WasmObjcopy.h b/llvm/tools/llvm-objcopy/Wasm/WasmObjcopy.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-objcopy/Wasm/WasmObjcopy.h @@ -0,0 +1,31 @@ +//===- WasmObjcopy.h -------------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_OBJCOPY_WASMOBJCOPY_H +#define LLVM_TOOLS_OBJCOPY_WASMOBJCOPY_H + +namespace llvm { +class Error; + +namespace object { +class WasmObjectFile; +} // end namespace object + +namespace objcopy { +struct CopyConfig; +class Buffer; + +namespace wasm { +Error executeObjcopyOnBinary(const CopyConfig &Config, + object::WasmObjectFile &In, Buffer &Out); + +} // end namespace wasm +} // end namespace objcopy +} // end namespace llvm + +#endif // LLVM_TOOLS_OBJCOPY_WASMOBJCOPY_H diff --git a/llvm/tools/llvm-objcopy/Wasm/WasmObjcopy.cpp b/llvm/tools/llvm-objcopy/Wasm/WasmObjcopy.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-objcopy/Wasm/WasmObjcopy.cpp @@ -0,0 +1,125 @@ +//===- 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 "Buffer.h" +#include "CopyConfig.h" +#include "Object.h" +#include "llvm-objcopy.h" +#include "llvm/Support/Errc.h" + +namespace llvm { +namespace objcopy { +namespace wasm { + +using namespace object; +using namespace wasm; + +// Adds named custom section with given contents to the object. +static void addSection(Object &Obj, StringRef Name, + ArrayRef Contents) { + Section Sec; + Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM; + Sec.Name = Name; + Sec.setOwnedContents(Contents); + Obj.addSections(Sec); +} + +static Error dumpSectionToFile(StringRef SecName, StringRef Filename, + Object &Obj) { + for (auto &Sec : Obj.getSections()) { + if (Sec.Name == SecName) { + ArrayRef Contents = Sec.getContents(); + if (Contents.empty()) + return createStringError(object_error::parse_failed, + "cannot dump section '%s': it has no contents", + SecName.str().c_str()); + Expected> BufferOrErr = + FileOutputBuffer::create(Filename, Contents.size()); + if (!BufferOrErr) + return BufferOrErr.takeError(); + std::unique_ptr Buf = std::move(*BufferOrErr); + std::copy(Contents.begin(), Contents.end(), Buf->getBufferStart()); + if (Error E = Buf->commit()) + return E; + return Error::success(); + } + } + return createStringError(object_error::parse_failed, "section '%s' not found", + SecName.str().c_str()); +} + +static Error handleArgs(const CopyConfig &Config, Object &Obj) { + // Only support AddSection, DumpSection, RemoveSection. + // Don't support OnlySection yet because we only operate on custom sections. + Obj.removeSections([&Config](const Section &Sec) { + if (Config.ToRemove.matches(Sec.Name)) + return true; + return false; + }); + + for (const auto &Flag : Config.AddSection) { + StringRef SecName, FileName; + std::tie(SecName, FileName) = Flag.split("="); + auto BufOrErr = MemoryBuffer::getFile(FileName); + if (!BufOrErr) + return createFileError(FileName, errorCodeToError(BufOrErr.getError())); + auto Buf = std::move(*BufOrErr); + addSection( + Obj, SecName, + makeArrayRef(reinterpret_cast(Buf->getBufferStart()), + Buf->getBufferSize())); + } + + for (const auto &Flag : Config.DumpSection) { + StringRef SecName; + StringRef FileName; + std::tie(SecName, FileName) = Flag.split("="); + if (Error E = dumpSectionToFile(SecName, FileName, Obj)) + return E; + } + + if (!Config.AddGnuDebugLink.empty() || !Config.BuildIdLinkDir.empty() || + Config.BuildIdLinkInput || Config.BuildIdLinkOutput || + 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, Buffer &Out) { + Reader TheReader(In); + Expected> ObjOrErr = TheReader.create(); + if (!ObjOrErr) + return createFileError(Config.InputFilename, ObjOrErr.takeError()); + Object *Obj = ObjOrErr->get(); + assert(Obj && "Unable to deserialize COFF object"); + if (Error E = handleArgs(Config, *Obj)) + return createFileError(Config.InputFilename, std::move(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 diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -8,10 +8,11 @@ #include "llvm-objcopy.h" #include "Buffer.h" +#include "COFF/COFFObjcopy.h" #include "CopyConfig.h" #include "ELF/ELFObjcopy.h" -#include "COFF/COFFObjcopy.h" #include "MachO/MachOObjcopy.h" +#include "Wasm/WasmObjcopy.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" @@ -25,6 +26,7 @@ #include "llvm/Object/ELFTypes.h" #include "llvm/Object/Error.h" #include "llvm/Object/MachO.h" +#include "llvm/Object/Wasm.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" @@ -172,6 +174,8 @@ return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out); else if (auto *MachOBinary = dyn_cast(&In)) return macho::executeObjcopyOnBinary(Config, *MachOBinary, Out); + else if (auto *WasmBinary = dyn_cast(&In)) + return objcopy::wasm::executeObjcopyOnBinary(Config, *WasmBinary, Out); else return createStringError(object_error::invalid_file_type, "unsupported object file format");