Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Feb 5, 2025
1 parent 42cc55e commit 48beed9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ timestamp="$(git log -1 --format=%ci)"
# Build mold in a container.
mkdir -p dist

podman run --arch $arch -it --rm --userns=host --pids-limit=-1 \
podman run --arch $arch -it --rm --userns=host --pids-limit=-1 --network=none \
-v "$(pwd):/mold:ro" -v "$(pwd)/dist:/dist" $image bash -c "
set -e
mkdir /build
Expand Down
21 changes: 10 additions & 11 deletions src/arch-loongarch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static u64 hi20(u64 val, u64 pc) {
return bits(page(val + 0x800) - page(pc), 31, 12);
}

static u64 highest32(u64 val, u64 pc) {
static u64 hi32(u64 val, u64 pc) {
// A PC-relative 64-bit address is materialized with the following
// instructions for the large code model:
//
Expand All @@ -72,11 +72,11 @@ static u64 highest32(u64 val, u64 pc) {
}

static u64 higher20(u64 val, u64 pc) {
return bits(highest32(val, pc), 51, 32);
return bits(hi32(val, pc), 51, 32);
}

static u64 highest12(u64 val, u64 pc) {
return bits(highest32(val, pc), 63, 52);
return bits(hi32(val, pc), 63, 52);
}

static void write_k12(u8 *loc, u32 val) {
Expand Down Expand Up @@ -128,8 +128,8 @@ static void set_rj(u8 *loc, u32 rj) {
// Returns true if isec's i'th relocation refers to the following
// relaxable instructioon pair.
//
// pcalau12i $t0, 0 # R_LARCH_GOT_PC_HI20
// ld.d $t0, $t0, 0 # R_LARCH_GOT_PC_LO12
// pcalau12i $t0, 0 # R_LARCH_GOT_PC_HI20, R_LARCH_RELAX
// ld.d $t0, $t0, 0 # R_LARCH_GOT_PC_LO12, R_LARCH_RELAX
static bool is_relaxable_got_load(Context<E> &ctx, InputSection<E> &isec, i64 i) {
std::span<const ElfRel<E>> rels = isec.get_rels(ctx);
Symbol<E> &sym = *isec.file.symbols[rels[i].r_sym];
Expand Down Expand Up @@ -870,8 +870,8 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
const ElfRel<E> &r = rels[i];
Symbol<E> &sym = *isec.file.symbols[r.r_sym];

auto remove = [&](i64 i) {
r_delta += i;
auto remove = [&](i64 d) {
r_delta += d;
deltas.push_back(RelocDelta{r.r_offset, r_delta});
};

Expand Down Expand Up @@ -986,11 +986,10 @@ void shrink_section(Context<E> &ctx, InputSection<E> &isec) {
// relax them to the following instruction.
//
// pcaddi $t0, <offset>
if (is_relaxable_got_load(ctx, isec, i)) {
i64 dist = compute_distance(ctx, sym, isec, r);
if ((dist & 0b11) == 0 && is_int(dist, 22))
if (is_relaxable_got_load(ctx, isec, i))
if (i64 dist = compute_distance(ctx, sym, isec, r);
is_int(dist, 22))
remove(4);
}
break;
case R_LARCH_TLS_DESC_PC_HI20:
if (sym.has_tlsdesc(ctx)) {
Expand Down
51 changes: 22 additions & 29 deletions src/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,13 @@ void OutputSection<E>::compute_section_size(Context<E> &ctx) {
i64 offset = 0;
};

std::span<InputSection<E> *> mem = members;
std::vector<Group> groups;
constexpr i64 group_size = 10000;

while (!mem.empty()) {
i64 sz = std::min<i64>(group_size, mem.size());
groups.push_back({mem.subspan(0, sz)});
mem = mem.subspan(sz);
for (std::span<InputSection<E> *> m = members; !m.empty();) {
i64 sz = std::min<i64>(group_size, m.size());
groups.push_back({m.subspan(0, sz)});
m = m.subspan(sz);
}

tbb::parallel_for_each(groups, [](Group &group) {
Expand Down Expand Up @@ -915,40 +914,38 @@ void OutputSection<E>::copy_buf(Context<E> &ctx) {
this->reldyn_offset);

for (AbsRel<E> &r : abs_rels) {
Word<E> *loc = (Word<E> *)(buf + r.isec->offset + r.offset);
u64 addr = this->shdr.sh_addr + r.isec->offset + r.offset;
Symbol<E> &sym = *r.sym;
u8 *loc = buf + r.isec->offset + r.offset;
u64 S = sym.get_addr(ctx);
u64 A = r.addend;
u64 P = this->shdr.sh_addr + r.isec->offset + r.offset;

if constexpr (is_riscv<E> || is_loongarch<E>) {
i64 delta = get_r_delta(*r.isec, r.offset);
loc = (Word<E> *)(buf + r.isec->offset + r.offset - delta);
addr -= delta;
loc -= delta;
P -= delta;
}

auto dynrel = [&](i64 ty, i64 idx, u64 val) {
*rel++ = ElfRel<E>(P, ty, idx, val);
if (ctx.arg.apply_dynamic_relocs)
*(Word<E> *)loc = val;
};

switch (r.kind) {
case ABS_REL_NONE:
case ABS_REL_RELR:
*loc = sym.get_addr(ctx) + r.addend;
*(Word<E> *)loc = S + A;
break;
case ABS_REL_BASEREL: {
u64 val = sym.get_addr(ctx) + r.addend;
*rel++ = ElfRel<E>(addr, E::R_RELATIVE, 0, val);
if (ctx.arg.apply_dynamic_relocs)
*loc = val;
case ABS_REL_BASEREL:
dynrel(E::R_RELATIVE, 0, S + A);
break;
}
case ABS_REL_IFUNC:
if constexpr (supports_ifunc<E>) {
u64 val = sym.get_addr(ctx, NO_PLT) + r.addend;
*rel++ = ElfRel<E>(addr, E::R_IRELATIVE, 0, val);
if (ctx.arg.apply_dynamic_relocs)
*loc = val;
}
if constexpr (supports_ifunc<E>)
dynrel(E::R_IRELATIVE, 0, sym.get_addr(ctx, NO_PLT) + A);
break;
case ABS_REL_DYNREL:
*rel++ = ElfRel<E>(addr, E::R_ABS, sym.get_dynsym_idx(ctx), r.addend);
if (ctx.arg.apply_dynamic_relocs)
*loc = r.addend;
dynrel(E::R_ABS, sym.get_dynsym_idx(ctx), A);
break;
}
}
Expand Down Expand Up @@ -2146,11 +2143,7 @@ void MergedSection<E>::compute_section_size(Context<E> &ctx) {
offset += ent->keylen;
}
}

sizes[i] = offset;

static Counter merged_strings("merged_strings");
merged_strings += entries.size();
});

i64 shard_size = map.nbuckets / map.NUM_SHARDS;
Expand Down
8 changes: 8 additions & 0 deletions src/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3296,6 +3296,14 @@ void show_stats(Context<E> &ctx) {
static Counter num_objs("num_objs", ctx.objs.size());
static Counter num_dsos("num_dsos", ctx.dsos.size());

using Entry = typename ConcurrentMap<SectionFragment<E>>::Entry;

static Counter merged_strings("merged_strings");
for (std::unique_ptr<MergedSection<E>> &sec : ctx.merged_sections)
for (Entry &ent : std::span(sec->map.entries, sec->map.nbuckets))
if (ent.key)
merged_strings++;

if constexpr (needs_thunk<E>) {
static Counter thunk_bytes("thunk_bytes");
for (Chunk<E> *chunk : ctx.chunks)
Expand Down

0 comments on commit 48beed9

Please sign in to comment.