[llvm] r187353 - Add support for the 's' operation to llvm-ar.

Rafael Espindola rafael.espindola at gmail.com
Mon Jul 29 05:40:31 PDT 2013


Author: rafael
Date: Mon Jul 29 07:40:31 2013
New Revision: 187353

URL: http://llvm.org/viewvc/llvm-project?rev=187353&view=rev
Log:
Add support for the 's' operation to llvm-ar.

If no other operation is specified, 's' becomes an operation instead of an
modifier. The s operation just creates a symbol table. It is the same as
running ranlib.

We assume the archive was created by a sane ar (like llvm-ar or gnu ar) and
if the symbol table is present, then it is current. We use that to optimize
the most common case: a broken build system that thinks it has to run ranlib.

Added:
    llvm/trunk/test/Object/Inputs/archive-test.a-corrupt-symbol-table
Modified:
    llvm/trunk/include/llvm/Object/Archive.h
    llvm/trunk/lib/Object/Archive.cpp
    llvm/trunk/test/Object/archive-symtab.test
    llvm/trunk/tools/llvm-ar/llvm-ar.cpp

Modified: llvm/trunk/include/llvm/Object/Archive.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/Archive.h?rev=187353&r1=187352&r2=187353&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/Archive.h (original)
+++ llvm/trunk/include/llvm/Object/Archive.h Mon Jul 29 07:40:31 2013
@@ -188,6 +188,8 @@ public:
   // check if a symbol is in the archive
   child_iterator findSym(StringRef name) const;
 
+  bool hasSymbolTable() const;
+
 private:
   child_iterator SymbolTable;
   child_iterator StringTable;

Modified: llvm/trunk/lib/Object/Archive.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/Archive.cpp?rev=187353&r1=187352&r2=187353&view=diff
==============================================================================
--- llvm/trunk/lib/Object/Archive.cpp (original)
+++ llvm/trunk/lib/Object/Archive.cpp Mon Jul 29 07:40:31 2013
@@ -385,7 +385,7 @@ Archive::Symbol Archive::Symbol::getNext
 }
 
 Archive::symbol_iterator Archive::begin_symbols() const {
-  if (SymbolTable == end_children())
+  if (!hasSymbolTable())
     return symbol_iterator(Symbol(this, 0, 0));
 
   const char *buf = SymbolTable->getBuffer().begin();
@@ -408,7 +408,7 @@ Archive::symbol_iterator Archive::begin_
 }
 
 Archive::symbol_iterator Archive::end_symbols() const {
-  if (SymbolTable == end_children())
+  if (!hasSymbolTable())
     return symbol_iterator(Symbol(this, 0, 0));
 
   const char *buf = SymbolTable->getBuffer().begin();
@@ -444,3 +444,7 @@ Archive::child_iterator Archive::findSym
   }
   return end_children();
 }
+
+bool Archive::hasSymbolTable() const {
+  return SymbolTable != end_children();
+}

Added: llvm/trunk/test/Object/Inputs/archive-test.a-corrupt-symbol-table
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/archive-test.a-corrupt-symbol-table?rev=187353&view=auto
==============================================================================
Binary files llvm/trunk/test/Object/Inputs/archive-test.a-corrupt-symbol-table (added) and llvm/trunk/test/Object/Inputs/archive-test.a-corrupt-symbol-table Mon Jul 29 07:40:31 2013 differ

Modified: llvm/trunk/test/Object/archive-symtab.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/archive-symtab.test?rev=187353&r1=187352&r2=187353&view=diff
==============================================================================
--- llvm/trunk/test/Object/archive-symtab.test (original)
+++ llvm/trunk/test/Object/archive-symtab.test Mon Jul 29 07:40:31 2013
@@ -16,3 +16,35 @@ CHECK-NEXT: trivial-object-test2.elf-x86
 CHECK-NEXT: 00000000 t bar
 CHECK-NEXT: 00000006 T foo
 CHECK-NEXT: 00000016 T main
+
+RUN: rm -f %t.a
+RUN: llvm-ar rcS %t.a %p/Inputs/trivial-object-test.elf-x86-64 %p/Inputs/trivial-object-test2.elf-x86-64
+RUN: llvm-nm -s %t.a | FileCheck %s --check-prefix=NOMAP
+
+NOMAP-NOT: Archive map
+
+RUN: llvm-ar s %t.a
+RUN: llvm-nm -s %t.a | FileCheck %s
+
+check that the archive does have a corrupt symbol table.
+RUN: rm -f %t.a
+RUN: cp %p/Inputs/archive-test.a-corrupt-symbol-table %t.a
+RUN: llvm-nm -s %t.a | FileCheck %s --check-prefix=CORRUPT
+
+CORRUPT: Archive map
+CORRUPT-NEXT: mbin in trivial-object-test.elf-x86-64
+CORRUPT-NEXT: foo in trivial-object-test2.elf-x86-64
+CORRUPT-NEXT: main in trivial-object-test2.elf-x86-64
+
+CORRUPT: trivial-object-test.elf-x86-64:
+CORRUPT-NEXT:         U SomeOtherFunction
+CORRUPT-NEXT: 00000000 T main
+CORRUPT-NEXT:         U puts
+CORRUPT-NEXT: trivial-object-test2.elf-x86-64:
+CORRUPT-NEXT: 00000000 t bar
+CORRUPT-NEXT: 00000006 T foo
+CORRUPT-NEXT: 00000016 T main
+
+check that the we *don't* update the symbol table.
+RUN: llvm-ar s %t.a
+RUN: llvm-nm -s %t.a | FileCheck %s --check-prefix=CORRUPT

Modified: llvm/trunk/tools/llvm-ar/llvm-ar.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-ar/llvm-ar.cpp?rev=187353&r1=187352&r2=187353&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-ar/llvm-ar.cpp (original)
+++ llvm/trunk/tools/llvm-ar/llvm-ar.cpp Mon Jul 29 07:40:31 2013
@@ -112,7 +112,8 @@ enum ArchiveOperation {
   QuickAppend,      ///< Quickly append to end of archive
   ReplaceOrInsert,  ///< Replace or Insert members
   DisplayTable,     ///< Display the table of contents
-  Extract           ///< Extract files back to file system
+  Extract,          ///< Extract files back to file system
+  CreateSymTab      ///< Create a symbol table in an existing archive
 };
 
 // Modifiers to follow operation to vary behavior
@@ -186,6 +187,8 @@ static ArchiveOperation parseCommandLine
   // Keep track of which operation was requested
   ArchiveOperation Operation;
 
+  bool MaybeJustCreateSymTab = false;
+
   for(unsigned i=0; i<Options.size(); ++i) {
     switch(Options[i]) {
     case 'd': ++NumOperations; Operation = Delete; break;
@@ -200,6 +203,7 @@ static ArchiveOperation parseCommandLine
     case 'o': OriginalDates = true; break;
     case 's':
       Symtab = true;
+      MaybeJustCreateSymTab = true;
       break;
     case 'S':
       Symtab = false;
@@ -233,6 +237,13 @@ static ArchiveOperation parseCommandLine
   // Everything on the command line at this point is a member.
   getMembers();
 
+ if (NumOperations == 0 && MaybeJustCreateSymTab) {
+    NumOperations = 1;
+    Operation = CreateSymTab;
+    if (!Members.empty())
+      show_help("The s operation takes only an archive as argument");
+  }
+
   // Perform various checks on the operation/modifier specification
   // to make sure we are dealing with a legal request.
   if (NumOperations == 0)
@@ -340,6 +351,7 @@ static bool shouldCreateArchive(ArchiveO
   case Move:
   case DisplayTable:
   case Extract:
+  case CreateSymTab:
     return false;
 
   case QuickAppend:
@@ -810,6 +822,19 @@ static void performWriteOperation(Archiv
   TemporaryOutput = NULL;
 }
 
+static void createSymbolTable(object::Archive *OldArchive) {
+  // When an archive is created or modified, if the s option is given, the
+  // resulting archive will have a current symbol table. If the S option
+  // is given, it will have no symbol table.
+  // In summary, we only need to update the symbol table if we have none.
+  // This is actually very common because of broken build systems that think
+  // they have to run ranlib.
+  if (OldArchive->hasSymbolTable())
+    return;
+
+  performWriteOperation(CreateSymTab, OldArchive);
+}
+
 static void performOperation(ArchiveOperation Operation,
                              object::Archive *OldArchive) {
   switch (Operation) {
@@ -825,6 +850,9 @@ static void performOperation(ArchiveOper
   case ReplaceOrInsert:
     performWriteOperation(Operation, OldArchive);
     return;
+  case CreateSymTab:
+    createSymbolTable(OldArchive);
+    return;
   }
   llvm_unreachable("Unknown operation.");
 }





More information about the llvm-commits mailing list