Skip to content

Commit

Permalink
[ELF] Pass Ctx & to Writer. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Sep 21, 2024
1 parent 3320400 commit 49ec508
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3210,5 +3210,5 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
}

// Write the result to the file.
writeResult<ELFT>();
writeResult<ELFT>(ctx);
}
43 changes: 22 additions & 21 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ template <class ELFT> class Writer {
public:
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)

Writer() : buffer(errorHandler().outputBuffer) {}
Writer(Ctx &ctx) : ctx(ctx), buffer(errorHandler().outputBuffer) {}

void run();

Expand Down Expand Up @@ -80,6 +80,7 @@ template <class ELFT> class Writer {
void writeSectionsBinary();
void writeBuildId();

Ctx &ctx;
std::unique_ptr<FileOutputBuffer> &buffer;

void addRelIpltSymbols();
Expand All @@ -91,8 +92,8 @@ template <class ELFT> class Writer {
};
} // anonymous namespace

template <class ELFT> void elf::writeResult() {
Writer<ELFT>().run();
template <class ELFT> void elf::writeResult(Ctx &ctx) {
Writer<ELFT>(ctx).run();
}

static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
Expand Down Expand Up @@ -2409,8 +2410,8 @@ template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
(config->zSeparate == SeparateSegmentKind::Code && prev &&
(prev->p_flags & PF_X) != (p->p_flags & PF_X)) ||
cmd->type == SHT_LLVM_PART_EHDR)
cmd->addrExpr = [] {
return alignToPowerOf2(ctx.script->getDot(), config->maxPageSize);
cmd->addrExpr = [&ctx = this->ctx] {
return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize);
};
// PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS,
// it must be the RW. Align to p_align(PT_TLS) to make sure
Expand All @@ -2426,15 +2427,15 @@ template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
// bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS
// blocks correctly. We need to keep the workaround for a while.
else if (ctx.tlsPhdr && ctx.tlsPhdr->firstSec == p->firstSec)
cmd->addrExpr = [] {
return alignToPowerOf2(ctx.script->getDot(), config->maxPageSize) +
alignToPowerOf2(ctx.script->getDot() % config->maxPageSize,
cmd->addrExpr = [&ctx = this->ctx] {
return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize) +
alignToPowerOf2(ctx.script->getDot() % ctx.arg.maxPageSize,
ctx.tlsPhdr->p_align);
};
else
cmd->addrExpr = [] {
return alignToPowerOf2(ctx.script->getDot(), config->maxPageSize) +
ctx.script->getDot() % config->maxPageSize;
cmd->addrExpr = [&ctx = this->ctx] {
return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize) +
ctx.script->getDot() % ctx.arg.maxPageSize;
};
}
};
Expand Down Expand Up @@ -2766,7 +2767,7 @@ template <class ELFT> void Writer<ELFT>::writeHeader() {

// Open a result file.
template <class ELFT> void Writer<ELFT>::openFile() {
uint64_t maxSize = config->is64 ? INT64_MAX : UINT32_MAX;
uint64_t maxSize = ctx.arg.is64 ? INT64_MAX : UINT32_MAX;
if (fileSize != size_t(fileSize) || maxSize < fileSize) {
std::string msg;
raw_string_ostream s(msg);
Expand All @@ -2778,17 +2779,17 @@ template <class ELFT> void Writer<ELFT>::openFile() {
return;
}

unlinkAsync(config->outputFile);
unlinkAsync(ctx.arg.outputFile);
unsigned flags = 0;
if (!config->relocatable)
if (!ctx.arg.relocatable)
flags |= FileOutputBuffer::F_executable;
if (!config->mmapOutputFile)
if (!ctx.arg.mmapOutputFile)
flags |= FileOutputBuffer::F_no_mmap;
Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
FileOutputBuffer::create(config->outputFile, fileSize, flags);
FileOutputBuffer::create(ctx.arg.outputFile, fileSize, flags);

if (!bufferOrErr) {
error("failed to open " + config->outputFile + ": " +
error("failed to open " + ctx.arg.outputFile + ": " +
llvm::toString(bufferOrErr.takeError()));
return;
}
Expand Down Expand Up @@ -2936,7 +2937,7 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
part.buildId->writeBuildId(output);
}

template void elf::writeResult<ELF32LE>();
template void elf::writeResult<ELF32BE>();
template void elf::writeResult<ELF64LE>();
template void elf::writeResult<ELF64BE>();
template void elf::writeResult<ELF32LE>(Ctx &);
template void elf::writeResult<ELF32BE>(Ctx &);
template void elf::writeResult<ELF64LE>(Ctx &);
template void elf::writeResult<ELF64BE>(Ctx &);
2 changes: 1 addition & 1 deletion lld/ELF/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace lld::elf {
class InputFile;
class OutputSection;
void copySectionsIntoPartitions();
template <class ELFT> void writeResult();
template <class ELFT> void writeResult(Ctx &ctx);

// This describes a program header entry.
// Each contains type, access flags and range of output sections that will be
Expand Down

0 comments on commit 49ec508

Please sign in to comment.