Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Aug 17, 2023
1 parent 68ac03d commit a94cd68
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
13 changes: 6 additions & 7 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ class Chunk {
virtual ChunkKind kind() { return SYNTHETIC; }
virtual OutputSection<E> *to_osec() { return nullptr; }
virtual i64 get_reldyn_size(Context<E> &ctx) const { return 0; }
virtual void construct_relr(Context<E> &ctx) {}
virtual void copy_buf(Context<E> &ctx) {}
virtual void write_to(Context<E> &ctx, u8 *buf) { unreachable(); }
virtual void update_shdr(Context<E> &ctx) {}
Expand Down Expand Up @@ -382,6 +383,9 @@ class Chunk {

// For --section-order
i64 sect_order = 0;

// For --pack-dyn-relocs=relr
std::vector<u64> relr;
};

// ELF header
Expand Down Expand Up @@ -450,17 +454,14 @@ class OutputSection : public Chunk<E> {
OutputSection(std::string_view name, u32 type, u64 flags);
ChunkKind kind() override { return OUTPUT_SECTION; }
OutputSection<E> *to_osec() override { return this; }
void construct_relr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;
void write_to(Context<E> &ctx, u8 *buf) override;

void compute_symtab_size(Context<E> &ctx) override;
void populate_symtab(Context<E> &ctx) override;

std::vector<InputSection<E> *> members;

void construct_relr(Context<E> &ctx);
std::vector<u64> relr;

std::vector<std::unique_ptr<RangeExtensionThunk<E>>> thunks;
std::unique_ptr<RelocSection<E>> reloc_sec;
};
Expand Down Expand Up @@ -492,6 +493,7 @@ class GotSection : public Chunk<E> {
i64 get_reldyn_size(Context<E> &ctx) const override;
void copy_buf(Context<E> &ctx) override;

void construct_relr(Context<E> &ctx) override;
void compute_symtab_size(Context<E> &ctx) override;
void populate_symtab(Context<E> &ctx) override;

Expand All @@ -500,9 +502,6 @@ class GotSection : public Chunk<E> {
std::vector<Symbol<E> *> tlsdesc_syms;
std::vector<Symbol<E> *> gottp_syms;
u32 tlsld_idx = -1;

void construct_relr(Context<E> &ctx);
std::vector<u64> relr;
};

template <typename E>
Expand Down
17 changes: 6 additions & 11 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -448,24 +448,19 @@ template <typename E>
void RelrDynSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_link = ctx.dynsym->shndx;

i64 n = ctx.got->relr.size();
i64 n = 0;
for (Chunk<E> *chunk : ctx.chunks)
if (OutputSection<E> *osec = chunk->to_osec())
n += osec->relr.size();
n += chunk->relr.size();
this->shdr.sh_size = n * sizeof(Word<E>);
}

template <typename E>
void RelrDynSection<E>::copy_buf(Context<E> &ctx) {
Word<E> *buf = (Word<E> *)(ctx.buf + this->shdr.sh_offset);

for (u64 val : ctx.got->relr)
*buf++ = (val & 1) ? val : (ctx.got->shdr.sh_addr + val);

for (Chunk<E> *chunk : ctx.chunks)
if (OutputSection<E> *osec = chunk->to_osec())
for (u64 val : osec->relr)
*buf++ = (val & 1) ? val : (osec->shdr.sh_addr + val);
for (u64 val : chunk->relr)
*buf++ = (val & 1) ? val : (chunk->shdr.sh_addr + val);
}

template <typename E>
Expand Down Expand Up @@ -997,7 +992,7 @@ void OutputSection<E>::construct_relr(Context<E> &ctx) {

// Compress them
std::vector<u64> pos = flatten(shards);
relr = encode_relr(pos, sizeof(Word<E>));
this->relr = encode_relr(pos, sizeof(Word<E>));
}

// Compute spaces needed for thunk symbols
Expand Down Expand Up @@ -1305,7 +1300,7 @@ void GotSection<E>::construct_relr(Context<E> &ctx) {
if (ent.is_relr(ctx))
pos.push_back(ent.idx * sizeof(Word<E>));

relr = encode_relr(pos, sizeof(Word<E>));
this->relr = encode_relr(pos, sizeof(Word<E>));
}

template <typename E>
Expand Down
5 changes: 1 addition & 4 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1504,11 +1504,8 @@ void construct_relr(Context<E> &ctx) {
Timer t(ctx, "construct_relr");

tbb::parallel_for_each(ctx.chunks, [&](Chunk<E> *chunk) {
if (OutputSection<E> *osec = chunk->to_osec())
osec->construct_relr(ctx);
chunk->construct_relr(ctx);
});

ctx.got->construct_relr(ctx);
}

template <typename E>
Expand Down

0 comments on commit a94cd68

Please sign in to comment.