Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the extra statistics of relative relocations in large binaries #1375

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add stats collecting for relative relocations
PetrShumilov committed Dec 3, 2024
commit 087d8f9ae33b7328a121f02a63a8ef70742d755a
5 changes: 5 additions & 0 deletions src/arch-arm32.cc
Original file line number Diff line number Diff line change
@@ -253,6 +253,7 @@ static bool is_jump_reachable(i64 val) {
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

auto get_tls_trampoline_addr = [&, i = 0](u64 addr) mutable {
for (; i < output_section->thunks.size(); i++) {
@@ -273,6 +274,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -532,6 +535,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
Error(ctx) << *this << ": unknown relocation: " << rel;
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
10 changes: 10 additions & 0 deletions src/arch-arm64.cc
Original file line number Diff line number Diff line change
@@ -146,6 +146,7 @@ static bool is_add(u8 *loc) {
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -156,6 +157,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -434,11 +437,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -449,6 +455,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -481,6 +489,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
break;
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
10 changes: 10 additions & 0 deletions src/arch-i386.cc
Original file line number Diff line number Diff line change
@@ -285,6 +285,7 @@ static u32 relax_tlsdesc_to_le(u8 *loc) {
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -295,6 +296,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -435,11 +438,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -450,6 +456,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -509,6 +517,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
5 changes: 5 additions & 0 deletions src/arch-loongarch.cc
Original file line number Diff line number Diff line change
@@ -265,6 +265,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

auto get_r_delta = [&](i64 idx) {
return extra.r_deltas.empty() ? 0 : extra.r_deltas[idx];
@@ -284,6 +285,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -657,6 +660,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
5 changes: 5 additions & 0 deletions src/arch-m68k.cc
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -87,6 +88,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -207,6 +210,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
10 changes: 10 additions & 0 deletions src/arch-ppc64v1.cc
Original file line number Diff line number Diff line change
@@ -153,6 +153,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -163,6 +164,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -279,11 +282,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -294,6 +300,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -328,6 +336,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
<< rel;
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
5 changes: 5 additions & 0 deletions src/arch-ppc64v2.cc
Original file line number Diff line number Diff line change
@@ -344,6 +344,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
template <>
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -354,6 +355,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -388,6 +391,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
<< rel;
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
5 changes: 5 additions & 0 deletions src/arch-riscv.cc
Original file line number Diff line number Diff line change
@@ -208,6 +208,7 @@ static inline bool is_hi20(const ElfRel<E> &rel) {
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;
u64 GP = ctx.__global_pointer ? ctx.__global_pointer->get_addr(ctx) : 0;

auto get_r_delta = [&](i64 idx) {
@@ -225,6 +226,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -620,6 +623,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
10 changes: 10 additions & 0 deletions src/arch-s390x.cc
Original file line number Diff line number Diff line change
@@ -115,6 +115,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -125,6 +126,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -323,11 +326,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -338,6 +344,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -372,6 +380,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
Fatal(ctx) << *this << ": apply_reloc_nonalloc: " << rel;
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
10 changes: 10 additions & 0 deletions src/arch-sparc64.cc
Original file line number Diff line number Diff line change
@@ -141,6 +141,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
template <>
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -151,6 +152,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -452,11 +455,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
unreachable();
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
std::span<const ElfRel<E>> rels = get_rels(ctx);
RelocationsStats rels_stats;

for (i64 i = 0; i < rels.size(); i++) {
const ElfRel<E> &rel = rels[i];
@@ -467,6 +473,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset;

auto check = [&](i64 val, i64 lo, i64 hi) {
if (ctx.arg.stats)
update_relocation_stats(rels_stats, i, val, lo, hi);
if (val < lo || hi <= val)
Error(ctx) << *this << ": relocation " << rel << " against "
<< sym << " out of range: " << val << " is not in ["
@@ -505,6 +513,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
Fatal(ctx) << *this << ": apply_reloc_nonalloc: " << rel;
}
}
if (ctx.arg.stats)
save_relocation_stats<E>(ctx, *this, rels_stats);
}

template <>
Loading