-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
105 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Shrink sections by interpreting relocations. | ||
|
||
#if MOLD_RV64LE || MOLD_RV64BE || MOLD_RV32LE || MOLD_RV32BE | ||
|
||
#include "mold.h" | ||
|
||
#include <tbb/parallel_for_each.h> | ||
|
||
namespace mold::elf { | ||
|
||
using E = MOLD_TARGET; | ||
|
||
static bool is_resizable(InputSection<E> *isec) { | ||
return isec && isec->is_alive && (isec->shdr().sh_flags & SHF_ALLOC) && | ||
(isec->shdr().sh_flags & SHF_EXECINSTR); | ||
} | ||
|
||
template <> | ||
i64 shrink_sections<E>(Context<E> &ctx) { | ||
Timer t(ctx, "shrink_sections"); | ||
|
||
// True if we can use the 2-byte instructions. This is usually true on | ||
// Unix because RV64GC is generally considered the baseline hardware. | ||
bool use_rvc = false; | ||
if constexpr (is_riscv<E>) | ||
use_rvc = get_eflags(ctx) & EF_RISCV_RVC; | ||
|
||
// Find all the relocations that can be relaxed. | ||
// This step should only shrink sections. | ||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) { | ||
for (std::unique_ptr<InputSection<E>> &isec : file->sections) | ||
if (is_resizable(isec.get())) | ||
shrink_section(ctx, *isec, use_rvc); | ||
}); | ||
|
||
// Fix symbol values. | ||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) { | ||
for (Symbol<E> *sym : file->symbols) { | ||
if (sym->file != file) | ||
continue; | ||
|
||
InputSection<E> *isec = sym->get_input_section(); | ||
if (!isec || isec->extra.r_deltas.empty()) | ||
continue; | ||
|
||
std::span<const ElfRel<E>> rels = isec->get_rels(ctx); | ||
auto it = std::lower_bound(rels.begin(), rels.end(), sym->value, | ||
[&](const ElfRel<E> &r, u64 val) { | ||
return r.r_offset < val; | ||
}); | ||
|
||
sym->value -= isec->extra.r_deltas[it - rels.begin()]; | ||
} | ||
}); | ||
|
||
// Re-compute section offset again to finalize them. | ||
compute_section_sizes(ctx); | ||
return set_osec_offsets(ctx); | ||
} | ||
|
||
} // namespace mold::elf | ||
|
||
#endif |