[PATCH] D128067: [llvm-ar] Fix MRI ADDLIB command when used with thin archives

Owen Reynolds via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 17 09:07:25 PDT 2022


gbreynoo created this revision.
gbreynoo added reviewers: MaskRay, jhenderson.
Herald added subscribers: StephenFan, rupprecht.
Herald added a project: All.
gbreynoo requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We did not properly handle using CREATETHIN in an MRI script and attempting to use ADDLIB to add the contents of a regular archive. This fix outputs a meaningful error message in this case and provides some more testing.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128067

Files:
  llvm/test/tools/llvm-ar/mri-thin-archive.test
  llvm/tools/llvm-ar/llvm-ar.cpp


Index: llvm/tools/llvm-ar/llvm-ar.cpp
===================================================================
--- llvm/tools/llvm-ar/llvm-ar.cpp
+++ llvm/tools/llvm-ar/llvm-ar.cpp
@@ -1064,6 +1064,8 @@
     case MRICommand::AddLib: {
       object::Archive &Lib = readLibrary(Rest);
       {
+        if (Thin && !Lib.isThin())
+          fail("cannot add a regular archives contents to a thin archive");
         Error Err = Error::success();
         for (auto &Member : Lib.children(Err))
           addChildMember(NewMembers, Member, /*FlattenArchive=*/Thin);
Index: llvm/test/tools/llvm-ar/mri-thin-archive.test
===================================================================
--- llvm/test/tools/llvm-ar/mri-thin-archive.test
+++ llvm/test/tools/llvm-ar/mri-thin-archive.test
@@ -1,23 +1,50 @@
-RUN: rm -rf %t && mkdir -p %t/addlib/
+# RUN: rm -rf %t
+# RUN: split-file %s %t
+# RUN: mkdir -p %t/addlib/
+# RUN: cd %t
 
-RUN: yaml2obj %S/Inputs/elf.yaml -o %t/elf.o
-RUN: cp %t/elf.o %t/addlib/elf.o
-RUN: cp %t/elf.o %t/delete.o
+# RUN: yaml2obj %S/Inputs/elf.yaml -o %t/elf.o
+# RUN: cp %t/elf.o %t/addlib/elf.o
+# RUN: cp %t/elf.o %t/delete.o
 
-RUN: cd %t && llvm-ar rTc addlib/addlib.ar addlib/elf.o
+# RUN:llvm-ar rTc addlib/thin.ar addlib/elf.o
+# RUN:llvm-ar rc addlib/regular.ar addlib/elf.o
 
-RUN: echo "createthin %t/archive.ar" > %t/mri.script
-RUN: echo "addmod elf.o" >> %t/mri.script
-RUN: echo "addlib addlib/addlib.ar" >> %t/mri.script
-RUN: echo "addmod delete.o" >> %t/mri.script
-RUN: echo "delete delete.o" >> %t/mri.script
-RUN: echo "save" >> %t/mri.script
-RUN: echo "end" >> %t/mri.script
+# RUN: llvm-ar -M < thin.script
+# RUN: FileCheck -input-file=archive.ar %s
 
-RUN: cd %t && llvm-ar -M < mri.script
-RUN: FileCheck -input-file=%t/archive.ar %s
+# CHECK: !<thin>
+# CHECK: elf.o
+# CHECK-NEXT: addlib/elf.o/
+# CHECK-NOT: delete.o
 
-CHECK: !<thin>
-CHECK: elf.o
-CHECK-NEXT: addlib/elf.o/
-CHECK-NOT: delete.o
+# RUN: llvm-ar -M < thin-to-regular.script
+# RUN: FileCheck -input-file=regular.ar %s --check-prefixes=REGULAR
+
+# REGULAR: !<arch>
+# REGULAR: elf.o
+
+# RUN: not llvm-ar -M < regular-to-thin.script 2>&1 | FileCheck %s --check-prefix=ERROR
+# RUN: not ls thin.ar
+# ERROR: error: script line 2: cannot add a regular archives contents to a thin archive
+
+#--- thin.script
+createthin archive.ar
+addmod elf.o
+addlib addlib/thin.ar
+addmod delete.o
+delete delete.o
+save
+end
+
+#--- thin-to-regular.script
+create regular.ar
+addlib addlib/thin.ar
+save
+end
+
+#--- regular-to-thin.script
+createthin thin.ar
+addlib addlib/regular.ar
+save
+end


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128067.437934.patch
Type: text/x-patch
Size: 2602 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220617/c5bb8a38/attachment.bin>


More information about the llvm-commits mailing list