Skip to content

Commit 069c52f

Browse files
committed
Check if the archive has already been added to avoid duplicates
This avoids adding archives multiple times, which results in duplicate objects in the resulting rlib, leading to symbol collision and link failures. This could happen when crate contains multiple link attributes that all reference the same archive.
1 parent 17e62f7 commit 069c52f

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/librustc_codegen_llvm/back/archive.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,20 @@ enum Addition {
3636
name_in_archive: String,
3737
},
3838
Archive {
39+
path: PathBuf,
3940
archive: ArchiveRO,
4041
skip: Box<dyn FnMut(&str) -> bool>,
4142
},
4243
}
4344

45+
impl Addition {
46+
fn path(&self) -> &Path {
47+
match self {
48+
Addition::File { path, .. } | Addition::Archive { path, .. } => path,
49+
}
50+
}
51+
}
52+
4453
fn is_relevant_child(c: &Child<'_>) -> bool {
4554
match c.name() {
4655
Some(name) => !name.contains("SYMDEF"),
@@ -188,12 +197,16 @@ impl<'a> LlvmArchiveBuilder<'a> {
188197
-> io::Result<()>
189198
where F: FnMut(&str) -> bool + 'static
190199
{
191-
let archive = match ArchiveRO::open(archive) {
200+
let archive_ro = match ArchiveRO::open(archive) {
192201
Ok(ar) => ar,
193202
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
194203
};
204+
if self.additions.iter().any(|ar| ar.path() == archive) {
205+
return Ok(())
206+
}
195207
self.additions.push(Addition::Archive {
196-
archive,
208+
path: archive.to_path_buf(),
209+
archive: archive_ro,
197210
skip: Box::new(skip),
198211
});
199212
Ok(())
@@ -243,7 +256,7 @@ impl<'a> LlvmArchiveBuilder<'a> {
243256
strings.push(path);
244257
strings.push(name);
245258
}
246-
Addition::Archive { archive, skip } => {
259+
Addition::Archive { archive, skip, .. } => {
247260
for child in archive.iter() {
248261
let child = child.map_err(string_to_io_error)?;
249262
if !is_relevant_child(&child) {

0 commit comments

Comments
 (0)