[llvm] [llvm-objcopy] MachO: Fix section finding policy for object files (PR #127604)

Dmitry Nechitaev via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 18 01:55:52 PST 2025


https://github.com/Nechda created https://github.com/llvm/llvm-project/pull/127604

llvm-objcopy is unable to find the specified segment when the `--update-section` flag is used. The input file is object.

This PR fixes #127495 

>From f694f516a592f74511388221881552cbf73b46f2 Mon Sep 17 00:00:00 2001
From: Dmitry Nechitaev <nechda6 at gmail.com>
Date: Tue, 18 Feb 2025 12:29:56 +0300
Subject: [PATCH] llvm-objcopy MachO: scan each section when --update-section
 option is used

---
 llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
index 682edffc84f34..b5ddaa5666229 100644
--- a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
+++ b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp
@@ -360,6 +360,12 @@ static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {
 static Expected<Section &> findSection(StringRef SecName, Object &O) {
   StringRef SegName;
   std::tie(SegName, SecName) = SecName.split(",");
+  if (O.Header.FileType == MachO::HeaderFileType::MH_OBJECT) {
+    for (const auto& LC : O.LoadCommands)
+      for (const auto& Sec : LC.Sections)
+        if (Sec->Segname == SegName && Sec->Sectname == SecName)
+          return *Sec;
+  }
   auto FoundSeg =
       llvm::find_if(O.LoadCommands, [SegName](const LoadCommand &LC) {
         return LC.getSegmentName() == SegName;



More information about the llvm-commits mailing list