Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Mar 10, 2024
1 parent 8eae0a3 commit c85dd88
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion elf/mapfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void print_map(Context<E> &ctx) {
<< std::setw(6) << (u64)osec->shdr.sh_addralign
<< " " << osec->name << "\n";

if (osec->kind() != OUTPUT_SECTION)
if (!osec->to_osec())
continue;

std::span<InputSection<E> *> members = ((OutputSection<E> *)osec)->members;
Expand Down
28 changes: 11 additions & 17 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,12 @@ void write_plt_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym);
template <typename E>
void write_pltgot_entry(Context<E> &ctx, u8 *buf, Symbol<E> &sym);

typedef enum { HEADER, OUTPUT_SECTION, SYNTHETIC } ChunkKind;

// Chunk represents a contiguous region in an output file.
template <typename E>
class Chunk {
public:
virtual ~Chunk() = default;
virtual ChunkKind kind() { return SYNTHETIC; }
virtual bool is_header() { return false; }
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) {}
Expand Down Expand Up @@ -397,7 +395,7 @@ class OutputEhdr : public Chunk<E> {
this->shdr.sh_addralign = sizeof(Word<E>);
}

ChunkKind kind() override { return HEADER; }
bool is_header() override { return true; }
void copy_buf(Context<E> &ctx) override;
};

Expand All @@ -411,7 +409,7 @@ class OutputShdr : public Chunk<E> {
this->shdr.sh_addralign = sizeof(Word<E>);
}

ChunkKind kind() override { return HEADER; }
bool is_header() override { return true; }
void copy_buf(Context<E> &ctx) override;
};

Expand All @@ -425,7 +423,7 @@ class OutputPhdr : public Chunk<E> {
this->shdr.sh_addralign = sizeof(Word<E>);
}

ChunkKind kind() override { return HEADER; }
bool is_header() override { return true; }
void update_shdr(Context<E> &ctx) override;
void copy_buf(Context<E> &ctx) override;

Expand Down Expand Up @@ -454,7 +452,6 @@ class OutputSection : public Chunk<E> {
this->shdr.sh_type = type;
}

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;
Expand Down Expand Up @@ -980,7 +977,7 @@ class CompressedSection : public Chunk<E> {

private:
ElfChdr<E> chdr = {};
std::unique_ptr<Compressor> compressed;
std::unique_ptr<Compressor> compressor;
};

template <typename E>
Expand Down Expand Up @@ -1053,11 +1050,8 @@ template <typename E> void write_gdb_index(Context<E> &ctx);
// discards the other by eliminating all sections that the other
// comdat section refers to.
struct ComdatGroup {
ComdatGroup() = default;
ComdatGroup(const ComdatGroup &other) : owner(other.owner.load()) {}

// The file priority of the owner file of this comdat section.
std::atomic_uint32_t owner = -1;
Atomic<u32> owner = -1;
};

template <typename E>
Expand Down Expand Up @@ -1197,12 +1191,12 @@ class ObjectFile : public InputFile<E> {
bool is_gcc_offload_obj = false;
bool is_rust_obj = false;

u64 num_dynrel = 0;
u64 reldyn_offset = 0;
i64 num_dynrel = 0;
i64 reldyn_offset = 0;

u64 fde_idx = 0;
u64 fde_offset = 0;
u64 fde_size = 0;
i64 fde_idx = 0;
i64 fde_offset = 0;
i64 fde_size = 0;

// For ICF
std::unique_ptr<InputSection<E>> llvm_addrsig;
Expand Down
12 changes: 6 additions & 6 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ void ShstrtabSection<E>::update_shdr(Context<E> &ctx) {
i64 offset = 1;

for (Chunk<E> *chunk : ctx.chunks) {
if (chunk->kind() != ChunkKind::HEADER && !chunk->name.empty()) {
if (!chunk->is_header() && !chunk->name.empty()) {
auto [it, inserted] = map.insert({chunk->name, offset});
chunk->shdr.sh_name = it->second;
if (inserted)
Expand All @@ -522,7 +522,7 @@ void ShstrtabSection<E>::copy_buf(Context<E> &ctx) {
base[0] = '\0';

for (Chunk<E> *chunk : ctx.chunks)
if (chunk->kind() != ChunkKind::HEADER && !chunk->name.empty())
if (!chunk->is_header() && !chunk->name.empty())
write_string(base + chunk->shdr.sh_name, chunk->name);
}

Expand Down Expand Up @@ -2748,11 +2748,11 @@ CompressedSection<E>::CompressedSection(Context<E> &ctx, Chunk<E> &chunk) {
switch (ctx.arg.compress_debug_sections) {
case COMPRESS_ZLIB:
chdr.ch_type = ELFCOMPRESS_ZLIB;
compressed.reset(new ZlibCompressor(buf, chunk.shdr.sh_size));
compressor.reset(new ZlibCompressor(buf, chunk.shdr.sh_size));
break;
case COMPRESS_ZSTD:
chdr.ch_type = ELFCOMPRESS_ZSTD;
compressed.reset(new ZstdCompressor(buf, chunk.shdr.sh_size));
compressor.reset(new ZstdCompressor(buf, chunk.shdr.sh_size));
break;
default:
unreachable();
Expand All @@ -2764,7 +2764,7 @@ CompressedSection<E>::CompressedSection(Context<E> &ctx, Chunk<E> &chunk) {
this->shdr = chunk.shdr;
this->shdr.sh_flags |= SHF_COMPRESSED;
this->shdr.sh_addralign = 1;
this->shdr.sh_size = sizeof(chdr) + compressed->compressed_size;
this->shdr.sh_size = sizeof(chdr) + compressor->compressed_size;
this->shndx = chunk.shndx;

// We don't need to keep the original data unless --gdb-index is given.
Expand All @@ -2778,7 +2778,7 @@ template <typename E>
void CompressedSection<E>::copy_buf(Context<E> &ctx) {
u8 *base = ctx.buf + this->shdr.sh_offset;
memcpy(base, &chdr, sizeof(chdr));
compressed->write_to(base + sizeof(chdr));
compressor->write_to(base + sizeof(chdr));
}

template <typename E>
Expand Down
6 changes: 3 additions & 3 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2565,14 +2565,14 @@ void compute_section_headers(Context<E> &ctx) {

// Remove empty chunks.
std::erase_if(ctx.chunks, [&](Chunk<E> *chunk) {
return chunk->kind() != OUTPUT_SECTION && chunk != ctx.gdb_index &&
return !chunk->to_osec() && chunk != ctx.gdb_index &&
chunk->shdr.sh_size == 0;
});

// Set section indices.
i64 shndx = 1;
for (i64 i = 0; i < ctx.chunks.size(); i++)
if (ctx.chunks[i]->kind() != HEADER)
if (!ctx.chunks[i]->is_header())
ctx.chunks[i]->shndx = shndx++;

if (ctx.symtab && SHN_LORESERVE <= shndx) {
Expand Down Expand Up @@ -2658,7 +2658,7 @@ void fix_synthetic_symbols(Context<E> &ctx) {

std::vector<Chunk<E> *> sections;
for (Chunk<E> *chunk : ctx.chunks)
if (chunk->kind() != HEADER && (chunk->shdr.sh_flags & SHF_ALLOC))
if (!chunk->is_header() && (chunk->shdr.sh_flags & SHF_ALLOC))
sections.push_back(chunk);

auto find = [&](std::string name) -> Chunk<E> * {
Expand Down

0 comments on commit c85dd88

Please sign in to comment.