forked from OSchip/llvm-project
[llvm-objcopy] Rename variable names "Section" to "Sec". NFC
"Section" can refer to the type llvm::objcopy:🧝:Section or the variable name. Rename it to "Sec" for clarity. "Sec" is already used a lot, so this change improves consistency as well. Also change `auto` to `const SectionBase` for readability. Reviewed By: grimar Differential Revision: https://reviews.llvm.org/D67143 llvm-svn: 370852
This commit is contained in:
parent
fea532230b
commit
b8b4fa4797
|
@ -305,9 +305,9 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
|
||||||
SecName.str().c_str());
|
SecName.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isCompressable(const SectionBase &Section) {
|
static bool isCompressable(const SectionBase &Sec) {
|
||||||
return !(Section.Flags & ELF::SHF_COMPRESSED) &&
|
return !(Sec.Flags & ELF::SHF_COMPRESSED) &&
|
||||||
StringRef(Section.Name).startswith(".debug");
|
StringRef(Sec.Name).startswith(".debug");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void replaceDebugSections(
|
static void replaceDebugSections(
|
||||||
|
@ -398,8 +398,8 @@ static Error updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) {
|
||||||
// symbols are still 'needed' and which are not.
|
// symbols are still 'needed' and which are not.
|
||||||
if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
|
if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
|
||||||
!Config.OnlySection.empty()) {
|
!Config.OnlySection.empty()) {
|
||||||
for (auto &Section : Obj.sections())
|
for (SectionBase &Sec : Obj.sections())
|
||||||
Section.markSymbols();
|
Sec.markSymbols();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto RemoveSymbolsPred = [&](const Symbol &Sym) {
|
auto RemoveSymbolsPred = [&](const Symbol &Sym) {
|
||||||
|
|
|
@ -1055,29 +1055,28 @@ void GroupSection::accept(MutableSectionVisitor &Visitor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true IFF a section is wholly inside the range of a segment
|
// Returns true IFF a section is wholly inside the range of a segment
|
||||||
static bool sectionWithinSegment(const SectionBase &Section,
|
static bool sectionWithinSegment(const SectionBase &Sec, const Segment &Seg) {
|
||||||
const Segment &Segment) {
|
|
||||||
// If a section is empty it should be treated like it has a size of 1. This is
|
// If a section is empty it should be treated like it has a size of 1. This is
|
||||||
// to clarify the case when an empty section lies on a boundary between two
|
// to clarify the case when an empty section lies on a boundary between two
|
||||||
// segments and ensures that the section "belongs" to the second segment and
|
// segments and ensures that the section "belongs" to the second segment and
|
||||||
// not the first.
|
// not the first.
|
||||||
uint64_t SecSize = Section.Size ? Section.Size : 1;
|
uint64_t SecSize = Sec.Size ? Sec.Size : 1;
|
||||||
|
|
||||||
if (Section.Type == SHT_NOBITS) {
|
if (Sec.Type == SHT_NOBITS) {
|
||||||
if (!(Section.Flags & SHF_ALLOC))
|
if (!(Sec.Flags & SHF_ALLOC))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool SectionIsTLS = Section.Flags & SHF_TLS;
|
bool SectionIsTLS = Sec.Flags & SHF_TLS;
|
||||||
bool SegmentIsTLS = Segment.Type == PT_TLS;
|
bool SegmentIsTLS = Seg.Type == PT_TLS;
|
||||||
if (SectionIsTLS != SegmentIsTLS)
|
if (SectionIsTLS != SegmentIsTLS)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return Segment.VAddr <= Section.Addr &&
|
return Seg.VAddr <= Sec.Addr &&
|
||||||
Segment.VAddr + Segment.MemSize >= Section.Addr + SecSize;
|
Seg.VAddr + Seg.MemSize >= Sec.Addr + SecSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Segment.Offset <= Section.OriginalOffset &&
|
return Seg.Offset <= Sec.OriginalOffset &&
|
||||||
Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
|
Seg.Offset + Seg.FileSize >= Sec.OriginalOffset + SecSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true IFF a segment's original offset is inside of another segment's
|
// Returns true IFF a segment's original offset is inside of another segment's
|
||||||
|
@ -1141,8 +1140,8 @@ SymbolTableSection *BasicELFBuilder::addSymTab(StringTableSection *StrTab) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BasicELFBuilder::initSections() {
|
void BasicELFBuilder::initSections() {
|
||||||
for (auto &Section : Obj->sections())
|
for (SectionBase &Sec : Obj->sections())
|
||||||
Section.initialize(Obj->sections());
|
Sec.initialize(Obj->sections());
|
||||||
}
|
}
|
||||||
|
|
||||||
void BinaryELFBuilder::addData(SymbolTableSection *SymTab) {
|
void BinaryELFBuilder::addData(SymbolTableSection *SymTab) {
|
||||||
|
@ -1256,10 +1255,9 @@ template <class ELFT> void ELFBuilder<ELFT>::findEhdrOffset() {
|
||||||
if (!ExtractPartition)
|
if (!ExtractPartition)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (const SectionBase &Section : Obj.sections()) {
|
for (const SectionBase &Sec : Obj.sections()) {
|
||||||
if (Section.Type == SHT_LLVM_PART_EHDR &&
|
if (Sec.Type == SHT_LLVM_PART_EHDR && Sec.Name == *ExtractPartition) {
|
||||||
Section.Name == *ExtractPartition) {
|
EhdrOffset = Sec.Offset;
|
||||||
EhdrOffset = Section.Offset;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1288,15 +1286,12 @@ void ELFBuilder<ELFT>::readProgramHeaders(const ELFFile<ELFT> &HeadersFile) {
|
||||||
Seg.MemSize = Phdr.p_memsz;
|
Seg.MemSize = Phdr.p_memsz;
|
||||||
Seg.Align = Phdr.p_align;
|
Seg.Align = Phdr.p_align;
|
||||||
Seg.Index = Index++;
|
Seg.Index = Index++;
|
||||||
for (SectionBase &Section : Obj.sections()) {
|
for (SectionBase &Sec : Obj.sections())
|
||||||
if (sectionWithinSegment(Section, Seg)) {
|
if (sectionWithinSegment(Sec, Seg)) {
|
||||||
Seg.addSection(&Section);
|
Seg.addSection(&Sec);
|
||||||
if (!Section.ParentSegment ||
|
if (!Sec.ParentSegment || Sec.ParentSegment->Offset > Seg.Offset)
|
||||||
Section.ParentSegment->Offset > Seg.Offset) {
|
Sec.ParentSegment = &Seg;
|
||||||
Section.ParentSegment = &Seg;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto &ElfHdr = Obj.ElfHdrSegment;
|
auto &ElfHdr = Obj.ElfHdrSegment;
|
||||||
|
@ -1550,11 +1545,11 @@ template <class ELFT> void ELFBuilder<ELFT>::readSections() {
|
||||||
// Now that all sections and symbols have been added we can add
|
// Now that all sections and symbols have been added we can add
|
||||||
// relocations that reference symbols and set the link and info fields for
|
// relocations that reference symbols and set the link and info fields for
|
||||||
// relocation sections.
|
// relocation sections.
|
||||||
for (auto &Section : Obj.sections()) {
|
for (auto &Sec : Obj.sections()) {
|
||||||
if (&Section == Obj.SymbolTable)
|
if (&Sec == Obj.SymbolTable)
|
||||||
continue;
|
continue;
|
||||||
Section.initialize(Obj.sections());
|
Sec.initialize(Obj.sections());
|
||||||
if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
|
if (auto RelSec = dyn_cast<RelocationSection>(&Sec)) {
|
||||||
auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
|
auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
|
||||||
if (RelSec->Type == SHT_REL)
|
if (RelSec->Type == SHT_REL)
|
||||||
initRelocations(RelSec, Obj.SymbolTable,
|
initRelocations(RelSec, Obj.SymbolTable,
|
||||||
|
@ -1562,7 +1557,7 @@ template <class ELFT> void ELFBuilder<ELFT>::readSections() {
|
||||||
else
|
else
|
||||||
initRelocations(RelSec, Obj.SymbolTable,
|
initRelocations(RelSec, Obj.SymbolTable,
|
||||||
unwrapOrError(ElfFile.relas(Shdr)));
|
unwrapOrError(ElfFile.relas(Shdr)));
|
||||||
} else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
|
} else if (auto GroupSec = dyn_cast<GroupSection>(&Sec)) {
|
||||||
initGroupSection(GroupSec);
|
initGroupSection(GroupSec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1913,17 +1908,17 @@ static uint64_t layoutSections(Range Sections, uint64_t Offset) {
|
||||||
// of the segment we can assign a new offset to the section. For sections not
|
// of the segment we can assign a new offset to the section. For sections not
|
||||||
// covered by segments we can just bump Offset to the next valid location.
|
// covered by segments we can just bump Offset to the next valid location.
|
||||||
uint32_t Index = 1;
|
uint32_t Index = 1;
|
||||||
for (auto &Section : Sections) {
|
for (auto &Sec : Sections) {
|
||||||
Section.Index = Index++;
|
Sec.Index = Index++;
|
||||||
if (Section.ParentSegment != nullptr) {
|
if (Sec.ParentSegment != nullptr) {
|
||||||
auto Segment = *Section.ParentSegment;
|
auto Segment = *Sec.ParentSegment;
|
||||||
Section.Offset =
|
Sec.Offset =
|
||||||
Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
|
Segment.Offset + (Sec.OriginalOffset - Segment.OriginalOffset);
|
||||||
} else {
|
} else {
|
||||||
Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
|
Offset = alignTo(Offset, Sec.Align == 0 ? 1 : Sec.Align);
|
||||||
Section.Offset = Offset;
|
Sec.Offset = Offset;
|
||||||
if (Section.Type != SHT_NOBITS)
|
if (Sec.Type != SHT_NOBITS)
|
||||||
Offset += Section.Size;
|
Offset += Sec.Size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Offset;
|
return Offset;
|
||||||
|
@ -2054,9 +2049,8 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
|
||||||
// Make sure we add the names of all the sections. Importantly this must be
|
// Make sure we add the names of all the sections. Importantly this must be
|
||||||
// done after we decide to add or remove SectionIndexes.
|
// done after we decide to add or remove SectionIndexes.
|
||||||
if (Obj.SectionNames != nullptr)
|
if (Obj.SectionNames != nullptr)
|
||||||
for (const auto &Section : Obj.sections()) {
|
for (const SectionBase &Sec : Obj.sections())
|
||||||
Obj.SectionNames->addString(Section.Name);
|
Obj.SectionNames->addString(Sec.Name);
|
||||||
}
|
|
||||||
|
|
||||||
initEhdrSegment();
|
initEhdrSegment();
|
||||||
|
|
||||||
|
@ -2065,7 +2059,7 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
|
||||||
// size-related fields before doing layout calculations.
|
// size-related fields before doing layout calculations.
|
||||||
uint64_t Index = 0;
|
uint64_t Index = 0;
|
||||||
auto SecSizer = std::make_unique<ELFSectionSizer<ELFT>>();
|
auto SecSizer = std::make_unique<ELFSectionSizer<ELFT>>();
|
||||||
for (auto &Sec : Obj.sections()) {
|
for (SectionBase &Sec : Obj.sections()) {
|
||||||
Sec.Index = Index++;
|
Sec.Index = Index++;
|
||||||
Sec.accept(*SecSizer);
|
Sec.accept(*SecSizer);
|
||||||
}
|
}
|
||||||
|
@ -2092,12 +2086,12 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
|
||||||
// Finally now that all offsets and indexes have been set we can finalize any
|
// Finally now that all offsets and indexes have been set we can finalize any
|
||||||
// remaining issues.
|
// remaining issues.
|
||||||
uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
|
uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
|
||||||
for (SectionBase &Section : Obj.sections()) {
|
for (SectionBase &Sec : Obj.sections()) {
|
||||||
Section.HeaderOffset = Offset;
|
Sec.HeaderOffset = Offset;
|
||||||
Offset += sizeof(Elf_Shdr);
|
Offset += sizeof(Elf_Shdr);
|
||||||
if (WriteSectionHeaders)
|
if (WriteSectionHeaders)
|
||||||
Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
|
Sec.NameIndex = Obj.SectionNames->findIndex(Sec.Name);
|
||||||
Section.finalize();
|
Sec.finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Error E = Buf.allocate(totalSize()))
|
if (Error E = Buf.allocate(totalSize()))
|
||||||
|
@ -2107,9 +2101,9 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Error BinaryWriter::write() {
|
Error BinaryWriter::write() {
|
||||||
for (auto &Section : Obj.sections())
|
for (const SectionBase &Sec : Obj.sections())
|
||||||
if (Section.Flags & SHF_ALLOC)
|
if (Sec.Flags & SHF_ALLOC)
|
||||||
Section.accept(*SecWriter);
|
Sec.accept(*SecWriter);
|
||||||
return Buf.commit();
|
return Buf.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2122,9 +2116,9 @@ Error BinaryWriter::finalize() {
|
||||||
// already had it's offset properly set. We only want to consider the segments
|
// already had it's offset properly set. We only want to consider the segments
|
||||||
// that will affect layout of allocated sections so we only add those.
|
// that will affect layout of allocated sections so we only add those.
|
||||||
std::vector<Segment *> OrderedSegments;
|
std::vector<Segment *> OrderedSegments;
|
||||||
for (SectionBase &Section : Obj.sections())
|
for (const SectionBase &Sec : Obj.sections())
|
||||||
if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr)
|
if ((Sec.Flags & SHF_ALLOC) != 0 && Sec.ParentSegment != nullptr)
|
||||||
OrderedSegments.push_back(Section.ParentSegment);
|
OrderedSegments.push_back(Sec.ParentSegment);
|
||||||
|
|
||||||
// For binary output, we're going to use physical addresses instead of
|
// For binary output, we're going to use physical addresses instead of
|
||||||
// virtual addresses, since a binary output is used for cases like ROM
|
// virtual addresses, since a binary output is used for cases like ROM
|
||||||
|
@ -2172,9 +2166,9 @@ Error BinaryWriter::finalize() {
|
||||||
// not hold. Then pass such a range to layoutSections instead of constructing
|
// not hold. Then pass such a range to layoutSections instead of constructing
|
||||||
// AllocatedSections here.
|
// AllocatedSections here.
|
||||||
std::vector<SectionBase *> AllocatedSections;
|
std::vector<SectionBase *> AllocatedSections;
|
||||||
for (SectionBase &Section : Obj.sections())
|
for (SectionBase &Sec : Obj.sections())
|
||||||
if (Section.Flags & SHF_ALLOC)
|
if (Sec.Flags & SHF_ALLOC)
|
||||||
AllocatedSections.push_back(&Section);
|
AllocatedSections.push_back(&Sec);
|
||||||
layoutSections(make_pointee_range(AllocatedSections), Offset);
|
layoutSections(make_pointee_range(AllocatedSections), Offset);
|
||||||
|
|
||||||
// Now that every section has been laid out we just need to compute the total
|
// Now that every section has been laid out we just need to compute the total
|
||||||
|
@ -2182,9 +2176,9 @@ Error BinaryWriter::finalize() {
|
||||||
// layoutSections, because we want to truncate the last segment to the end of
|
// layoutSections, because we want to truncate the last segment to the end of
|
||||||
// its last section, to match GNU objcopy's behaviour.
|
// its last section, to match GNU objcopy's behaviour.
|
||||||
TotalSize = 0;
|
TotalSize = 0;
|
||||||
for (SectionBase *Section : AllocatedSections)
|
for (SectionBase *Sec : AllocatedSections)
|
||||||
if (Section->Type != SHT_NOBITS)
|
if (Sec->Type != SHT_NOBITS)
|
||||||
TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
|
TotalSize = std::max(TotalSize, Sec->Offset + Sec->Size);
|
||||||
|
|
||||||
if (Error E = Buf.allocate(TotalSize))
|
if (Error E = Buf.allocate(TotalSize))
|
||||||
return E;
|
return E;
|
||||||
|
@ -2268,17 +2262,17 @@ Error IHexWriter::finalize() {
|
||||||
// If any section we're to write has segment then we
|
// If any section we're to write has segment then we
|
||||||
// switch to using physical addresses. Otherwise we
|
// switch to using physical addresses. Otherwise we
|
||||||
// use section virtual address.
|
// use section virtual address.
|
||||||
for (auto &Section : Obj.sections())
|
for (const SectionBase &Sec : Obj.sections())
|
||||||
if (ShouldWrite(Section) && IsInPtLoad(Section)) {
|
if (ShouldWrite(Sec) && IsInPtLoad(Sec)) {
|
||||||
UseSegments = true;
|
UseSegments = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &Section : Obj.sections())
|
for (const SectionBase &Sec : Obj.sections())
|
||||||
if (ShouldWrite(Section) && (!UseSegments || IsInPtLoad(Section))) {
|
if (ShouldWrite(Sec) && (!UseSegments || IsInPtLoad(Sec))) {
|
||||||
if (Error E = checkSection(Section))
|
if (Error E = checkSection(Sec))
|
||||||
return E;
|
return E;
|
||||||
Sections.insert(&Section);
|
Sections.insert(&Sec);
|
||||||
}
|
}
|
||||||
|
|
||||||
IHexSectionWriterBase LengthCalc(Buf);
|
IHexSectionWriterBase LengthCalc(Buf);
|
||||||
|
|
Loading…
Reference in New Issue