[llvm] r358037 - [LLVM-C] Add Section and Symbol Iterator Accessors for Object File Binaries

Robert Widmann via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 9 14:53:31 PDT 2019


Author: codafi
Date: Tue Apr  9 14:53:31 2019
New Revision: 358037

URL: http://llvm.org/viewvc/llvm-project?rev=358037&view=rev
Log:
[LLVM-C] Add Section and Symbol Iterator Accessors for Object File Binaries

Summary: This brings us to full feature parity with the old API, so I've deprecated it and updated the tests.  I'll do a follow-up patch to do some more cleanup and documentation work in this header.

Reviewers: whitequark, deadalnix

Reviewed By: whitequark

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D60407

Modified:
    llvm/trunk/include/llvm-c/Object.h
    llvm/trunk/lib/Object/Object.cpp
    llvm/trunk/tools/llvm-c-test/object.c

Modified: llvm/trunk/include/llvm-c/Object.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Object.h?rev=358037&r1=358036&r2=358037&view=diff
==============================================================================
--- llvm/trunk/include/llvm-c/Object.h (original)
+++ llvm/trunk/include/llvm-c/Object.h Tue Apr  9 14:53:31 2019
@@ -34,7 +34,6 @@ extern "C" {
  */
 
 // Opaque type wrappers
-typedef struct LLVMOpaqueObjectFile *LLVMObjectFileRef;
 typedef struct LLVMOpaqueSectionIterator *LLVMSectionIteratorRef;
 typedef struct LLVMOpaqueSymbolIterator *LLVMSymbolIteratorRef;
 typedef struct LLVMOpaqueRelocationIterator *LLVMRelocationIteratorRef;
@@ -103,25 +102,56 @@ LLVMMemoryBufferRef LLVMBinaryCopyMemory
  */
 LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR);
 
+/**
+ * Retrieve a copy of the the section iterator for this object file.
+ *
+ * If there are no sections, the result is NULL.
+ *
+ * The returned iterator is merely a shallow copy. Nevertheless, it is
+ * the responsibility of the caller to free it with
+ * \c LLVMDisposeSectionIterator.
+ *
+ * @see llvm::object::sections()
+ */
+LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR);
 
-// ObjectFile creation
-LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf);
-void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile);
+/**
+ * Returns whether the given section iterator is at the end.
+ *
+ * @see llvm::object::section_end
+ */
+LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
+                                              LLVMSectionIteratorRef SI);
+
+/**
+ * Retrieve a copy of the the symbol iterator for this object file.
+ *
+ * If there are no symbols, the result is NULL.
+ *
+ * The returned iterator is merely a shallow copy. Nevertheless, it is
+ * the responsibility of the caller to free it with
+ * \c LLVMDisposeSymbolIterator.
+ *
+ * @see llvm::object::symbols()
+ */
+LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR);
+
+/**
+ * Returns whether the given symbol iterator is at the end.
+ *
+ * @see llvm::object::symbol_end
+ */
+LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
+                                             LLVMSymbolIteratorRef SI);
 
-// ObjectFile Section iterators
-LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile);
 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI);
-LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
-                                LLVMSectionIteratorRef SI);
+
 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI);
 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
                                  LLVMSymbolIteratorRef Sym);
 
 // ObjectFile Symbol iterators
-LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile);
 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI);
-LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
-                                LLVMSymbolIteratorRef SI);
 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI);
 
 // SectionRef accessors
@@ -154,6 +184,28 @@ uint64_t LLVMGetRelocationType(LLVMReloc
 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI);
 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI);
 
+/** Deprecated: Use LLVMBinaryRef instead. */
+typedef struct LLVMOpaqueObjectFile *LLVMObjectFileRef;
+
+/** Deprecated: Use LLVMCreateBinary instead. */
+LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf);
+
+/** Deprecated: Use LLVMDisposeBinary instead. */
+void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile);
+
+/** Deprecated: Use LLVMObjectFileCopySectionIterator instead. */
+LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile);
+
+/** Deprecated: Use LLVMObjectFileIsSectionIteratorAtEnd instead. */
+LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
+                                    LLVMSectionIteratorRef SI);
+
+/** Deprecated: Use LLVMObjectFileCopySymbolIterator instead. */
+LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile);
+
+/** Deprecated: Use LLVMObjectFileIsSymbolIteratorAtEnd instead. */
+LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
+                                   LLVMSymbolIteratorRef SI);
 /**
  * @}
  */

Modified: llvm/trunk/lib/Object/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/Object.cpp?rev=358037&r1=358036&r2=358037&view=diff
==============================================================================
--- llvm/trunk/lib/Object/Object.cpp (original)
+++ llvm/trunk/lib/Object/Object.cpp Tue Apr  9 14:53:31 2019
@@ -131,6 +131,34 @@ LLVMBinaryType LLVMBinaryGetType(LLVMBin
   return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType());
 }
 
+LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR) {
+  auto OF = cast<ObjectFile>(unwrap(BR));
+  auto sections = OF->sections();
+  if (sections.begin() == sections.end())
+    return nullptr;
+  return wrap(new section_iterator(sections.begin()));
+}
+
+LLVMBool LLVMObjectFileIsSectionIteratorAtEnd(LLVMBinaryRef BR,
+                                              LLVMSectionIteratorRef SI) {
+  auto OF = cast<ObjectFile>(unwrap(BR));
+  return (*unwrap(SI) == OF->section_end()) ? 1 : 0;
+}
+
+LLVMSymbolIteratorRef LLVMObjectFileCopySymbolIterator(LLVMBinaryRef BR) {
+  auto OF = cast<ObjectFile>(unwrap(BR));
+  auto symbols = OF->symbols();
+  if (symbols.begin() == symbols.end())
+    return nullptr;
+  return wrap(new symbol_iterator(symbols.begin()));
+}
+
+LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
+                                             LLVMSymbolIteratorRef SI) {
+  auto OF = cast<ObjectFile>(unwrap(BR));
+  return (*unwrap(SI) == OF->symbol_end()) ? 1 : 0;
+}
+
 // ObjectFile creation
 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
   std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));

Modified: llvm/trunk/tools/llvm-c-test/object.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/object.c?rev=358037&r1=358036&r2=358037&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/object.c (original)
+++ llvm/trunk/tools/llvm-c-test/object.c Tue Apr  9 14:53:31 2019
@@ -19,23 +19,26 @@
 
 int llvm_object_list_sections(void) {
   LLVMMemoryBufferRef MB;
-  LLVMObjectFileRef O;
+  LLVMBinaryRef O;
   LLVMSectionIteratorRef sect;
-  char *msg = NULL;
 
-  if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
-    fprintf(stderr, "Error reading file: %s\n", msg);
+  char *outBufferErr = NULL;
+  if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
+    fprintf(stderr, "Error reading file: %s\n", outBufferErr);
+    free(outBufferErr);
     exit(1);
   }
 
-  O = LLVMCreateObjectFile(MB);
-  if (!O) {
-    fprintf(stderr, "Error reading object\n");
+  char *outBinaryErr = NULL;
+  O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
+  if (!O || outBinaryErr) {
+    fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
+    free(outBinaryErr);
     exit(1);
   }
 
-  sect = LLVMGetSections(O);
-  while (!LLVMIsSectionIteratorAtEnd(O, sect)) {
+  sect = LLVMObjectFileCopySectionIterator(O);
+  while (sect && !LLVMObjectFileIsSectionIteratorAtEnd(O, sect)) {
     printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
            LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
 
@@ -44,32 +47,37 @@ int llvm_object_list_sections(void) {
 
   LLVMDisposeSectionIterator(sect);
 
-  LLVMDisposeObjectFile(O);
+  LLVMDisposeBinary(O);
+
+  LLVMDisposeMemoryBuffer(MB);
 
   return 0;
 }
 
 int llvm_object_list_symbols(void) {
   LLVMMemoryBufferRef MB;
-  LLVMObjectFileRef O;
+  LLVMBinaryRef O;
   LLVMSectionIteratorRef sect;
   LLVMSymbolIteratorRef sym;
-  char *msg = NULL;
 
-  if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
-    fprintf(stderr, "Error reading file: %s\n", msg);
+  char *outBufferErr = NULL;
+  if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
+    fprintf(stderr, "Error reading file: %s\n", outBufferErr);
+    free(outBufferErr);
     exit(1);
   }
 
-  O = LLVMCreateObjectFile(MB);
-  if (!O) {
-    fprintf(stderr, "Error reading object\n");
+  char *outBinaryErr = NULL;
+  O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
+  if (!O || outBinaryErr) {
+    fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
+    free(outBinaryErr);
     exit(1);
   }
 
-  sect = LLVMGetSections(O);
-  sym = LLVMGetSymbols(O);
-  while (!LLVMIsSymbolIteratorAtEnd(O, sym)) {
+  sect = LLVMObjectFileCopySectionIterator(O);
+  sym = LLVMObjectFileCopySymbolIterator(O);
+  while (sect && sym && !LLVMObjectFileIsSymbolIteratorAtEnd(O, sym)) {
 
     LLVMMoveToContainingSection(sect, sym);
     printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
@@ -81,7 +89,9 @@ int llvm_object_list_symbols(void) {
 
   LLVMDisposeSymbolIterator(sym);
 
-  LLVMDisposeObjectFile(O);
+  LLVMDisposeBinary(O);
+
+  LLVMDisposeMemoryBuffer(MB);
 
   return 0;
 }




More information about the llvm-commits mailing list