2021-05-20 00:58:17 +08:00
|
|
|
//===- ICF.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 "ICF.h"
|
|
|
|
#include "ConcatOutputSection.h"
|
2022-05-04 09:00:41 +08:00
|
|
|
#include "Config.h"
|
2021-05-20 00:58:17 +08:00
|
|
|
#include "InputSection.h"
|
2022-05-04 09:00:41 +08:00
|
|
|
#include "SymbolTable.h"
|
2021-05-20 00:58:17 +08:00
|
|
|
#include "Symbols.h"
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
#include "UnwindInfoSection.h"
|
|
|
|
|
2022-02-17 09:03:57 +08:00
|
|
|
#include "lld/Common/CommonLinkerContext.h"
|
2022-05-04 09:00:41 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2021-05-20 00:58:17 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2022-02-23 21:57:52 +08:00
|
|
|
#include "llvm/Support/xxhash.h"
|
2021-05-20 00:58:17 +08:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::macho;
|
|
|
|
|
2022-03-08 01:35:15 +08:00
|
|
|
static constexpr bool verboseDiagnostics = false;
|
|
|
|
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
class ICF {
|
|
|
|
public:
|
|
|
|
ICF(std::vector<ConcatInputSection *> &inputs);
|
|
|
|
void run();
|
2022-03-08 01:35:12 +08:00
|
|
|
|
|
|
|
using EqualsFn = bool (ICF::*)(const ConcatInputSection *,
|
|
|
|
const ConcatInputSection *);
|
|
|
|
void segregate(size_t begin, size_t end, EqualsFn);
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
size_t findBoundary(size_t begin, size_t end);
|
|
|
|
void forEachClassRange(size_t begin, size_t end,
|
2022-03-05 09:22:40 +08:00
|
|
|
llvm::function_ref<void(size_t, size_t)> func);
|
|
|
|
void forEachClass(llvm::function_ref<void(size_t, size_t)> func);
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
|
2022-03-08 01:35:12 +08:00
|
|
|
bool equalsConstant(const ConcatInputSection *ia,
|
|
|
|
const ConcatInputSection *ib);
|
|
|
|
bool equalsVariable(const ConcatInputSection *ia,
|
|
|
|
const ConcatInputSection *ib);
|
|
|
|
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
// ICF needs a copy of the inputs vector because its equivalence-class
|
|
|
|
// segregation algorithm destroys the proper sequence.
|
|
|
|
std::vector<ConcatInputSection *> icfInputs;
|
2022-03-08 01:35:12 +08:00
|
|
|
|
|
|
|
unsigned icfPass = 0;
|
|
|
|
std::atomic<bool> icfRepeat{false};
|
2022-03-08 01:35:15 +08:00
|
|
|
std::atomic<uint64_t> equalsConstantCount{0};
|
|
|
|
std::atomic<uint64_t> equalsVariableCount{0};
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
};
|
|
|
|
|
2021-05-20 00:58:17 +08:00
|
|
|
ICF::ICF(std::vector<ConcatInputSection *> &inputs) {
|
|
|
|
icfInputs.assign(inputs.begin(), inputs.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ICF = Identical Code Folding
|
|
|
|
//
|
|
|
|
// We only fold __TEXT,__text, so this is really "code" folding, and not
|
|
|
|
// "COMDAT" folding. String and scalar constant literals are deduplicated
|
|
|
|
// elsewhere.
|
|
|
|
//
|
|
|
|
// Summary of segments & sections:
|
|
|
|
//
|
|
|
|
// The __TEXT segment is readonly at the MMU. Some sections are already
|
|
|
|
// deduplicated elsewhere (__TEXT,__cstring & __TEXT,__literal*) and some are
|
|
|
|
// synthetic and inherently free of duplicates (__TEXT,__stubs &
|
[lld-macho] Have ICF operate on all sections at once
ICF previously operated only within a given OutputSection. We would
merge all CFStrings first, then merge all regular code sections in a
second phase. This worked fine since CFStrings would never reference
regular `__text` sections. However, I would like to expand ICF to merge
functions that reference unwind info. Unwind info references the LSDA
section, which can in turn reference the `__text` section, so we cannot
perform ICF in phases.
In order to have ICF operate on InputSections spanning multiple
OutputSections, we need a way to distinguish InputSections that are
destined for different OutputSections, so that we don't fold across
section boundaries. We achieve this by creating OutputSections early,
and setting `InputSection::parent` to point to them. This is what
LLD-ELF does. (This change should also make it easier to implement the
`section$start$` symbols.)
This diff also folds InputSections w/o checking their flags, which I
think is the right behavior -- if they are destined for the same
OutputSection, they will have the same flags in the output (even if
their input flags differ). I.e. the `parent` pointer check subsumes the
`flags` check. In practice this has nearly no effect (ICF did not become
any more effective on chromium_framework).
I've also updated ICF.cpp's block comment to better reflect its current
status.
Reviewed By: #lld-macho, smeenai
Differential Revision: https://reviews.llvm.org/D105641
2021-07-18 01:42:26 +08:00
|
|
|
// __TEXT,__unwind_info). Note that we don't yet run ICF on __TEXT,__const,
|
|
|
|
// because doing so induces many test failures.
|
2021-05-20 00:58:17 +08:00
|
|
|
//
|
|
|
|
// The __LINKEDIT segment is readonly at the MMU, yet entirely synthetic, and
|
|
|
|
// thus ineligible for ICF.
|
|
|
|
//
|
|
|
|
// The __DATA_CONST segment is read/write at the MMU, but is logically const to
|
[lld-macho] Have ICF operate on all sections at once
ICF previously operated only within a given OutputSection. We would
merge all CFStrings first, then merge all regular code sections in a
second phase. This worked fine since CFStrings would never reference
regular `__text` sections. However, I would like to expand ICF to merge
functions that reference unwind info. Unwind info references the LSDA
section, which can in turn reference the `__text` section, so we cannot
perform ICF in phases.
In order to have ICF operate on InputSections spanning multiple
OutputSections, we need a way to distinguish InputSections that are
destined for different OutputSections, so that we don't fold across
section boundaries. We achieve this by creating OutputSections early,
and setting `InputSection::parent` to point to them. This is what
LLD-ELF does. (This change should also make it easier to implement the
`section$start$` symbols.)
This diff also folds InputSections w/o checking their flags, which I
think is the right behavior -- if they are destined for the same
OutputSection, they will have the same flags in the output (even if
their input flags differ). I.e. the `parent` pointer check subsumes the
`flags` check. In practice this has nearly no effect (ICF did not become
any more effective on chromium_framework).
I've also updated ICF.cpp's block comment to better reflect its current
status.
Reviewed By: #lld-macho, smeenai
Differential Revision: https://reviews.llvm.org/D105641
2021-07-18 01:42:26 +08:00
|
|
|
// the application after dyld applies fixups to pointer data. We currently
|
|
|
|
// fold only the __DATA_CONST,__cfstring section.
|
2021-05-20 00:58:17 +08:00
|
|
|
//
|
|
|
|
// The __DATA segment is read/write at the MMU, and as application-writeable
|
|
|
|
// data, none of its sections are eligible for ICF.
|
|
|
|
//
|
|
|
|
// Please see the large block comment in lld/ELF/ICF.cpp for an explanation
|
|
|
|
// of the segregation algorithm.
|
|
|
|
//
|
|
|
|
// FIXME(gkm): implement keep-unique attributes
|
|
|
|
// FIXME(gkm): implement address-significance tables for MachO object files
|
|
|
|
|
2021-07-23 23:33:28 +08:00
|
|
|
// Compare "non-moving" parts of two ConcatInputSections, namely everything
|
|
|
|
// except references to other ConcatInputSections.
|
2022-03-08 01:35:12 +08:00
|
|
|
bool ICF::equalsConstant(const ConcatInputSection *ia,
|
|
|
|
const ConcatInputSection *ib) {
|
2022-03-08 01:35:15 +08:00
|
|
|
if (verboseDiagnostics)
|
|
|
|
++equalsConstantCount;
|
[lld-macho] Have ICF operate on all sections at once
ICF previously operated only within a given OutputSection. We would
merge all CFStrings first, then merge all regular code sections in a
second phase. This worked fine since CFStrings would never reference
regular `__text` sections. However, I would like to expand ICF to merge
functions that reference unwind info. Unwind info references the LSDA
section, which can in turn reference the `__text` section, so we cannot
perform ICF in phases.
In order to have ICF operate on InputSections spanning multiple
OutputSections, we need a way to distinguish InputSections that are
destined for different OutputSections, so that we don't fold across
section boundaries. We achieve this by creating OutputSections early,
and setting `InputSection::parent` to point to them. This is what
LLD-ELF does. (This change should also make it easier to implement the
`section$start$` symbols.)
This diff also folds InputSections w/o checking their flags, which I
think is the right behavior -- if they are destined for the same
OutputSection, they will have the same flags in the output (even if
their input flags differ). I.e. the `parent` pointer check subsumes the
`flags` check. In practice this has nearly no effect (ICF did not become
any more effective on chromium_framework).
I've also updated ICF.cpp's block comment to better reflect its current
status.
Reviewed By: #lld-macho, smeenai
Differential Revision: https://reviews.llvm.org/D105641
2021-07-18 01:42:26 +08:00
|
|
|
// We can only fold within the same OutputSection.
|
|
|
|
if (ia->parent != ib->parent)
|
|
|
|
return false;
|
2021-05-20 00:58:17 +08:00
|
|
|
if (ia->data.size() != ib->data.size())
|
|
|
|
return false;
|
|
|
|
if (ia->data != ib->data)
|
|
|
|
return false;
|
|
|
|
if (ia->relocs.size() != ib->relocs.size())
|
|
|
|
return false;
|
2021-07-23 23:33:28 +08:00
|
|
|
auto f = [](const Reloc &ra, const Reloc &rb) {
|
2021-05-20 00:58:17 +08:00
|
|
|
if (ra.type != rb.type)
|
|
|
|
return false;
|
|
|
|
if (ra.pcrel != rb.pcrel)
|
|
|
|
return false;
|
|
|
|
if (ra.length != rb.length)
|
|
|
|
return false;
|
|
|
|
if (ra.offset != rb.offset)
|
|
|
|
return false;
|
|
|
|
if (ra.referent.is<Symbol *>() != rb.referent.is<Symbol *>())
|
2021-07-23 23:33:28 +08:00
|
|
|
return false;
|
2021-05-20 00:58:17 +08:00
|
|
|
|
2021-07-23 23:33:28 +08:00
|
|
|
InputSection *isecA, *isecB;
|
[lld/mac] Fix mislink with ICF
When comparing relocations against two symbols, ICF's equalsConstant() did not
look at the value of the two symbols. With subsections_via_symbols, the value
is usually 0 but not always: In particular, it isn't 0 for constants in string
and literal sections. Since we ignored the value, comparing two constant string
symbols or two literal symbols always compared the 0th's element, so functions
in the same TU always compared as equal.
This can cause mislinks, and, with -dead_strip, crashes.
Fixes PR52349, see that bug for lots of details and examples of mislinks.
While here, make the existing assembly in icf-literals.s a bit more realistic
(use leaq instead of movq with strings, and use foo(%rip) instead of
foo@gotpcrel(%rip)). This has no interesting effect, it just maybe makes the
test look a bit less surprising.
Differential Revision: https://reviews.llvm.org/D112862
2021-10-30 10:25:23 +08:00
|
|
|
|
|
|
|
uint64_t valueA = 0;
|
|
|
|
uint64_t valueB = 0;
|
2021-05-20 00:58:17 +08:00
|
|
|
if (ra.referent.is<Symbol *>()) {
|
|
|
|
const auto *sa = ra.referent.get<Symbol *>();
|
|
|
|
const auto *sb = rb.referent.get<Symbol *>();
|
|
|
|
if (sa->kind() != sb->kind())
|
|
|
|
return false;
|
2022-03-08 21:25:56 +08:00
|
|
|
// ICF runs before Undefineds are treated (and potentially converted into
|
|
|
|
// DylibSymbols).
|
|
|
|
if (isa<DylibSymbol>(sa) || isa<Undefined>(sa))
|
|
|
|
return sa == sb && ra.addend == rb.addend;
|
|
|
|
assert(isa<Defined>(sa));
|
2021-10-18 03:42:27 +08:00
|
|
|
const auto *da = cast<Defined>(sa);
|
|
|
|
const auto *db = cast<Defined>(sb);
|
|
|
|
if (!da->isec || !db->isec) {
|
|
|
|
assert(da->isAbsolute() && db->isAbsolute());
|
2022-03-08 21:25:56 +08:00
|
|
|
return da->value + ra.addend == db->value + rb.addend;
|
2021-10-18 03:42:27 +08:00
|
|
|
}
|
|
|
|
isecA = da->isec;
|
[lld/mac] Fix mislink with ICF
When comparing relocations against two symbols, ICF's equalsConstant() did not
look at the value of the two symbols. With subsections_via_symbols, the value
is usually 0 but not always: In particular, it isn't 0 for constants in string
and literal sections. Since we ignored the value, comparing two constant string
symbols or two literal symbols always compared the 0th's element, so functions
in the same TU always compared as equal.
This can cause mislinks, and, with -dead_strip, crashes.
Fixes PR52349, see that bug for lots of details and examples of mislinks.
While here, make the existing assembly in icf-literals.s a bit more realistic
(use leaq instead of movq with strings, and use foo(%rip) instead of
foo@gotpcrel(%rip)). This has no interesting effect, it just maybe makes the
test look a bit less surprising.
Differential Revision: https://reviews.llvm.org/D112862
2021-10-30 10:25:23 +08:00
|
|
|
valueA = da->value;
|
2021-10-18 03:42:27 +08:00
|
|
|
isecB = db->isec;
|
[lld/mac] Fix mislink with ICF
When comparing relocations against two symbols, ICF's equalsConstant() did not
look at the value of the two symbols. With subsections_via_symbols, the value
is usually 0 but not always: In particular, it isn't 0 for constants in string
and literal sections. Since we ignored the value, comparing two constant string
symbols or two literal symbols always compared the 0th's element, so functions
in the same TU always compared as equal.
This can cause mislinks, and, with -dead_strip, crashes.
Fixes PR52349, see that bug for lots of details and examples of mislinks.
While here, make the existing assembly in icf-literals.s a bit more realistic
(use leaq instead of movq with strings, and use foo(%rip) instead of
foo@gotpcrel(%rip)). This has no interesting effect, it just maybe makes the
test look a bit less surprising.
Differential Revision: https://reviews.llvm.org/D112862
2021-10-30 10:25:23 +08:00
|
|
|
valueB = db->value;
|
2021-07-23 23:33:28 +08:00
|
|
|
} else {
|
|
|
|
isecA = ra.referent.get<InputSection *>();
|
|
|
|
isecB = rb.referent.get<InputSection *>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isecA->parent != isecB->parent)
|
|
|
|
return false;
|
|
|
|
// Sections with identical parents should be of the same kind.
|
|
|
|
assert(isecA->kind() == isecB->kind());
|
|
|
|
// We will compare ConcatInputSection contents in equalsVariable.
|
|
|
|
if (isa<ConcatInputSection>(isecA))
|
2022-03-08 21:25:56 +08:00
|
|
|
return ra.addend == rb.addend;
|
2021-07-23 23:33:28 +08:00
|
|
|
// Else we have two literal sections. References to them are equal iff their
|
|
|
|
// offsets in the output section are equal.
|
2022-04-23 03:34:54 +08:00
|
|
|
if (ra.referent.is<Symbol *>())
|
|
|
|
// For symbol relocs, we compare the contents at the symbol address. We
|
|
|
|
// don't do `getOffset(value + addend)` because value + addend may not be
|
|
|
|
// a valid offset in the literal section.
|
|
|
|
return isecA->getOffset(valueA) == isecB->getOffset(valueB) &&
|
|
|
|
ra.addend == rb.addend;
|
|
|
|
else {
|
|
|
|
assert(valueA == 0 && valueB == 0);
|
|
|
|
// For section relocs, we compare the content at the section offset.
|
|
|
|
return isecA->getOffset(ra.addend) == isecB->getOffset(rb.addend);
|
|
|
|
}
|
2021-07-23 23:33:28 +08:00
|
|
|
};
|
|
|
|
return std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(),
|
|
|
|
f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare the "moving" parts of two ConcatInputSections -- i.e. everything not
|
|
|
|
// handled by equalsConstant().
|
2022-03-08 01:35:12 +08:00
|
|
|
bool ICF::equalsVariable(const ConcatInputSection *ia,
|
|
|
|
const ConcatInputSection *ib) {
|
2022-03-08 01:35:15 +08:00
|
|
|
if (verboseDiagnostics)
|
|
|
|
++equalsVariableCount;
|
2021-07-23 23:33:28 +08:00
|
|
|
assert(ia->relocs.size() == ib->relocs.size());
|
2022-03-08 01:35:12 +08:00
|
|
|
auto f = [this](const Reloc &ra, const Reloc &rb) {
|
2021-07-23 23:33:28 +08:00
|
|
|
// We already filtered out mismatching values/addends in equalsConstant.
|
|
|
|
if (ra.referent == rb.referent)
|
|
|
|
return true;
|
|
|
|
const ConcatInputSection *isecA, *isecB;
|
|
|
|
if (ra.referent.is<Symbol *>()) {
|
|
|
|
// Matching DylibSymbols are already filtered out by the
|
|
|
|
// identical-referent check above. Non-matching DylibSymbols were filtered
|
|
|
|
// out in equalsConstant(). So we can safely cast to Defined here.
|
|
|
|
const auto *da = cast<Defined>(ra.referent.get<Symbol *>());
|
|
|
|
const auto *db = cast<Defined>(rb.referent.get<Symbol *>());
|
|
|
|
if (da->isAbsolute())
|
|
|
|
return true;
|
|
|
|
isecA = dyn_cast<ConcatInputSection>(da->isec);
|
|
|
|
if (!isecA)
|
|
|
|
return true; // literal sections were checked in equalsConstant.
|
|
|
|
isecB = cast<ConcatInputSection>(db->isec);
|
2021-05-20 00:58:17 +08:00
|
|
|
} else {
|
|
|
|
const auto *sa = ra.referent.get<InputSection *>();
|
|
|
|
const auto *sb = rb.referent.get<InputSection *>();
|
2021-07-23 23:33:28 +08:00
|
|
|
isecA = dyn_cast<ConcatInputSection>(sa);
|
|
|
|
if (!isecA)
|
|
|
|
return true;
|
|
|
|
isecB = cast<ConcatInputSection>(sb);
|
2021-05-20 00:58:17 +08:00
|
|
|
}
|
2021-07-23 23:33:28 +08:00
|
|
|
return isecA->icfEqClass[icfPass % 2] == isecB->icfEqClass[icfPass % 2];
|
2021-05-20 00:58:17 +08:00
|
|
|
};
|
2021-11-13 05:01:25 +08:00
|
|
|
if (!std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(), f))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If there are symbols with associated unwind info, check that the unwind
|
|
|
|
// info matches. For simplicity, we only handle the case where there are only
|
|
|
|
// symbols at offset zero within the section (which is typically the case with
|
|
|
|
// .subsections_via_symbols.)
|
2021-11-16 02:46:59 +08:00
|
|
|
auto hasCU = [](Defined *d) { return d->unwindEntry != nullptr; };
|
2021-11-13 05:01:25 +08:00
|
|
|
auto itA = std::find_if(ia->symbols.begin(), ia->symbols.end(), hasCU);
|
|
|
|
auto itB = std::find_if(ib->symbols.begin(), ib->symbols.end(), hasCU);
|
|
|
|
if (itA == ia->symbols.end())
|
|
|
|
return itB == ib->symbols.end();
|
|
|
|
if (itB == ib->symbols.end())
|
|
|
|
return false;
|
|
|
|
const Defined *da = *itA;
|
|
|
|
const Defined *db = *itB;
|
2021-11-16 02:46:59 +08:00
|
|
|
if (da->unwindEntry->icfEqClass[icfPass % 2] !=
|
|
|
|
db->unwindEntry->icfEqClass[icfPass % 2] ||
|
2021-11-13 05:01:25 +08:00
|
|
|
da->value != 0 || db->value != 0)
|
|
|
|
return false;
|
|
|
|
auto isZero = [](Defined *d) { return d->value == 0; };
|
|
|
|
return std::find_if_not(std::next(itA), ia->symbols.end(), isZero) ==
|
|
|
|
ia->symbols.end() &&
|
|
|
|
std::find_if_not(std::next(itB), ib->symbols.end(), isZero) ==
|
|
|
|
ib->symbols.end();
|
2021-05-20 00:58:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first InputSection after BEGIN whose equivalence class differs
|
|
|
|
size_t ICF::findBoundary(size_t begin, size_t end) {
|
|
|
|
uint64_t beginHash = icfInputs[begin]->icfEqClass[icfPass % 2];
|
|
|
|
for (size_t i = begin + 1; i < end; ++i)
|
|
|
|
if (beginHash != icfInputs[i]->icfEqClass[icfPass % 2])
|
|
|
|
return i;
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke FUNC on subranges with matching equivalence class
|
|
|
|
void ICF::forEachClassRange(size_t begin, size_t end,
|
2022-03-05 09:22:40 +08:00
|
|
|
llvm::function_ref<void(size_t, size_t)> func) {
|
2021-05-20 00:58:17 +08:00
|
|
|
while (begin < end) {
|
|
|
|
size_t mid = findBoundary(begin, end);
|
|
|
|
func(begin, mid);
|
|
|
|
begin = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split icfInputs into shards, then parallelize invocation of FUNC on subranges
|
|
|
|
// with matching equivalence class
|
2022-03-05 09:22:40 +08:00
|
|
|
void ICF::forEachClass(llvm::function_ref<void(size_t, size_t)> func) {
|
2021-05-20 00:58:17 +08:00
|
|
|
// Only use threads when the benefits outweigh the overhead.
|
|
|
|
const size_t threadingThreshold = 1024;
|
|
|
|
if (icfInputs.size() < threadingThreshold) {
|
|
|
|
forEachClassRange(0, icfInputs.size(), func);
|
|
|
|
++icfPass;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shard into non-overlapping intervals, and call FUNC in parallel. The
|
|
|
|
// sharding must be completed before any calls to FUNC are made so that FUNC
|
|
|
|
// can modify the InputSection in its shard without causing data races.
|
|
|
|
const size_t shards = 256;
|
|
|
|
size_t step = icfInputs.size() / shards;
|
|
|
|
size_t boundaries[shards + 1];
|
|
|
|
boundaries[0] = 0;
|
|
|
|
boundaries[shards] = icfInputs.size();
|
|
|
|
parallelForEachN(1, shards, [&](size_t i) {
|
|
|
|
boundaries[i] = findBoundary((i - 1) * step, icfInputs.size());
|
|
|
|
});
|
|
|
|
parallelForEachN(1, shards + 1, [&](size_t i) {
|
|
|
|
if (boundaries[i - 1] < boundaries[i]) {
|
|
|
|
forEachClassRange(boundaries[i - 1], boundaries[i], func);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
++icfPass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICF::run() {
|
|
|
|
// Into each origin-section hash, combine all reloc referent section hashes.
|
|
|
|
for (icfPass = 0; icfPass < 2; ++icfPass) {
|
2021-06-25 10:23:04 +08:00
|
|
|
parallelForEach(icfInputs, [&](ConcatInputSection *isec) {
|
2022-03-08 01:35:49 +08:00
|
|
|
uint32_t hash = isec->icfEqClass[icfPass % 2];
|
2021-05-20 00:58:17 +08:00
|
|
|
for (const Reloc &r : isec->relocs) {
|
|
|
|
if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
|
2022-03-08 05:34:02 +08:00
|
|
|
if (auto *defined = dyn_cast<Defined>(sym)) {
|
2021-06-29 02:43:34 +08:00
|
|
|
if (defined->isec) {
|
2022-03-07 12:27:35 +08:00
|
|
|
if (auto referentIsec =
|
|
|
|
dyn_cast<ConcatInputSection>(defined->isec))
|
|
|
|
hash += defined->value + referentIsec->icfEqClass[icfPass % 2];
|
2021-06-29 02:43:34 +08:00
|
|
|
else
|
|
|
|
hash += defined->isec->kind() +
|
|
|
|
defined->isec->getOffset(defined->value);
|
|
|
|
} else {
|
|
|
|
hash += defined->value;
|
|
|
|
}
|
2022-03-08 05:34:02 +08:00
|
|
|
} else {
|
|
|
|
// ICF runs before Undefined diags
|
|
|
|
assert(isa<Undefined>(sym) || isa<DylibSymbol>(sym));
|
|
|
|
}
|
2021-05-20 00:58:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set MSB to 1 to avoid collisions with non-hashed classes.
|
2022-03-08 01:35:49 +08:00
|
|
|
isec->icfEqClass[(icfPass + 1) % 2] = hash | (1ull << 31);
|
2021-05-20 00:58:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-25 10:23:04 +08:00
|
|
|
llvm::stable_sort(
|
|
|
|
icfInputs, [](const ConcatInputSection *a, const ConcatInputSection *b) {
|
|
|
|
return a->icfEqClass[0] < b->icfEqClass[0];
|
|
|
|
});
|
2022-03-08 01:35:12 +08:00
|
|
|
forEachClass([&](size_t begin, size_t end) {
|
|
|
|
segregate(begin, end, &ICF::equalsConstant);
|
|
|
|
});
|
2021-05-20 00:58:17 +08:00
|
|
|
|
|
|
|
// Split equivalence groups by comparing relocations until convergence
|
|
|
|
do {
|
|
|
|
icfRepeat = false;
|
|
|
|
forEachClass([&](size_t begin, size_t end) {
|
2022-03-08 01:35:12 +08:00
|
|
|
segregate(begin, end, &ICF::equalsVariable);
|
2021-05-20 00:58:17 +08:00
|
|
|
});
|
|
|
|
} while (icfRepeat);
|
|
|
|
log("ICF needed " + Twine(icfPass) + " iterations");
|
2022-03-08 01:35:15 +08:00
|
|
|
if (verboseDiagnostics) {
|
|
|
|
log("equalsConstant() called " + Twine(equalsConstantCount) + " times");
|
|
|
|
log("equalsVariable() called " + Twine(equalsVariableCount) + " times");
|
|
|
|
}
|
2021-05-20 00:58:17 +08:00
|
|
|
|
|
|
|
// Fold sections within equivalence classes
|
|
|
|
forEachClass([&](size_t begin, size_t end) {
|
|
|
|
if (end - begin < 2)
|
|
|
|
return;
|
|
|
|
ConcatInputSection *beginIsec = icfInputs[begin];
|
|
|
|
for (size_t i = begin + 1; i < end; ++i)
|
|
|
|
beginIsec->foldIdentical(icfInputs[i]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split an equivalence class into smaller classes.
|
2022-03-08 01:35:12 +08:00
|
|
|
void ICF::segregate(size_t begin, size_t end, EqualsFn equals) {
|
2021-05-20 00:58:17 +08:00
|
|
|
while (begin < end) {
|
|
|
|
// Divide [begin, end) into two. Let mid be the start index of the
|
|
|
|
// second group.
|
2022-03-08 01:35:12 +08:00
|
|
|
auto bound = std::stable_partition(
|
|
|
|
icfInputs.begin() + begin + 1, icfInputs.begin() + end,
|
|
|
|
[&](ConcatInputSection *isec) {
|
|
|
|
return (this->*equals)(icfInputs[begin], isec);
|
|
|
|
});
|
2021-05-20 00:58:17 +08:00
|
|
|
size_t mid = bound - icfInputs.begin();
|
|
|
|
|
|
|
|
// Split [begin, end) into [begin, mid) and [mid, end). We use mid as an
|
|
|
|
// equivalence class ID because every group ends with a unique index.
|
|
|
|
for (size_t i = begin; i < mid; ++i)
|
|
|
|
icfInputs[i]->icfEqClass[(icfPass + 1) % 2] = mid;
|
|
|
|
|
|
|
|
// If we created a group, we need to iterate the main loop again.
|
|
|
|
if (mid != end)
|
|
|
|
icfRepeat = true;
|
|
|
|
|
|
|
|
begin = mid;
|
|
|
|
}
|
|
|
|
}
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
|
2022-05-04 09:00:41 +08:00
|
|
|
void macho::markSymAsAddrSig(Symbol *s) {
|
|
|
|
if (auto *d = dyn_cast_or_null<Defined>(s))
|
|
|
|
if (d->isec)
|
|
|
|
d->isec->keepUnique = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void macho::markAddrSigSymbols() {
|
|
|
|
for (InputFile *file : inputFiles) {
|
|
|
|
ObjFile *obj = dyn_cast<ObjFile>(file);
|
|
|
|
if (!obj)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Section *addrSigSection = obj->addrSigSection;
|
|
|
|
if (!addrSigSection)
|
|
|
|
continue;
|
|
|
|
assert(addrSigSection->subsections.size() == 1);
|
|
|
|
|
|
|
|
Subsection *subSection = &addrSigSection->subsections[0];
|
|
|
|
ArrayRef<unsigned char> &contents = subSection->isec->data;
|
|
|
|
|
|
|
|
const uint8_t *pData = contents.begin();
|
|
|
|
while (pData != contents.end()) {
|
|
|
|
unsigned size;
|
|
|
|
const char *err;
|
|
|
|
uint32_t symIndex = decodeULEB128(pData, &size, contents.end(), &err);
|
|
|
|
if (err)
|
|
|
|
fatal(toString(file) + ": could not decode addrsig section: " + err);
|
|
|
|
markSymAsAddrSig(obj->symbols[symIndex]);
|
|
|
|
pData += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
void macho::foldIdenticalSections() {
|
|
|
|
TimeTraceScope timeScope("Fold Identical Code Sections");
|
|
|
|
// The ICF equivalence-class segregation algorithm relies on pre-computed
|
|
|
|
// hashes of InputSection::data for the ConcatOutputSection::inputs and all
|
|
|
|
// sections referenced by their relocs. We could recursively traverse the
|
|
|
|
// relocs to find every referenced InputSection, but that precludes easy
|
|
|
|
// parallelization. Therefore, we hash every InputSection here where we have
|
|
|
|
// them all accessible as simple vectors.
|
|
|
|
|
|
|
|
// If an InputSection is ineligible for ICF, we give it a unique ID to force
|
|
|
|
// it into an unfoldable singleton equivalence class. Begin the unique-ID
|
|
|
|
// space at inputSections.size(), so that it will never intersect with
|
|
|
|
// equivalence-class IDs which begin at 0. Since hashes & unique IDs never
|
|
|
|
// coexist with equivalence-class IDs, this is not necessary, but might help
|
|
|
|
// someone keep the numbers straight in case we ever need to debug the
|
|
|
|
// ICF::segregate()
|
[lld-macho] Have ICF operate on all sections at once
ICF previously operated only within a given OutputSection. We would
merge all CFStrings first, then merge all regular code sections in a
second phase. This worked fine since CFStrings would never reference
regular `__text` sections. However, I would like to expand ICF to merge
functions that reference unwind info. Unwind info references the LSDA
section, which can in turn reference the `__text` section, so we cannot
perform ICF in phases.
In order to have ICF operate on InputSections spanning multiple
OutputSections, we need a way to distinguish InputSections that are
destined for different OutputSections, so that we don't fold across
section boundaries. We achieve this by creating OutputSections early,
and setting `InputSection::parent` to point to them. This is what
LLD-ELF does. (This change should also make it easier to implement the
`section$start$` symbols.)
This diff also folds InputSections w/o checking their flags, which I
think is the right behavior -- if they are destined for the same
OutputSection, they will have the same flags in the output (even if
their input flags differ). I.e. the `parent` pointer check subsumes the
`flags` check. In practice this has nearly no effect (ICF did not become
any more effective on chromium_framework).
I've also updated ICF.cpp's block comment to better reflect its current
status.
Reviewed By: #lld-macho, smeenai
Differential Revision: https://reviews.llvm.org/D105641
2021-07-18 01:42:26 +08:00
|
|
|
std::vector<ConcatInputSection *> hashable;
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
uint64_t icfUniqueID = inputSections.size();
|
|
|
|
for (ConcatInputSection *isec : inputSections) {
|
[lld-macho] Have ICF operate on all sections at once
ICF previously operated only within a given OutputSection. We would
merge all CFStrings first, then merge all regular code sections in a
second phase. This worked fine since CFStrings would never reference
regular `__text` sections. However, I would like to expand ICF to merge
functions that reference unwind info. Unwind info references the LSDA
section, which can in turn reference the `__text` section, so we cannot
perform ICF in phases.
In order to have ICF operate on InputSections spanning multiple
OutputSections, we need a way to distinguish InputSections that are
destined for different OutputSections, so that we don't fold across
section boundaries. We achieve this by creating OutputSections early,
and setting `InputSection::parent` to point to them. This is what
LLD-ELF does. (This change should also make it easier to implement the
`section$start$` symbols.)
This diff also folds InputSections w/o checking their flags, which I
think is the right behavior -- if they are destined for the same
OutputSection, they will have the same flags in the output (even if
their input flags differ). I.e. the `parent` pointer check subsumes the
`flags` check. In practice this has nearly no effect (ICF did not become
any more effective on chromium_framework).
I've also updated ICF.cpp's block comment to better reflect its current
status.
Reviewed By: #lld-macho, smeenai
Differential Revision: https://reviews.llvm.org/D105641
2021-07-18 01:42:26 +08:00
|
|
|
// FIXME: consider non-code __text sections as hashable?
|
2022-03-08 21:25:59 +08:00
|
|
|
bool isHashable = (isCodeSection(isec) || isCfStringSection(isec) ||
|
|
|
|
isClassRefsSection(isec)) &&
|
2022-05-04 09:00:41 +08:00
|
|
|
!isec->keepUnique && !isec->shouldOmitFromOutput() &&
|
2022-02-23 21:57:52 +08:00
|
|
|
sectionType(isec->getFlags()) == MachO::S_REGULAR;
|
2021-11-13 05:01:25 +08:00
|
|
|
if (isHashable) {
|
[lld-macho] Have ICF operate on all sections at once
ICF previously operated only within a given OutputSection. We would
merge all CFStrings first, then merge all regular code sections in a
second phase. This worked fine since CFStrings would never reference
regular `__text` sections. However, I would like to expand ICF to merge
functions that reference unwind info. Unwind info references the LSDA
section, which can in turn reference the `__text` section, so we cannot
perform ICF in phases.
In order to have ICF operate on InputSections spanning multiple
OutputSections, we need a way to distinguish InputSections that are
destined for different OutputSections, so that we don't fold across
section boundaries. We achieve this by creating OutputSections early,
and setting `InputSection::parent` to point to them. This is what
LLD-ELF does. (This change should also make it easier to implement the
`section$start$` symbols.)
This diff also folds InputSections w/o checking their flags, which I
think is the right behavior -- if they are destined for the same
OutputSection, they will have the same flags in the output (even if
their input flags differ). I.e. the `parent` pointer check subsumes the
`flags` check. In practice this has nearly no effect (ICF did not become
any more effective on chromium_framework).
I've also updated ICF.cpp's block comment to better reflect its current
status.
Reviewed By: #lld-macho, smeenai
Differential Revision: https://reviews.llvm.org/D105641
2021-07-18 01:42:26 +08:00
|
|
|
hashable.push_back(isec);
|
2021-11-13 05:01:25 +08:00
|
|
|
for (Defined *d : isec->symbols)
|
2021-11-16 02:46:59 +08:00
|
|
|
if (d->unwindEntry)
|
|
|
|
hashable.push_back(d->unwindEntry);
|
2022-03-12 02:31:20 +08:00
|
|
|
|
|
|
|
// __cfstring has embedded addends that foil ICF's hashing / equality
|
|
|
|
// checks. (We can ignore embedded addends when doing ICF because the same
|
|
|
|
// information gets recorded in our Reloc structs.) We therefore create a
|
|
|
|
// mutable copy of the CFString and zero out the embedded addends before
|
|
|
|
// performing any hashing / equality checks.
|
|
|
|
if (isCfStringSection(isec) || isClassRefsSection(isec)) {
|
|
|
|
// We have to do this copying serially as the BumpPtrAllocator is not
|
|
|
|
// thread-safe. FIXME: Make a thread-safe allocator.
|
|
|
|
MutableArrayRef<uint8_t> copy = isec->data.copy(bAlloc());
|
|
|
|
for (const Reloc &r : isec->relocs)
|
|
|
|
target->relocateOne(copy.data() + r.offset, r, /*va=*/0,
|
|
|
|
/*relocVA=*/0);
|
|
|
|
isec->data = copy;
|
|
|
|
}
|
2021-11-13 05:01:25 +08:00
|
|
|
} else {
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
isec->icfEqClass[0] = ++icfUniqueID;
|
2021-11-13 05:01:25 +08:00
|
|
|
}
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
}
|
2022-02-23 21:57:52 +08:00
|
|
|
parallelForEach(hashable, [](ConcatInputSection *isec) {
|
|
|
|
assert(isec->icfEqClass[0] == 0); // don't overwrite a unique ID!
|
|
|
|
// Turn-on the top bit to guarantee that valid hashes have no collisions
|
|
|
|
// with the small-integer unique IDs for ICF-ineligible sections
|
2022-03-08 01:35:49 +08:00
|
|
|
isec->icfEqClass[0] = xxHash64(isec->data) | (1ull << 31);
|
2022-02-23 21:57:52 +08:00
|
|
|
});
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
// Now that every input section is either hashed or marked as unique, run the
|
|
|
|
// segregation algorithm to detect foldable subsections.
|
[lld-macho] Have ICF operate on all sections at once
ICF previously operated only within a given OutputSection. We would
merge all CFStrings first, then merge all regular code sections in a
second phase. This worked fine since CFStrings would never reference
regular `__text` sections. However, I would like to expand ICF to merge
functions that reference unwind info. Unwind info references the LSDA
section, which can in turn reference the `__text` section, so we cannot
perform ICF in phases.
In order to have ICF operate on InputSections spanning multiple
OutputSections, we need a way to distinguish InputSections that are
destined for different OutputSections, so that we don't fold across
section boundaries. We achieve this by creating OutputSections early,
and setting `InputSection::parent` to point to them. This is what
LLD-ELF does. (This change should also make it easier to implement the
`section$start$` symbols.)
This diff also folds InputSections w/o checking their flags, which I
think is the right behavior -- if they are destined for the same
OutputSection, they will have the same flags in the output (even if
their input flags differ). I.e. the `parent` pointer check subsumes the
`flags` check. In practice this has nearly no effect (ICF did not become
any more effective on chromium_framework).
I've also updated ICF.cpp's block comment to better reflect its current
status.
Reviewed By: #lld-macho, smeenai
Differential Revision: https://reviews.llvm.org/D105641
2021-07-18 01:42:26 +08:00
|
|
|
ICF(hashable).run();
|
[lld-macho] Move ICF earlier to avoid emitting redundant binds
This is a pretty big refactoring diff, so here are the motivations:
Previously, ICF ran after scanRelocations(), where we emitting
bind/rebase opcodes etc. So we had a bunch of redundant leftovers after
ICF. Having ICF run before Writer seems like a better design, and is
what LLD-ELF does, so this diff refactors it accordingly.
However, ICF had two dependencies on things occurring in Writer: 1) it
needs literals to be deduplicated beforehand and 2) it needs to know
which functions have unwind info, which was being handled by
`UnwindInfoSection::prepareRelocations()`.
In order to do literal deduplication earlier, we need to add literal
input sections to their corresponding output sections. So instead of
putting all input sections into the big `inputSections` vector, and then
filtering them by type later on, I've changed things so that literal
sections get added directly to their output sections during the 'gather'
phase. Likewise for compact unwind sections -- they get added directly
to the UnwindInfoSection now. This latter change is not strictly
necessary, but makes it easier for ICF to determine which functions have
unwind info.
Adding literal sections directly to their output sections means that we
can no longer determine `inputOrder` from iterating over
`inputSections`. Instead, we store that order explicitly on
InputSection. Bloating the size of InputSection for this purpose would
be unfortunate -- but LLD-ELF has already solved this problem: it reuses
`outSecOff` to store this order value.
One downside of this refactor is that we now make an additional pass
over the unwind info relocations to figure out which functions have
unwind info, since want to know that before `processRelocations()`. I've
made sure to run that extra loop only if ICF is enabled, so there should
be no overhead in non-optimizing runs of the linker.
The upside of all this is that the `inputSections` vector now contains
only ConcatInputSections that are destined for ConcatOutputSections, so
we can clean up a bunch of code that just existed to filter out other
elements from that vector.
I will test for the lack of redundant binds/rebases in the upcoming
cfstring deduplication diff. While binds/rebases can also happen in the
regular `.text` section, they're more common in `.data` sections, so it
seems more natural to test it that way.
This change is perf-neutral when linking chromium_framework.
Reviewed By: oontvoo
Differential Revision: https://reviews.llvm.org/D105044
2021-07-02 08:33:42 +08:00
|
|
|
}
|