Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -55,7 +55,8 @@ } void Section::writeSection(FileOutputBuffer &Out) const { - if(Type == SHT_NOBITS) return; + if (Type == SHT_NOBITS) + return; uint8_t *Buf = Out.getBufferStart() + Offset; std::copy(std::begin(Contents), std::end(Contents), Buf); } @@ -104,7 +105,10 @@ void Object::readSectionHeaders(const ELFFile &ElfFile) { uint32_t Index = 0; for (const auto &Shdr : unwrapOrError(ElfFile.sections())) { - if (Index == 0) { ++Index; continue; } + if (Index == 0) { + ++Index; + continue; + } if (Shdr.sh_type == SHT_STRTAB) continue; ArrayRef Data; @@ -121,8 +125,7 @@ Sec->Info = Shdr.sh_info; Sec->Align = Shdr.sh_addralign; Sec->EntrySize = Shdr.sh_entsize; - Sec->Index = Index; - Index++; + Sec->Index = Index++; SectionNames->addString(Sec->Name); Sections.push_back(std::move(Sec)); } @@ -155,7 +158,7 @@ } template void Object::sortSections() { - // Put allocated sections in address order. Maintain ordering as closely as + // Put all sections in offset order. Maintain the ordering as closely as // possible while meeting that demand however. auto CompareSections = [](const SecPtr &A, const SecPtr &B) { return A->Offset < B->Offset; @@ -190,8 +193,10 @@ } // 'offset' should now be just after all the section data so we should set the // section header table offset to be exactly here. This spot might not be - // aligned properlly however so we should align it as needed. This only takes - // a little bit of tweaking to ensure that the sh_name is 4 byte aligned. + // aligned properly however so we should align it as needed. This only takes + // a little bit of tweaking to ensure that the sh_name is 4 byte aligned. By + // ensuring that that sh_name is aligned we ensure all fields of the section + // header are aligned as well. Offset = alignTo(Offset, sizeof(typename ELFT::Word)); SHOffset = Offset; } @@ -245,7 +250,7 @@ template void Object::writeSectionHeaders(FileOutputBuffer &Out) const { uint8_t *Buf = Out.getBufferStart() + SHOffset; - Elf_Shdr &Shdr = *reinterpret_cast(Buf); + Elf_Shdr &Shdr = *reinterpret_cast(Buf); Shdr.sh_name = 0; Shdr.sh_type = SHT_NULL; Shdr.sh_flags = 0;