Skip to content

Commit 39e024d

Browse files
authored
[lld][WebAssembly] Use the archive offset with --whole-archive (#78791)
This essentially ports 0b1413a from the ELF linker.
1 parent f9bc1ee commit 39e024d

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; Based on lld/test/ELF/lto/thin-archivecollision.ll
2+
3+
; RUN: opt -module-summary %s -o %t.o
4+
; RUN: mkdir -p %t1 %t2
5+
; RUN: opt -module-summary %p/Inputs/thin1.ll -o %t1/t.coll.o
6+
; RUN: opt -module-summary %p/Inputs/thin2.ll -o %t2/t.coll.o
7+
8+
; RUN: rm -f %t.a
9+
; RUN: llvm-ar rcs %t.a %t1/t.coll.o %t2/t.coll.o
10+
; RUN: wasm-ld %t.o %t.a -o %t
11+
; RUN: obj2yaml %t | FileCheck %s
12+
13+
; Check we handle this case correctly even in presence of --whole-archive.
14+
; RUN: wasm-ld %t.o --whole-archive %t.a -o %t
15+
; RUN: obj2yaml %t | FileCheck %s
16+
17+
; CHECK: Name: _start
18+
; CHECK: Name: foo
19+
; CHECK: Name: blah
20+
21+
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128"
22+
target triple = "wasm32-unknown-unknown"
23+
24+
define i32 @_start() #0 {
25+
entry:
26+
%call = call i32 @foo(i32 23)
27+
%call1 = call i32 @blah(i32 37)
28+
ret i32 0
29+
}
30+
31+
declare i32 @foo(i32) #1
32+
declare i32 @blah(i32) #1
33+
34+
attributes #0 = { noinline optnone }

lld/wasm/Driver.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,20 @@ static void readImportFile(StringRef filename) {
233233

234234
// Returns slices of MB by parsing MB as an archive file.
235235
// Each slice consists of a member file in the archive.
236-
std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef mb) {
236+
std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
237+
MemoryBufferRef mb) {
237238
std::unique_ptr<Archive> file =
238239
CHECK(Archive::create(mb),
239240
mb.getBufferIdentifier() + ": failed to parse archive");
240241

241-
std::vector<MemoryBufferRef> v;
242+
std::vector<std::pair<MemoryBufferRef, uint64_t>> v;
242243
Error err = Error::success();
243244
for (const Archive::Child &c : file->children(err)) {
244245
MemoryBufferRef mbref =
245246
CHECK(c.getMemoryBufferRef(),
246247
mb.getBufferIdentifier() +
247248
": could not get the buffer for a child of the archive");
248-
v.push_back(mbref);
249+
v.push_back(std::make_pair(mbref, c.getChildOffset()));
249250
}
250251
if (err)
251252
fatal(mb.getBufferIdentifier() +
@@ -273,8 +274,8 @@ void LinkerDriver::addFile(StringRef path) {
273274

274275
// Handle -whole-archive.
275276
if (inWholeArchive) {
276-
for (MemoryBufferRef &m : getArchiveMembers(mbref)) {
277-
auto *object = createObjectFile(m, path);
277+
for (const auto &[m, offset] : getArchiveMembers(mbref)) {
278+
auto *object = createObjectFile(m, path, offset);
278279
// Mark object as live; object members are normally not
279280
// live by default but -whole-archive is designed to treat
280281
// them as such.

0 commit comments

Comments
 (0)