Skip to content

Commit cd630a4

Browse files
committed
Add stats collecting for relative relocations
1 parent 216b4a2 commit cd630a4

File tree

13 files changed

+144
-1
lines changed

13 files changed

+144
-1
lines changed

src/arch-arm32.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ static Thunk<E> &get_reachable_thunk(OutputSection<E> &osec, u64 addr) {
288288
template <>
289289
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
290290
std::span<const ElfRel<E>> rels = get_rels(ctx);
291+
RelocationsStats rels_stats;
291292

292293
for (i64 i = 0; i < rels.size(); i++) {
293294
const ElfRel<E> &rel = rels[i];
@@ -305,6 +306,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
305306
u64 GOT = ctx.got->shdr.sh_addr;
306307

307308
auto check = [&](i64 val, i64 lo, i64 hi) {
309+
if (ctx.arg.stats)
310+
update_relocation_stats(rels_stats, i, val, lo, hi);
308311
check_range(ctx, i, val, lo, hi);
309312
};
310313

@@ -549,6 +552,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
549552
Error(ctx) << *this << ": unknown relocation: " << rel;
550553
}
551554
}
555+
if (ctx.arg.stats)
556+
save_relocation_stats<E>(ctx, *this, rels_stats);
552557
}
553558

554559
template <>

src/arch-arm64.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static bool is_add(u8 *loc) {
146146
template <>
147147
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
148148
std::span<const ElfRel<E>> rels = get_rels(ctx);
149+
RelocationsStats rels_stats;
149150

150151
for (i64 i = 0; i < rels.size(); i++) {
151152
const ElfRel<E> &rel = rels[i];
@@ -154,14 +155,15 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
154155

155156
Symbol<E> &sym = *file.symbols[rel.r_sym];
156157
u8 *loc = base + rel.r_offset;
157-
158158
u64 S = sym.get_addr(ctx);
159159
u64 A = rel.r_addend;
160160
u64 P = get_addr() + rel.r_offset;
161161
u64 G = sym.get_got_idx(ctx) * sizeof(Word<E>);
162162
u64 GOT = ctx.got->shdr.sh_addr;
163163

164164
auto check = [&](i64 val, i64 lo, i64 hi) {
165+
if (ctx.arg.stats)
166+
update_relocation_stats(rels_stats, i, val, lo, hi);
165167
check_range(ctx, i, val, lo, hi);
166168
};
167169

@@ -431,11 +433,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
431433
unreachable();
432434
}
433435
}
436+
if (ctx.arg.stats)
437+
save_relocation_stats<E>(ctx, *this, rels_stats);
434438
}
435439

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

440445
for (i64 i = 0; i < rels.size(); i++) {
441446
const ElfRel<E> &rel = rels[i];
@@ -453,6 +458,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
453458
u64 A = frag ? frag_addend : (i64)rel.r_addend;
454459

455460
auto check = [&](i64 val, i64 lo, i64 hi) {
461+
if (ctx.arg.stats)
462+
update_relocation_stats(rels_stats, i, val, lo, hi);
456463
check_range(ctx, val, i, lo, hi);
457464
};
458465

@@ -473,6 +480,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
473480
break;
474481
}
475482
}
483+
if (ctx.arg.stats)
484+
save_relocation_stats<E>(ctx, *this, rels_stats);
476485
}
477486

478487
template <>

src/arch-i386.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static u32 relax_tlsdesc_to_le(u8 *loc) {
279279
template <>
280280
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
281281
std::span<const ElfRel<E>> rels = get_rels(ctx);
282+
RelocationsStats rels_stats;
282283

283284
for (i64 i = 0; i < rels.size(); i++) {
284285
const ElfRel<E> &rel = rels[i];
@@ -295,6 +296,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
295296
u64 GOT = ctx.got->shdr.sh_addr;
296297

297298
auto check = [&](i64 val, i64 lo, i64 hi) {
299+
if (ctx.arg.stats)
300+
update_relocation_stats(rels_stats, i, val, lo, hi);
298301
check_range(ctx, i, val, lo, hi);
299302
};
300303

@@ -430,11 +433,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
430433
unreachable();
431434
}
432435
}
436+
if (ctx.arg.stats)
437+
save_relocation_stats<E>(ctx, *this, rels_stats);
433438
}
434439

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

439445
for (i64 i = 0; i < rels.size(); i++) {
440446
const ElfRel<E> &rel = rels[i];
@@ -453,6 +459,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
453459
u64 GOT = ctx.got->shdr.sh_addr;
454460

455461
auto check = [&](i64 val, i64 lo, i64 hi) {
462+
if (ctx.arg.stats)
463+
update_relocation_stats(rels_stats, i, val, lo, hi);
456464
check_range(ctx, val, i, lo, hi);
457465
};
458466

@@ -501,6 +509,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
501509
unreachable();
502510
}
503511
}
512+
if (ctx.arg.stats)
513+
save_relocation_stats<E>(ctx, *this, rels_stats);
504514
}
505515

506516
template <>

src/arch-loongarch.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
273273
std::span<RelocDelta> deltas = extra.r_deltas;
274274
i64 k = 0;
275275
u8 *buf = (u8 *)contents.data();
276+
RelocationsStats rels_stats;
276277

277278
for (i64 i = 0; i < rels.size(); i++) {
278279
const ElfRel<E> &rel = rels[i];
@@ -326,6 +327,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
326327
};
327328

328329
auto check_branch = [&](i64 val, i64 lo, i64 hi) {
330+
if (ctx.arg.stats)
331+
update_relocation_stats(rels_stats, i, val, lo, hi);
329332
check(val, lo, hi);
330333
if (val & 0b11)
331334
Error(ctx) << *this << ": misaligned symbol " << sym
@@ -652,6 +655,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
652655
unreachable();
653656
}
654657
}
658+
if (ctx.arg.stats)
659+
save_relocation_stats<E>(ctx, *this, rels_stats);
655660
}
656661

657662
template <>

src/arch-m68k.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
7777
template <>
7878
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
7979
std::span<const ElfRel<E>> rels = get_rels(ctx);
80+
RelocationsStats rels_stats;
8081

8182
for (i64 i = 0; i < rels.size(); i++) {
8283
const ElfRel<E> &rel = rels[i];
@@ -93,6 +94,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
9394
u64 GOT = ctx.got->shdr.sh_addr;
9495

9596
auto check = [&](i64 val, i64 lo, i64 hi) {
97+
if (ctx.arg.stats)
98+
update_relocation_stats(rels_stats, i, val, lo, hi);
9699
check_range(ctx, i, val, lo, hi);
97100
};
98101

@@ -204,6 +207,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
204207
unreachable();
205208
}
206209
}
210+
if (ctx.arg.stats)
211+
save_relocation_stats<E>(ctx, *this, rels_stats);
207212
}
208213

209214
template <>

src/arch-ppc64v1.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
153153
template <>
154154
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
155155
std::span<const ElfRel<E>> rels = get_rels(ctx);
156+
RelocationsStats rels_stats;
156157

157158
for (i64 i = 0; i < rels.size(); i++) {
158159
const ElfRel<E> &rel = rels[i];
@@ -170,6 +171,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
170171
u64 TOC = ctx.extra.TOC->value;
171172

172173
auto check = [&](i64 val, i64 lo, i64 hi) {
174+
if (ctx.arg.stats)
175+
update_relocation_stats(rels_stats, i, val, lo, hi);
173176
check_range(ctx, i, val, lo, hi);
174177
};
175178

@@ -276,11 +279,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
276279
unreachable();
277280
}
278281
}
282+
if (ctx.arg.stats)
283+
save_relocation_stats<E>(ctx, *this, rels_stats);
279284
}
280285

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

285291
for (i64 i = 0; i < rels.size(); i++) {
286292
const ElfRel<E> &rel = rels[i];
@@ -298,6 +304,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
298304
u64 A = frag ? frag_addend : (i64)rel.r_addend;
299305

300306
auto check = [&](i64 val, i64 lo, i64 hi) {
307+
if (ctx.arg.stats)
308+
update_relocation_stats(rels_stats, i, val, lo, hi);
301309
check_range(ctx, val, i, lo, hi);
302310
};
303311

@@ -320,6 +328,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
320328
<< rel;
321329
}
322330
}
331+
if (ctx.arg.stats)
332+
save_relocation_stats<E>(ctx, *this, rels_stats);
323333
}
324334

325335
template <>

src/arch-ppc64v2.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
345345
template <>
346346
void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
347347
std::span<const ElfRel<E>> rels = get_rels(ctx);
348+
RelocationsStats rels_stats;
348349

349350
for (i64 i = 0; i < rels.size(); i++) {
350351
const ElfRel<E> &rel = rels[i];
@@ -362,6 +363,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
362363
u64 A = frag ? frag_addend : (i64)rel.r_addend;
363364

364365
auto check = [&](i64 val, i64 lo, i64 hi) {
366+
if (ctx.arg.stats)
367+
update_relocation_stats(rels_stats, i, val, lo, hi);
365368
check_range(ctx, i, val, lo, hi);
366369
};
367370

@@ -384,6 +387,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
384387
<< rel;
385388
}
386389
}
390+
if (ctx.arg.stats)
391+
save_relocation_stats<E>(ctx, *this, rels_stats);
387392
}
388393

389394
template <>

src/arch-riscv.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
269269
std::span<RelocDelta> deltas = extra.r_deltas;
270270
i64 k = 0;
271271
u8 *buf = (u8 *)contents.data();
272+
RelocationsStats rels_stats;
272273

273274
for (i64 i = 0; i < rels.size(); i++) {
274275
const ElfRel<E> &rel = rels[i];
@@ -298,6 +299,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
298299
u64 GOT = ctx.got->shdr.sh_addr;
299300

300301
auto check = [&](i64 val, i64 lo, i64 hi) {
302+
if (ctx.arg.stats)
303+
update_relocation_stats(rels_stats, i, val, lo, hi);
301304
check_range(ctx, i, val, lo, hi);
302305
};
303306

@@ -642,6 +645,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
642645
unreachable();
643646
}
644647
}
648+
if (ctx.arg.stats)
649+
save_relocation_stats<E>(ctx, *this, rels_stats);
645650
}
646651

647652
template <>

src/arch-s390x.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
115115
template <>
116116
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
117117
std::span<const ElfRel<E>> rels = get_rels(ctx);
118+
RelocationsStats rels_stats;
118119

119120
for (i64 i = 0; i < rels.size(); i++) {
120121
const ElfRel<E> &rel = rels[i];
@@ -131,6 +132,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
131132
u64 GOT = ctx.got->shdr.sh_addr;
132133

133134
auto check = [&](i64 val, i64 lo, i64 hi) {
135+
if (ctx.arg.stats)
136+
update_relocation_stats(rels_stats, i, val, lo, hi);
134137
check_range(ctx, i, val, lo, hi);
135138
};
136139

@@ -330,11 +333,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
330333
unreachable();
331334
}
332335
}
336+
if (ctx.arg.stats)
337+
save_relocation_stats<E>(ctx, *this, rels_stats);
333338
}
334339

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

339345
for (i64 i = 0; i < rels.size(); i++) {
340346
const ElfRel<E> &rel = rels[i];
@@ -352,6 +358,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
352358
u64 A = frag ? frag_addend : (i64)rel.r_addend;
353359

354360
auto check = [&](i64 val, i64 lo, i64 hi) {
361+
if (ctx.arg.stats)
362+
update_relocation_stats(rels_stats, i, val, lo, hi);
355363
check_range(ctx, val, i, lo, hi);
356364
};
357365

@@ -376,6 +384,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
376384
Fatal(ctx) << *this << ": apply_reloc_nonalloc: " << rel;
377385
}
378386
}
387+
if (ctx.arg.stats)
388+
save_relocation_stats<E>(ctx, *this, rels_stats);
379389
}
380390

381391
template <>

src/arch-sparc64.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ void EhFrameSection<E>::apply_eh_reloc(Context<E> &ctx, const ElfRel<E> &rel,
138138
template <>
139139
void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
140140
std::span<const ElfRel<E>> rels = get_rels(ctx);
141+
RelocationsStats rels_stats;
141142

142143
for (i64 i = 0; i < rels.size(); i++) {
143144
const ElfRel<E> &rel = rels[i];
@@ -154,6 +155,8 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
154155
u64 GOT = ctx.got->shdr.sh_addr;
155156

156157
auto check = [&](i64 val, i64 lo, i64 hi) {
158+
if (ctx.arg.stats)
159+
update_relocation_stats(rels_stats, i, val, lo, hi);
157160
check_range(ctx, i, val, lo, hi);
158161
};
159162

@@ -446,11 +449,14 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
446449
unreachable();
447450
}
448451
}
452+
if (ctx.arg.stats)
453+
save_relocation_stats<E>(ctx, *this, rels_stats);
449454
}
450455

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

455461
for (i64 i = 0; i < rels.size(); i++) {
456462
const ElfRel<E> &rel = rels[i];
@@ -468,6 +474,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
468474
u64 A = frag ? frag_addend : (i64)rel.r_addend;
469475

470476
auto check = [&](i64 val, i64 lo, i64 hi) {
477+
if (ctx.arg.stats)
478+
update_relocation_stats(rels_stats, i, val, lo, hi);
471479
check_range(ctx, val, i, lo, hi);
472480
};
473481

@@ -494,6 +502,8 @@ void InputSection<E>::apply_reloc_nonalloc(Context<E> &ctx, u8 *base) {
494502
Fatal(ctx) << *this << ": apply_reloc_nonalloc: " << rel;
495503
}
496504
}
505+
if (ctx.arg.stats)
506+
save_relocation_stats<E>(ctx, *this, rels_stats);
497507
}
498508

499509
template <>

0 commit comments

Comments
 (0)