Skip to content

Commit 38e37bf

Browse files
MaskRaychencha3
authored andcommitted
[ELF] Add isStaticRelSecType to simplify SHT_REL/SHT_RELA testing. NFC
and make it easier to introduce a new relocation format. https://discourse.llvm.org/t/rfc-relleb-a-compact-relocation-format-for-elf/77600 Pull Request: llvm#85893
1 parent 3c55767 commit 38e37bf

File tree

7 files changed

+17
-15
lines changed

7 files changed

+17
-15
lines changed

lld/ELF/InputFiles.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
835835

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

848-
if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) {
848+
if (isStaticRelSecType(sec.sh_type)) {
849849
// Find a relocation target section and associate this section with that.
850850
// Target may have been discarded if it is in a different section group
851851
// and the group is discarded, even though it's a violation of the spec.

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) {
348348
}
349349

350350
InputSectionBase *InputSection::getRelocatedSection() const {
351-
if (file->isInternal() || (type != SHT_RELA && type != SHT_REL))
351+
if (file->isInternal() || !isStaticRelSecType(type))
352352
return nullptr;
353353
ArrayRef<InputSectionBase *> sections = file->getSections();
354354
return sections[info];

lld/ELF/InputSection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ class SyntheticSection : public InputSection {
448448
}
449449
};
450450

451+
inline bool isStaticRelSecType(uint32_t type) {
452+
return type == llvm::ELF::SHT_RELA || type == llvm::ELF::SHT_REL;
453+
}
454+
451455
inline bool isDebugSection(const InputSectionBase &sec) {
452456
return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
453457
sec.name.starts_with(".debug");

lld/ELF/LinkerScript.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,13 +740,12 @@ static OutputDesc *addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
740740
// should combine these relocation sections into single output.
741741
// We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
742742
// other REL[A] sections created by linker itself.
743-
if (!isa<SyntheticSection>(isec) &&
744-
(isec->type == SHT_REL || isec->type == SHT_RELA)) {
743+
if (!isa<SyntheticSection>(isec) && isStaticRelSecType(isec->type)) {
745744
auto *sec = cast<InputSection>(isec);
746745
OutputSection *out = sec->getRelocatedSection()->getOutputSection();
747746

748-
if (out->relocationSection) {
749-
out->relocationSection->recordSection(sec);
747+
if (auto *relSec = out->relocationSection) {
748+
relSec->recordSection(sec);
750749
return nullptr;
751750
}
752751

lld/ELF/MarkLive.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ template <class ELFT> void MarkLive<ELFT>::run() {
276276
// collection.
277277
// - Groups members are retained or discarded as a unit.
278278
if (!(sec->flags & SHF_ALLOC)) {
279-
bool isRel = sec->type == SHT_REL || sec->type == SHT_RELA;
280-
if (!isRel && !sec->nextInSectionGroup) {
279+
if (!isStaticRelSecType(sec->type) && !sec->nextInSectionGroup) {
281280
sec->markLive();
282281
for (InputSection *isec : sec->dependentSections)
283282
isec->markLive();

lld/ELF/OutputSections.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ void OutputSection::finalize() {
615615
return;
616616
}
617617

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

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

751751
void OutputSection::checkDynRelAddends(const uint8_t *bufStart) {
752752
assert(config->writeAddends && config->checkDynamicRelocs);
753-
assert(type == SHT_REL || type == SHT_RELA);
753+
assert(isStaticRelSecType(type));
754754
SmallVector<InputSection *, 0> storage;
755755
ArrayRef<InputSection *> sections = getInputSections(*this, storage);
756756
parallelFor(0, sections.size(), [&](size_t i) {

lld/ELF/Writer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
796796
continue;
797797
for (InputSectionBase *s : isd->sections) {
798798
// Relocations are not using REL[A] section symbols.
799-
if (s->type == SHT_REL || s->type == SHT_RELA)
799+
if (isStaticRelSecType(s->type))
800800
continue;
801801

802802
// Unlike other synthetic sections, mergeable output sections contain
@@ -3045,20 +3045,20 @@ template <class ELFT> void Writer<ELFT>::writeSections() {
30453045
// section while doing it.
30463046
parallel::TaskGroup tg;
30473047
for (OutputSection *sec : outputSections)
3048-
if (sec->type == SHT_REL || sec->type == SHT_RELA)
3048+
if (isStaticRelSecType(sec->type))
30493049
sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
30503050
}
30513051
{
30523052
parallel::TaskGroup tg;
30533053
for (OutputSection *sec : outputSections)
3054-
if (sec->type != SHT_REL && sec->type != SHT_RELA)
3054+
if (!isStaticRelSecType(sec->type))
30553055
sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
30563056
}
30573057

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

0 commit comments

Comments
 (0)