0

I'm using LLVM api in order to parse bitcode files. I have the following snippet and I'm using this command to generate the bitcode $CC -emit-llvm -c -g source.c where CC is set to the clang path.

#include <stdio.h>

struct Point {
    int a;
    int b;
};

int func_0(struct Point p, int x) {
    return 0;
}

The TypeID is supposed to have a numeric value, based on the type of the parameter. However, both for the integer x and the struct Point I obtain the value of 10 which is referred as a TokenTyID. So, I decided to use the functions isIntegerTy() and isStructTy(), respectively, to see if at least in this case, I obtain the right result. This solution works for the integer parameter x, but not for the struct. How can I correctly identify structs and read their fields?

Just to completeness, to parse the bitcode I use this code:

using namespace llvm;

int main(int argc, char** argv) {
    LLVMContext context;
    OwningPtr<MemoryBuffer> mb;
    MemoryBuffer::getFile(FileName, mb);
    Module *m = ParseBitcodeFile(mb.get(), context);

    for (Module::const_iterator i = m->getFunctionList().begin(), e = m->getFunctionList().end(); i != e; ++i) {
        if (i->isDeclaration() || i->getName().str() == "main")
            continue;

        std::cout << i->getName().str() << std::endl;

        Type* ret_type = i->getReturnType();
        std::cout << "\t(ret) " << ret_type->getTypeID() << std::endl;

        Function::const_arg_iterator ai;
        Function::const_arg_iterator ae;

        for (ai = i->arg_begin(), ae = i->arg_end(); ai != ae; ++ai) {
            Type* t = ai->getType();

            std::cout << "\t" << ai->getName().str() << " " << t->getTypeID()
                      << "(" << t->getFunctionNumParams() << ")"
                      << " is struct? " << (t->isStructTy() ? "Y" : "N")
                      << " is int? " << (t->isIntegerTy() ? "Y" : "N")
                      << "\n";
        }
    }

    return 0;
}

I read this post Why does Clang coerce struct parameters to ints about the translation performed by clang with the structs and I'm pretty sure that is my same problem.

Community
  • 1
  • 1
Surcle
  • 572
  • 1
  • 5
  • 15
  • Tried dumping LLVM IR using `clang -S -emit-llvm`? – arrowd Apr 04 '17 at 05:48
  • Yes, it does not work as well. If I generate the IR code with the "-S" option and then generate the bitcode with "llvm-as", the problem is the same. – Surcle Apr 04 '17 at 09:18
  • I mean, try looking at generated IR and find out what your structure is turned into. – arrowd Apr 04 '17 at 09:23
  • The generated IR is the same. Data structures are replaced like I explained in my first post. – Surcle Apr 04 '17 at 09:28

1 Answers1

0

Since clang changes the function signature in the IR, you will have to get that information using debug info. Here is some rough code:

DITypeIdentifierMap TypeIdentifierMap;

DIType* getLowestDINode(DIType* Ty) {
  if (Ty->getTag() == dwarf::DW_TAG_pointer_type ||
      Ty->getTag() == dwarf::DW_TAG_member) {
    DIType *baseTy =
      dyn_cast<DIDerivedType>(Ty)->getBaseType().resolve(TypeIdentifierMap);
    if (!baseTy) {
      errs() << "Type : NULL - Nothing more to do\n";
      return NULL;
    }

    //Skip all the DINodes with DW_TAG_typedef tag
    while ((baseTy->getTag() == dwarf::DW_TAG_typedef || baseTy->getTag() == dwarf::DW_TAG_const_type
          || baseTy->getTag() == dwarf::DW_TAG_pointer_type)) {
      if (DITypeRef temp = dyn_cast<DIDerivedType>(baseTy)->getBaseType())
        baseTy = temp.resolve(TypeIdentifierMap);
      else
        break;
    } 
    return baseTy;
  }
  return Ty;
}

int main(int argc, char** argv) {
  LLVMContext context;
  OwningPtr<MemoryBuffer> mb;
  MemoryBuffer::getFile(FileName, mb);
  Module *m = ParseBitcodeFile(mb.get(), context);
  if (NamedMDNode *CU_Nodes = m.getNamedMetadata("llvm.dbg.cu")) {
    TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
  }
  SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
  F.getAllMetadata(MDs);
  for (auto &MD : MDs) {
    if (MDNode *N = MD.second) {
      if (auto *subRoutine = dyn_cast<DISubprogram>(N)->getType()) {
        if (!subRoutine->getTypeArray()[0]) {
          errs() << "return type \"void\" for Function : " << F.getName().str()
            << "\n";
        }

        const auto &TypeRef = subRoutine->getTypeArray();
        for (int i=0; i<TypeRef.size(); i++) {
          // Resolve the type
          DIType *Ty = ArgTypeRef.resolve(TypeIdentifierMap);
          DIType* baseTy = getLowestDINode(Ty);
          if (!baseTy)
            return;
          // If that pointer is a struct
          if (baseTy->getTag() == dwarf::DW_TAG_structure_type) {
            std::cout << "structure type name: " << baseTy->getName().str() << std::endl();
          }
        }
      }
    }
  }
}

I know it looks ugly but using debug info is not easy.

deLta
  • 561
  • 2
  • 12
  • Thank you for your reply. Could you give me the whole code of the previous statements? – Surcle Apr 04 '17 at 09:15
  • Sure, here is a link to the code: https://github.com/jiten-thakkar/DataStructureAnalysis/blob/dsa_llvm3.8/lib/dsaGenerator/DSAGenerator.cpp I have been working on this for some other project – deLta Apr 04 '17 at 16:29