Skip to content

Commit a928d12

Browse files
committed
[llvm-objcopy] Initial support for wasm in llvm-objcopy
Currently only supports simple copying, other operations to follow. Reviewers: sbc100, alexshap, jhenderson Differential Revision: https://reviews.llvm.org/D70930
1 parent 954d042 commit a928d12

File tree

12 files changed

+489
-1
lines changed

12 files changed

+489
-1
lines changed

llvm/include/llvm/Object/Wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class WasmObjectFile : public ObjectFile {
152152
uint32_t getNumImportedGlobals() const { return NumImportedGlobals; }
153153
uint32_t getNumImportedFunctions() const { return NumImportedFunctions; }
154154
uint32_t getNumImportedEvents() const { return NumImportedEvents; }
155+
uint32_t getNumSections() const { return Sections.size(); }
155156
void moveSymbolNext(DataRefImpl &Symb) const override;
156157

157158
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## Test a basic copy of an archive containing a wasm object.
2+
3+
# RUN: yaml2obj %s -o %t
4+
5+
## Create an archive and copy it using llvm-objcopy.
6+
# RUN: rm -f %t.a
7+
# RUN: llvm-ar crs %t.a %t
8+
# RUN: cp %t.a %t.copy.a
9+
# RUN: llvm-objcopy %t.a %t2.a
10+
## Create another archive from an objcopy-copied object, verify that they match.
11+
# RUN: llvm-objcopy %t %t2
12+
# RUN: llvm-ar p %t2.a > %t3
13+
# RUN: cmp %t2 %t3
14+
15+
## Check that the copied archive has the correct index contents.
16+
# RUN: llvm-nm --print-armap %t.a | FileCheck --check-prefix=INDEX-TABLE %s
17+
# RUN: llvm-nm --print-armap %t2.a | FileCheck --check-prefix=INDEX-TABLE %s
18+
## Verify that llvm-objcopy has not modifed the input.
19+
# RUN: cmp %t.copy.a %t.a
20+
21+
# INDEX-TABLE: Archive map
22+
# INDEX-TABLE-NEXT: func1 in
23+
24+
## Do the same with an archive that has no index.
25+
# RUN: rm -f %t.no.index.a
26+
# RUN: llvm-ar crS %t.no.index.a %t
27+
# RUN: llvm-objcopy %t.no.index.a %t2.no.index.a
28+
# RUN: llvm-ar p %t2.no.index.a > %t4
29+
30+
# RUN: llvm-nm --print-armap %t.no.index.a | FileCheck --check-prefix=NO-INDEX-TABLE %s
31+
# RUN: llvm-nm --print-armap %t2.no.index.a | FileCheck --check-prefix=NO-INDEX-TABLE %s
32+
# RUN: cmp %t2 %t4
33+
34+
# NO-INDEX-TABLE-NOT: Archive map
35+
# NO-INDEX-TABLE-NOT: func1 in
36+
37+
--- !WASM
38+
FileHeader:
39+
Version: 0x00000001
40+
Sections:
41+
- Type: TYPE
42+
Signatures:
43+
- Index: 0
44+
ParamTypes:
45+
- I32
46+
ReturnTypes:
47+
- F32
48+
- Index: 1
49+
ParamTypes:
50+
- I32
51+
- I64
52+
ReturnTypes: []
53+
- Type: FUNCTION
54+
FunctionTypes:
55+
- 0
56+
- 1
57+
- Type: CODE
58+
Relocations:
59+
- Type: R_WASM_TABLE_INDEX_SLEB
60+
Index: 0
61+
Offset: 0x00000002
62+
- Type: R_WASM_FUNCTION_INDEX_LEB
63+
Index: 1
64+
Offset: 0x0000002
65+
Functions:
66+
- Index: 0
67+
Locals:
68+
- Type: I32
69+
Count: 3
70+
Body: 010101010B
71+
- Index: 1
72+
Locals:
73+
- Type: I32
74+
Count: 1
75+
Body: 010101010B
76+
- Type: CUSTOM
77+
Name: linking
78+
Version: 2
79+
SymbolTable:
80+
- Index: 0
81+
Kind: FUNCTION
82+
Name: func1
83+
Flags: [ ]
84+
Function: 0
85+
- Index: 1
86+
Kind: FUNCTION
87+
Name: func2
88+
Flags: [ ]
89+
Function: 1
90+
...
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Test that the copied object has the same yaml conversion as the original.
2+
## The copied object is not bit-identical to the yaml2obj-generated
3+
## one, as yaml2obj uses 5-byte LEBs for section sizes (unlike objcopy/clang).
4+
# RUN: yaml2obj %s -o %t.o
5+
# RUN: llvm-objcopy %t.o %t2.o
6+
# RUN: obj2yaml %t.o > %t.yaml
7+
# RUN: obj2yaml %t2.o > %t2.yaml
8+
# RUN: diff %t.yaml %t2.yaml
9+
10+
--- !WASM
11+
FileHeader:
12+
Version: 0x00000001
13+
Sections:
14+
- Type: TYPE
15+
Signatures:
16+
- Index: 0
17+
ParamTypes:
18+
- I32
19+
ReturnTypes:
20+
- F32
21+
- Index: 1
22+
ParamTypes:
23+
- I32
24+
- I64
25+
ReturnTypes: []
26+
- Type: FUNCTION
27+
FunctionTypes:
28+
- 0
29+
- 1
30+
- Type: CODE
31+
Relocations:
32+
- Type: R_WASM_TABLE_INDEX_SLEB
33+
Index: 0
34+
Offset: 0x00000002
35+
- Type: R_WASM_FUNCTION_INDEX_LEB
36+
Index: 1
37+
Offset: 0x0000002
38+
Functions:
39+
- Index: 0
40+
Locals:
41+
- Type: I32
42+
Count: 3
43+
Body: 010101010B
44+
- Index: 1
45+
Locals:
46+
- Type: I32
47+
Count: 1
48+
Body: 010101010B
49+
- Type: CUSTOM
50+
Name: linking
51+
Version: 2
52+
SymbolTable:
53+
- Index: 0
54+
Kind: FUNCTION
55+
Name: func1
56+
Flags: [ ]
57+
Function: 0
58+
- Index: 1
59+
Kind: FUNCTION
60+
Name: func2
61+
Flags: [ ]
62+
Function: 1
63+
...

llvm/tools/llvm-objcopy/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ add_llvm_tool(llvm-objcopy
3333
MachO/MachOWriter.cpp
3434
MachO/MachOLayoutBuilder.cpp
3535
MachO/Object.cpp
36+
wasm/Reader.cpp
37+
wasm/Writer.cpp
38+
wasm/WasmObjcopy.cpp
3639
DEPENDS
3740
ObjcopyOptsTableGen
3841
InstallNameToolOptsTableGen

llvm/tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
#include "llvm-objcopy.h"
1010
#include "Buffer.h"
11+
#include "COFF/COFFObjcopy.h"
1112
#include "CopyConfig.h"
1213
#include "ELF/ELFObjcopy.h"
13-
#include "COFF/COFFObjcopy.h"
1414
#include "MachO/MachOObjcopy.h"
15+
#include "wasm/WasmObjcopy.h"
1516

1617
#include "llvm/ADT/STLExtras.h"
1718
#include "llvm/ADT/SmallVector.h"
@@ -25,6 +26,7 @@
2526
#include "llvm/Object/ELFTypes.h"
2627
#include "llvm/Object/Error.h"
2728
#include "llvm/Object/MachO.h"
29+
#include "llvm/Object/Wasm.h"
2830
#include "llvm/Option/Arg.h"
2931
#include "llvm/Option/ArgList.h"
3032
#include "llvm/Option/Option.h"
@@ -172,6 +174,8 @@ static Error executeObjcopyOnBinary(CopyConfig &Config, object::Binary &In,
172174
return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out);
173175
else if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In))
174176
return macho::executeObjcopyOnBinary(Config, *MachOBinary, Out);
177+
else if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In))
178+
return objcopy::wasm::executeObjcopyOnBinary(Config, *WasmBinary, Out);
175179
else
176180
return createStringError(object_error::invalid_file_type,
177181
"unsupported object file format");

llvm/tools/llvm-objcopy/wasm/Object.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===- Object.h -------------------------------------------------*- C++ -*-===//
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+
#ifndef LLVM_TOOLS_LLVM_OBJCOPY_WASM_OBJECT_H
10+
#define LLVM_TOOLS_LLVM_OBJCOPY_WASM_OBJECT_H
11+
12+
#include "llvm/ADT/ArrayRef.h"
13+
#include "llvm/ADT/StringRef.h"
14+
#include "llvm/Object/Wasm.h"
15+
#include <vector>
16+
17+
namespace llvm {
18+
namespace objcopy {
19+
namespace wasm {
20+
21+
struct Section {
22+
// For now, each section is only an opaque binary blob with no distinction
23+
// between custom and known sections.
24+
uint8_t SectionType;
25+
StringRef Name;
26+
ArrayRef<uint8_t> Contents;
27+
};
28+
29+
struct Object {
30+
llvm::wasm::WasmObjectHeader Header;
31+
// For now don't discriminate between kinds of sections.
32+
std::vector<Section> Sections;
33+
};
34+
35+
} // end namespace wasm
36+
} // end namespace objcopy
37+
} // end namespace llvm
38+
39+
#endif // LLVM_TOOLS_LLVM_OBJCOPY_WASM_OBJECT_H
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===- Reader.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 "Reader.h"
10+
11+
namespace llvm {
12+
namespace objcopy {
13+
namespace wasm {
14+
15+
using namespace object;
16+
using namespace llvm::wasm;
17+
18+
Expected<std::unique_ptr<Object>> Reader::create() const {
19+
auto Obj = std::make_unique<Object>();
20+
Obj->Header = WasmObj.getHeader();
21+
std::vector<Section> Sections;
22+
Obj->Sections.reserve(WasmObj.getNumSections());
23+
for (const SectionRef &Sec : WasmObj.sections()) {
24+
const WasmSection &WS = WasmObj.getWasmSection(Sec);
25+
Obj->Sections.push_back(
26+
{static_cast<uint8_t>(WS.Type), WS.Name, WS.Content});
27+
}
28+
return std::move(Obj);
29+
}
30+
31+
} // end namespace wasm
32+
} // end namespace objcopy
33+
} // end namespace llvm

llvm/tools/llvm-objcopy/wasm/Reader.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- Reader.h -------------------------------------------------*- C++ -*-===//
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+
#ifndef LLVM_TOOLS_LLVM_OBJCOPY_WASM_READER_H
10+
#define LLVM_TOOLS_LLVM_OBJCOPY_WASM_READER_H
11+
12+
#include "Object.h"
13+
14+
namespace llvm {
15+
namespace objcopy {
16+
namespace wasm {
17+
18+
class Reader {
19+
public:
20+
explicit Reader(const object::WasmObjectFile &O) : WasmObj(O) {}
21+
Expected<std::unique_ptr<Object>> create() const;
22+
23+
private:
24+
const object::WasmObjectFile &WasmObj;
25+
};
26+
27+
} // end namespace wasm
28+
} // end namespace objcopy
29+
} // end namespace llvm
30+
31+
#endif // LLVM_TOOLS_LLVM_OBJCOPY_WASM_READER_H
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
24+
static 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+
47+
Error 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

0 commit comments

Comments
 (0)