Home | History | Annotate | Download | only in llvm-c-test
      1 /*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
      2 |*                                                                            *|
      3 |*                     The LLVM Compiler Infrastructure                       *|
      4 |*                                                                            *|
      5 |* This file is distributed under the University of Illinois Open Source      *|
      6 |* License. See LICENSE.TXT for details.                                      *|
      7 |*                                                                            *|
      8 |*===----------------------------------------------------------------------===*|
      9 |*                                                                            *|
     10 |* This file implements the --object-list-sections and --object-list-symbols  *|
     11 |* commands in llvm-c-test.                                                   *|
     12 |*                                                                            *|
     13 \*===----------------------------------------------------------------------===*/
     14 
     15 #include "llvm-c-test.h"
     16 #include "llvm-c/Object.h"
     17 #include <stdio.h>
     18 #include <stdlib.h>
     19 
     20 int llvm_object_list_sections(void) {
     21   LLVMMemoryBufferRef MB;
     22   LLVMObjectFileRef O;
     23   LLVMSectionIteratorRef sect;
     24   char *msg = NULL;
     25 
     26   if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
     27     fprintf(stderr, "Error reading file: %s\n", msg);
     28     exit(1);
     29   }
     30 
     31   O = LLVMCreateObjectFile(MB);
     32   if (!O) {
     33     fprintf(stderr, "Error reading object\n");
     34     exit(1);
     35   }
     36 
     37   sect = LLVMGetSections(O);
     38   while (!LLVMIsSectionIteratorAtEnd(O, sect)) {
     39     printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
     40            LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
     41 
     42     LLVMMoveToNextSection(sect);
     43   }
     44 
     45   LLVMDisposeSectionIterator(sect);
     46 
     47   LLVMDisposeObjectFile(O);
     48 
     49   return 0;
     50 }
     51 
     52 int llvm_object_list_symbols(void) {
     53   LLVMMemoryBufferRef MB;
     54   LLVMObjectFileRef O;
     55   LLVMSectionIteratorRef sect;
     56   LLVMSymbolIteratorRef sym;
     57   char *msg = NULL;
     58 
     59   if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
     60     fprintf(stderr, "Error reading file: %s\n", msg);
     61     exit(1);
     62   }
     63 
     64   O = LLVMCreateObjectFile(MB);
     65   if (!O) {
     66     fprintf(stderr, "Error reading object\n");
     67     exit(1);
     68   }
     69 
     70   sect = LLVMGetSections(O);
     71   sym = LLVMGetSymbols(O);
     72   while (!LLVMIsSymbolIteratorAtEnd(O, sym)) {
     73 
     74     LLVMMoveToContainingSection(sect, sym);
     75     printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
     76            LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym),
     77            LLVMGetSectionName(sect));
     78 
     79     LLVMMoveToNextSymbol(sym);
     80   }
     81 
     82   LLVMDisposeSymbolIterator(sym);
     83 
     84   LLVMDisposeObjectFile(O);
     85 
     86   return 0;
     87 }
     88