[llvm] 6d72845 - [llvm-objcopy][MachO] Code cleanup

Alexander Shaposhnikov via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 29 14:08:38 PDT 2021


Author: Alexander Shaposhnikov
Date: 2021-06-29T14:08:23-07:00
New Revision: 6d72845a8517eea6a69a493351fb4f03f3c10c21

URL: https://github.com/llvm/llvm-project/commit/6d72845a8517eea6a69a493351fb4f03f3c10c21
DIFF: https://github.com/llvm/llvm-project/commit/6d72845a8517eea6a69a493351fb4f03f3c10c21.diff

LOG: [llvm-objcopy][MachO] Code cleanup

1. Remove unnecessary templates.
2. Fix potentially unaligned reads inside constructSection.

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D105089

Added: 
    

Modified: 
    llvm/tools/llvm-objcopy/MachO/MachOReader.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
index d1f87bde1240..da47e4bc61f2 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
@@ -28,7 +28,7 @@ void MachOReader::readHeader(Object &O) const {
 }
 
 template <typename SectionType>
-static Section constructSectionCommon(SectionType Sec, uint32_t Index) {
+static Section constructSectionCommon(const SectionType &Sec, uint32_t Index) {
   StringRef SegName(Sec.segname, strnlen(Sec.segname, sizeof(Sec.segname)));
   StringRef SectName(Sec.sectname, strnlen(Sec.sectname, sizeof(Sec.sectname)));
   Section S(SegName, SectName);
@@ -46,14 +46,11 @@ static Section constructSectionCommon(SectionType Sec, uint32_t Index) {
   return S;
 }
 
-template <typename SectionType>
-Section constructSection(SectionType Sec, uint32_t Index);
-
-template <> Section constructSection(MachO::section Sec, uint32_t Index) {
+Section constructSection(const MachO::section &Sec, uint32_t Index) {
   return constructSectionCommon(Sec, Index);
 }
 
-template <> Section constructSection(MachO::section_64 Sec, uint32_t Index) {
+Section constructSection(const MachO::section_64 &Sec, uint32_t Index) {
   Section S = constructSectionCommon(Sec, Index);
   S.Reserved3 = Sec.reserved3;
   return S;
@@ -63,21 +60,20 @@ template <typename SectionType, typename SegmentType>
 Expected<std::vector<std::unique_ptr<Section>>> static extractSections(
     const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
     const object::MachOObjectFile &MachOObj, uint32_t &NextSectionIndex) {
-  auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
-  const SectionType *Curr =
-      reinterpret_cast<const SectionType *>(LoadCmd.Ptr + sizeof(SegmentType));
   std::vector<std::unique_ptr<Section>> Sections;
-  for (; reinterpret_cast<const void *>(Curr) < End; Curr++) {
-    if (MachOObj.isLittleEndian() != sys::IsLittleEndianHost) {
-      SectionType Sec;
-      memcpy((void *)&Sec, Curr, sizeof(SectionType));
+  for (auto Curr = reinterpret_cast<const SectionType *>(LoadCmd.Ptr +
+                                                         sizeof(SegmentType)),
+            End = reinterpret_cast<const SectionType *>(LoadCmd.Ptr +
+                                                        LoadCmd.C.cmdsize);
+       Curr < End; ++Curr) {
+    SectionType Sec;
+    memcpy((void *)&Sec, Curr, sizeof(SectionType));
+
+    if (MachOObj.isLittleEndian() != sys::IsLittleEndianHost)
       MachO::swapStruct(Sec);
-      Sections.push_back(
-          std::make_unique<Section>(constructSection(Sec, NextSectionIndex)));
-    } else {
-      Sections.push_back(
-          std::make_unique<Section>(constructSection(*Curr, NextSectionIndex)));
-    }
+
+    Sections.push_back(
+        std::make_unique<Section>(constructSection(Sec, NextSectionIndex)));
 
     Section &S = *Sections.back();
 


        


More information about the llvm-commits mailing list