Skip to content

Commit ade55d0

Browse files
committed
[llvm-objcopy][ELF] Add OriginalType & OriginalFlags
`llvm::objcopy::elf::*Section::classof` matches Type and Flags, yet Type and Flags are mutable (by setSectionFlagsAndTypes and upcoming --only-keep-debug feature). Add OriginalType & OriginalFlags to be used in classof, to prevent classof results from changing. Reviewed By: jakehehrlich, jhenderson, alexshap Differential Revision: https://reviews.llvm.org/D69739
1 parent 03bf229 commit ade55d0

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

llvm/tools/llvm-objcopy/ELF/Object.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ void GnuDebugLinkSection::init(StringRef File) {
10091009
Size = alignTo(FileName.size() + 1, 4) + 4;
10101010
// The CRC32 will only be aligned if we align the whole section.
10111011
Align = 4;
1012-
Type = ELF::SHT_PROGBITS;
1012+
Type = OriginalType = ELF::SHT_PROGBITS;
10131013
Name = ".gnu_debuglink";
10141014
// For sections not found in segments, OriginalOffset is only used to
10151015
// establish the order that sections should go in. By using the maximum
@@ -1521,8 +1521,8 @@ template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
15211521
}
15221522
auto &Sec = makeSection(Shdr);
15231523
Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
1524-
Sec.Type = Shdr.sh_type;
1525-
Sec.Flags = Shdr.sh_flags;
1524+
Sec.Type = Sec.OriginalType = Shdr.sh_type;
1525+
Sec.Flags = Sec.OriginalFlags = Shdr.sh_flags;
15261526
Sec.Addr = Shdr.sh_addr;
15271527
Sec.Offset = Shdr.sh_offset;
15281528
Sec.OriginalOffset = Shdr.sh_offset;

llvm/tools/llvm-objcopy/ELF/Object.h

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,13 @@ class SectionBase {
384384
std::string Name;
385385
Segment *ParentSegment = nullptr;
386386
uint64_t HeaderOffset;
387-
uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
388387
uint32_t Index;
389388
bool HasSymbol = false;
390389

390+
uint64_t OriginalFlags = 0;
391+
uint64_t OriginalType = ELF::SHT_NULL;
392+
uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
393+
391394
uint64_t Addr = 0;
392395
uint64_t Align = 1;
393396
uint32_t EntrySize = 0;
@@ -490,17 +493,17 @@ class OwnedDataSection : public SectionBase {
490493
OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
491494
: Data(std::begin(Data), std::end(Data)) {
492495
Name = SecName.str();
493-
Type = ELF::SHT_PROGBITS;
496+
Type = OriginalType = ELF::SHT_PROGBITS;
494497
Size = Data.size();
495498
OriginalOffset = std::numeric_limits<uint64_t>::max();
496499
}
497500

498501
OwnedDataSection(const Twine &SecName, uint64_t SecAddr, uint64_t SecFlags,
499502
uint64_t SecOff) {
500503
Name = SecName.str();
501-
Type = ELF::SHT_PROGBITS;
504+
Type = OriginalType = ELF::SHT_PROGBITS;
502505
Addr = SecAddr;
503-
Flags = SecFlags;
506+
Flags = OriginalFlags = SecFlags;
504507
OriginalOffset = SecOff;
505508
}
506509

@@ -530,7 +533,7 @@ class CompressedSection : public SectionBase {
530533
void accept(MutableSectionVisitor &Visitor) override;
531534

532535
static bool classof(const SectionBase *S) {
533-
return (S->Flags & ELF::SHF_COMPRESSED) ||
536+
return (S->OriginalFlags & ELF::SHF_COMPRESSED) ||
534537
(StringRef(S->Name).startswith(".zdebug"));
535538
}
536539
};
@@ -543,7 +546,7 @@ class DecompressedSection : public SectionBase {
543546
: SectionBase(Sec) {
544547
Size = Sec.getDecompressedSize();
545548
Align = Sec.getDecompressedAlign();
546-
Flags = (Flags & ~ELF::SHF_COMPRESSED);
549+
Flags = OriginalFlags = (Flags & ~ELF::SHF_COMPRESSED);
547550
if (StringRef(Name).startswith(".zdebug"))
548551
Name = "." + Name.substr(2);
549552
}
@@ -567,7 +570,7 @@ class StringTableSection : public SectionBase {
567570

568571
public:
569572
StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
570-
Type = ELF::SHT_STRTAB;
573+
Type = OriginalType = ELF::SHT_STRTAB;
571574
}
572575

573576
void addString(StringRef Name);
@@ -577,9 +580,9 @@ class StringTableSection : public SectionBase {
577580
void accept(MutableSectionVisitor &Visitor) override;
578581

579582
static bool classof(const SectionBase *S) {
580-
if (S->Flags & ELF::SHF_ALLOC)
583+
if (S->OriginalFlags & ELF::SHF_ALLOC)
581584
return false;
582-
return S->Type == ELF::SHT_STRTAB;
585+
return S->OriginalType == ELF::SHT_STRTAB;
583586
}
584587
};
585588

@@ -648,7 +651,7 @@ class SectionIndexSection : public SectionBase {
648651
Name = ".symtab_shndx";
649652
Align = 4;
650653
EntrySize = 4;
651-
Type = ELF::SHT_SYMTAB_SHNDX;
654+
Type = OriginalType = ELF::SHT_SYMTAB_SHNDX;
652655
}
653656
};
654657

@@ -666,7 +669,7 @@ class SymbolTableSection : public SectionBase {
666669
using SymPtr = std::unique_ptr<Symbol>;
667670

668671
public:
669-
SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
672+
SymbolTableSection() { Type = OriginalType = ELF::SHT_SYMTAB; }
670673

671674
void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
672675
uint64_t Value, uint8_t Visibility, uint16_t Shndx,
@@ -695,7 +698,7 @@ class SymbolTableSection : public SectionBase {
695698
const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
696699

697700
static bool classof(const SectionBase *S) {
698-
return S->Type == ELF::SHT_SYMTAB;
701+
return S->OriginalType == ELF::SHT_SYMTAB;
699702
}
700703
};
701704

@@ -724,7 +727,7 @@ class RelocationSectionBase : public SectionBase {
724727
void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
725728

726729
static bool classof(const SectionBase *S) {
727-
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
730+
return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
728731
}
729732
};
730733

@@ -762,9 +765,9 @@ class RelocationSection
762765
const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
763766

764767
static bool classof(const SectionBase *S) {
765-
if (S->Flags & ELF::SHF_ALLOC)
768+
if (S->OriginalFlags & ELF::SHF_ALLOC)
766769
return false;
767-
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
770+
return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
768771
}
769772
};
770773

@@ -799,7 +802,7 @@ class GroupSection : public SectionBase {
799802
const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
800803

801804
static bool classof(const SectionBase *S) {
802-
return S->Type == ELF::SHT_GROUP;
805+
return S->OriginalType == ELF::SHT_GROUP;
803806
}
804807
};
805808

@@ -808,7 +811,7 @@ class DynamicSymbolTableSection : public Section {
808811
explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
809812

810813
static bool classof(const SectionBase *S) {
811-
return S->Type == ELF::SHT_DYNSYM;
814+
return S->OriginalType == ELF::SHT_DYNSYM;
812815
}
813816
};
814817

@@ -817,7 +820,7 @@ class DynamicSection : public Section {
817820
explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
818821

819822
static bool classof(const SectionBase *S) {
820-
return S->Type == ELF::SHT_DYNAMIC;
823+
return S->OriginalType == ELF::SHT_DYNAMIC;
821824
}
822825
};
823826

@@ -838,9 +841,9 @@ class DynamicRelocationSection
838841
function_ref<bool(const SectionBase *)> ToRemove) override;
839842

840843
static bool classof(const SectionBase *S) {
841-
if (!(S->Flags & ELF::SHF_ALLOC))
844+
if (!(S->OriginalFlags & ELF::SHF_ALLOC))
842845
return false;
843-
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
846+
return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA;
844847
}
845848
};
846849

0 commit comments

Comments
 (0)