Skip to content

Commit

Permalink
[ELF][LTO] Fix non-v2 LTO build
Browse files Browse the repository at this point in the history
If a given linker LTO plugin does not support the v3 API, we resolve
all symbol names to identify the set of object files that are actually
needed for linking and then restart the linker to do the same thing
again from scratch, excluding the unneeded files from the beginning.

We do this because the v2 API does not provide a way to "unload" an
object file once it is read.

To identify an unneeded object file on the second run, we serialize
its name in the form of `<archive-name>:<offset-in-archive>` or just
a filename. If an object file is in a thin archive, we simply used
its filename.

But if the same file is directly given to a linker as well as as a
thin archive member, the file would accidentally be excluded on the
second run.

This change fixes that issue. With this change, it looks like we can
build GCC 12 with LTO using mold.

Fixes #454
  • Loading branch information
rui314 committed Apr 29, 2022
1 parent 3f54531 commit 708ad63
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions archive-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ read_thin_archive_members(C &ctx, MappedFile<C> *mf) {
std::string path = name.starts_with('/') ?
name : (filepath(mf->name).parent_path() / name).string();
vec.push_back(MappedFile<C>::must_open(ctx, path));
vec.back()->thin_parent = mf;
data = body;
}
return vec;
Expand Down
1 change: 1 addition & 0 deletions elf/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static ObjectFile<E> *new_lto_obj(Context<E> &ctx, MappedFile<Context<E>> *mf,

ObjectFile<E> *file = read_lto_object(ctx, mf);
file->priority = ctx.file_priority++;
file->archive_name = archive_name;
file->is_in_lib = ctx.in_lib || (!archive_name.empty() && !ctx.whole_archive);
file->is_alive = !file->is_in_lib;
ctx.has_lto_object = true;
Expand Down
7 changes: 7 additions & 0 deletions mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ class MappedFile {
// because archive members may have the same name.
return parent->name + ":" + std::to_string(get_offset());
}

if (thin_parent) {
// If this is a thin archive member, the filename part is
// guaranteed to be unique.
return thin_parent->name + ":" + name;
}
return name;
}

Expand All @@ -599,6 +605,7 @@ class MappedFile {
i64 mtime = 0;
bool given_fullpath = true;
MappedFile *parent = nullptr;
MappedFile *thin_parent = nullptr;
int fd = -1;
};

Expand Down

0 comments on commit 708ad63

Please sign in to comment.