[BOLT] Add BB index to BAT (#86044)

GitOrigin-RevId: 3b3de48fd84b8269d5f45ee0a9dc6b7448368424
diff --git a/MachO/Driver.cpp b/MachO/Driver.cpp
index 919a14b..3624892 100644
--- a/MachO/Driver.cpp
+++ b/MachO/Driver.cpp
@@ -612,7 +612,7 @@
     if (!osec)
       osec = ConcatOutputSection::getOrCreateForInput(isec);
     isec->parent = osec;
-    addInputSection(isec);
+    inputSections.push_back(isec);
 
     // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip
     // and pass them on here.
@@ -1220,18 +1220,53 @@
 
 static void gatherInputSections() {
   TimeTraceScope timeScope("Gathering input sections");
+  int inputOrder = 0;
   for (const InputFile *file : inputFiles) {
     for (const Section *section : file->sections) {
       // Compact unwind entries require special handling elsewhere. (In
       // contrast, EH frames are handled like regular ConcatInputSections.)
       if (section->name == section_names::compactUnwind)
         continue;
-      for (const Subsection &subsection : section->subsections)
-        addInputSection(subsection.isec);
+      ConcatOutputSection *osec = nullptr;
+      for (const Subsection &subsection : section->subsections) {
+        if (auto *isec = dyn_cast<ConcatInputSection>(subsection.isec)) {
+          if (isec->isCoalescedWeak())
+            continue;
+          if (config->emitInitOffsets &&
+              sectionType(isec->getFlags()) == S_MOD_INIT_FUNC_POINTERS) {
+            in.initOffsets->addInput(isec);
+            continue;
+          }
+          isec->outSecOff = inputOrder++;
+          if (!osec)
+            osec = ConcatOutputSection::getOrCreateForInput(isec);
+          isec->parent = osec;
+          inputSections.push_back(isec);
+        } else if (auto *isec =
+                       dyn_cast<CStringInputSection>(subsection.isec)) {
+          if (isec->getName() == section_names::objcMethname) {
+            if (in.objcMethnameSection->inputOrder == UnspecifiedInputOrder)
+              in.objcMethnameSection->inputOrder = inputOrder++;
+            in.objcMethnameSection->addInput(isec);
+          } else {
+            if (in.cStringSection->inputOrder == UnspecifiedInputOrder)
+              in.cStringSection->inputOrder = inputOrder++;
+            in.cStringSection->addInput(isec);
+          }
+        } else if (auto *isec =
+                       dyn_cast<WordLiteralInputSection>(subsection.isec)) {
+          if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder)
+            in.wordLiteralSection->inputOrder = inputOrder++;
+          in.wordLiteralSection->addInput(isec);
+        } else {
+          llvm_unreachable("unexpected input section kind");
+        }
+      }
     }
     if (!file->objCImageInfo.empty())
       in.objCImageInfo->addFile(file);
   }
+  assert(inputOrder <= UnspecifiedInputOrder);
 }
 
 static void foldIdenticalLiterals() {
@@ -1387,7 +1422,6 @@
     concatOutputSections.clear();
     inputFiles.clear();
     inputSections.clear();
-    inputSectionsOrder = 0;
     loadedArchives.clear();
     loadedObjectFrameworks.clear();
     missingAutolinkWarnings.clear();
diff --git a/MachO/InputSection.cpp b/MachO/InputSection.cpp
index 22930d5..8f5affb 100644
--- a/MachO/InputSection.cpp
+++ b/MachO/InputSection.cpp
@@ -37,44 +37,6 @@
               "instances of it");
 
 std::vector<ConcatInputSection *> macho::inputSections;
-int macho::inputSectionsOrder = 0;
-
-// Call this function to add a new InputSection and have it routed to the
-// appropriate container. Depending on its type and current config, it will
-// either be added to 'inputSections' vector or to a synthetic section.
-void lld::macho::addInputSection(InputSection *inputSection) {
-  if (auto *isec = dyn_cast<ConcatInputSection>(inputSection)) {
-    if (isec->isCoalescedWeak())
-      return;
-    if (config->emitInitOffsets &&
-        sectionType(isec->getFlags()) == S_MOD_INIT_FUNC_POINTERS) {
-      in.initOffsets->addInput(isec);
-      return;
-    }
-    isec->outSecOff = inputSectionsOrder++;
-    auto *osec = ConcatOutputSection::getOrCreateForInput(isec);
-    isec->parent = osec;
-    inputSections.push_back(isec);
-  } else if (auto *isec = dyn_cast<CStringInputSection>(inputSection)) {
-    if (isec->getName() == section_names::objcMethname) {
-      if (in.objcMethnameSection->inputOrder == UnspecifiedInputOrder)
-        in.objcMethnameSection->inputOrder = inputSectionsOrder++;
-      in.objcMethnameSection->addInput(isec);
-    } else {
-      if (in.cStringSection->inputOrder == UnspecifiedInputOrder)
-        in.cStringSection->inputOrder = inputSectionsOrder++;
-      in.cStringSection->addInput(isec);
-    }
-  } else if (auto *isec = dyn_cast<WordLiteralInputSection>(inputSection)) {
-    if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder)
-      in.wordLiteralSection->inputOrder = inputSectionsOrder++;
-    in.wordLiteralSection->addInput(isec);
-  } else {
-    llvm_unreachable("unexpected input section kind");
-  }
-
-  assert(inputSectionsOrder <= UnspecifiedInputOrder);
-}
 
 uint64_t InputSection::getFileSize() const {
   return isZeroFill(getFlags()) ? 0 : getSize();
diff --git a/MachO/InputSection.h b/MachO/InputSection.h
index 694bdf7..b25f063 100644
--- a/MachO/InputSection.h
+++ b/MachO/InputSection.h
@@ -302,8 +302,6 @@
 bool isGccExceptTabSection(const InputSection *);
 
 extern std::vector<ConcatInputSection *> inputSections;
-// This is used as a counter for specyfing input order for input sections
-extern int inputSectionsOrder;
 
 namespace section_names {
 
@@ -371,7 +369,6 @@
 
 } // namespace section_names
 
-void addInputSection(InputSection *inputSection);
 } // namespace macho
 
 std::string toString(const macho::InputSection *);
diff --git a/MachO/ObjC.cpp b/MachO/ObjC.cpp
index 5902b82..40df224 100644
--- a/MachO/ObjC.cpp
+++ b/MachO/ObjC.cpp
@@ -790,7 +790,7 @@
       infoCategoryWriter.catPtrListInfo.align);
   listSec->parent = infoCategoryWriter.catPtrListInfo.outputSection;
   listSec->live = true;
-  addInputSection(listSec);
+  allInputSections.push_back(listSec);
 
   listSec->parent = infoCategoryWriter.catPtrListInfo.outputSection;
 
@@ -848,7 +848,7 @@
       infoCategoryWriter.catPtrListInfo.align);
   listSec->parent = infoCategoryWriter.catPtrListInfo.outputSection;
   listSec->live = true;
-  addInputSection(listSec);
+  allInputSections.push_back(listSec);
 
   listSec->parent = infoCategoryWriter.catPtrListInfo.outputSection;
 
@@ -889,7 +889,7 @@
                                bodyData, infoCategoryWriter.catListInfo.align);
   newCatList->parent = infoCategoryWriter.catListInfo.outputSection;
   newCatList->live = true;
-  addInputSection(newCatList);
+  allInputSections.push_back(newCatList);
 
   newCatList->parent = infoCategoryWriter.catListInfo.outputSection;
 
@@ -927,7 +927,7 @@
                                bodyData, infoCategoryWriter.catBodyInfo.align);
   newBodySec->parent = infoCategoryWriter.catBodyInfo.outputSection;
   newBodySec->live = true;
-  addInputSection(newBodySec);
+  allInputSections.push_back(newBodySec);
 
   std::string symName =
       objc::symbol_names::category + baseClassName + "_$_(" + name + ")";
@@ -1132,7 +1132,7 @@
           infoCategoryWriter.catListInfo.align);
       listSec->parent = infoCategoryWriter.catListInfo.outputSection;
       listSec->live = true;
-      addInputSection(listSec);
+      allInputSections.push_back(listSec);
 
       std::string slotSymName = "<__objc_catlist slot for category ";
       slotSymName += nonErasedCatBody->getName();
@@ -1221,11 +1221,9 @@
 
 StringRef ObjcCategoryMerger::newStringData(const char *str) {
   uint32_t len = strlen(str);
-  uint32_t bufSize = len + 1;
-  auto &data = newSectionData(bufSize);
+  auto &data = newSectionData(len + 1);
   char *strData = reinterpret_cast<char *>(data.data());
-  // Copy the string chars and null-terminator
-  memcpy(strData, str, bufSize);
+  strncpy(strData, str, len);
   return StringRef(strData, len);
 }
 
diff --git a/MachO/SyntheticSections.cpp b/MachO/SyntheticSections.cpp
index 1b36945..7ee3261 100644
--- a/MachO/SyntheticSections.cpp
+++ b/MachO/SyntheticSections.cpp
@@ -793,7 +793,7 @@
 
   in.imageLoaderCache->parent =
       ConcatOutputSection::getOrCreateForInput(in.imageLoaderCache);
-  addInputSection(in.imageLoaderCache);
+  inputSections.push_back(in.imageLoaderCache);
   // Since this isn't in the symbol table or in any input file, the noDeadStrip
   // argument doesn't matter.
   dyldPrivate =
@@ -855,7 +855,7 @@
                                 /*addend=*/static_cast<int64_t>(methnameOffset),
                                 /*referent=*/in.objcMethnameSection->isec});
   objcSelref->parent = ConcatOutputSection::getOrCreateForInput(objcSelref);
-  addInputSection(objcSelref);
+  inputSections.push_back(objcSelref);
   objcSelref->isFinal = true;
   methnameToSelref[CachedHashStringRef(methname)] = objcSelref;
   return objcSelref;