Skip to content

[ELF] Add isStaticRelSecType to simplify SHT_REL/SHT_RELA testing. NFC #85893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lld/ELF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,

// We have a second loop. It is used to:
// 1) handle SHF_LINK_ORDER sections.
// 2) create SHT_REL[A] sections. In some cases the section header index of a
// 2) create relocation sections. In some cases the section header index of a
// relocation section may be smaller than that of the relocated section. In
// such cases, the relocation section would attempt to reference a target
// section that has not yet been created. For simplicity, delay creation of
Expand All @@ -845,7 +845,7 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
continue;
const Elf_Shdr &sec = objSections[i];

if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) {
if (isStaticRelSecType(sec.sh_type)) {
// Find a relocation target section and associate this section with that.
// Target may have been discarded if it is in a different section group
// and the group is discarded, even though it's a violation of the spec.
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/InputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) {
}

InputSectionBase *InputSection::getRelocatedSection() const {
if (file->isInternal() || (type != SHT_RELA && type != SHT_REL))
if (file->isInternal() || !isStaticRelSecType(type))
return nullptr;
ArrayRef<InputSectionBase *> sections = file->getSections();
return sections[info];
Expand Down
4 changes: 4 additions & 0 deletions lld/ELF/InputSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ class SyntheticSection : public InputSection {
}
};

inline bool isStaticRelSecType(uint32_t type) {
return type == llvm::ELF::SHT_RELA || type == llvm::ELF::SHT_REL;
}

inline bool isDebugSection(const InputSectionBase &sec) {
return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
sec.name.starts_with(".debug");
Expand Down
7 changes: 3 additions & 4 deletions lld/ELF/LinkerScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,13 +740,12 @@ static OutputDesc *addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
// should combine these relocation sections into single output.
// We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
// other REL[A] sections created by linker itself.
if (!isa<SyntheticSection>(isec) &&
(isec->type == SHT_REL || isec->type == SHT_RELA)) {
if (!isa<SyntheticSection>(isec) && isStaticRelSecType(isec->type)) {
auto *sec = cast<InputSection>(isec);
OutputSection *out = sec->getRelocatedSection()->getOutputSection();

if (out->relocationSection) {
out->relocationSection->recordSection(sec);
if (auto *relSec = out->relocationSection) {
relSec->recordSection(sec);
return nullptr;
}

Expand Down
3 changes: 1 addition & 2 deletions lld/ELF/MarkLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ template <class ELFT> void MarkLive<ELFT>::run() {
// collection.
// - Groups members are retained or discarded as a unit.
if (!(sec->flags & SHF_ALLOC)) {
bool isRel = sec->type == SHT_REL || sec->type == SHT_RELA;
if (!isRel && !sec->nextInSectionGroup) {
if (!isStaticRelSecType(sec->type) && !sec->nextInSectionGroup) {
sec->markLive();
for (InputSection *isec : sec->dependentSections)
isec->markLive();
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/OutputSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ void OutputSection::finalize() {
return;
}

if (!config->copyRelocs || (type != SHT_RELA && type != SHT_REL))
if (!config->copyRelocs || !isStaticRelSecType(type))
return;

// Skip if 'first' is synthetic, i.e. not a section created by --emit-relocs.
Expand Down Expand Up @@ -750,7 +750,7 @@ std::array<uint8_t, 4> OutputSection::getFiller() {

void OutputSection::checkDynRelAddends(const uint8_t *bufStart) {
assert(config->writeAddends && config->checkDynamicRelocs);
assert(type == SHT_REL || type == SHT_RELA);
assert(isStaticRelSecType(type));
SmallVector<InputSection *, 0> storage;
ArrayRef<InputSection *> sections = getInputSections(*this, storage);
parallelFor(0, sections.size(), [&](size_t i) {
Expand Down
8 changes: 4 additions & 4 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
continue;
for (InputSectionBase *s : isd->sections) {
// Relocations are not using REL[A] section symbols.
if (s->type == SHT_REL || s->type == SHT_RELA)
if (isStaticRelSecType(s->type))
continue;

// Unlike other synthetic sections, mergeable output sections contain
Expand Down Expand Up @@ -3045,20 +3045,20 @@ template <class ELFT> void Writer<ELFT>::writeSections() {
// section while doing it.
parallel::TaskGroup tg;
for (OutputSection *sec : outputSections)
if (sec->type == SHT_REL || sec->type == SHT_RELA)
if (isStaticRelSecType(sec->type))
sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
}
{
parallel::TaskGroup tg;
for (OutputSection *sec : outputSections)
if (sec->type != SHT_REL && sec->type != SHT_RELA)
if (!isStaticRelSecType(sec->type))
sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
}

// Finally, check that all dynamic relocation addends were written correctly.
if (config->checkDynamicRelocs && config->writeAddends) {
for (OutputSection *sec : outputSections)
if (sec->type == SHT_REL || sec->type == SHT_RELA)
if (isStaticRelSecType(sec->type))
sec->checkDynRelAddends(Out::bufferStart);
}
}
Expand Down