Skip to content

Instantly share code, notes, and snippets.

@serge-sans-paille
Last active May 17, 2023 18:03
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save serge-sans-paille/c57d0791c7a9dbfc76e5c2f794e650b4 to your computer and use it in GitHub Desktop.
Save serge-sans-paille/c57d0791c7a9dbfc76e5c2f794e650b4 to your computer and use it in GitHub Desktop.
if(GlobalVariable* GA = M.getGlobalVariable("llvm.global.annotations")) {
// the first operand holds the metadata
for (Value *AOp : GA->operands()) {
// all metadata are stored in an array of struct of metadata
if (ConstantArray *CA = dyn_cast<ConstantArray>(AOp)) {
// so iterate over the operands
for (Value *CAOp : CA->operands()) {
// get the struct, which holds a pointer to the annotated function
// as first field, and the annotation as second field
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(CAOp)) {
if (CS->getNumOperands() >= 2) {
Function* AnnotatedFunction = cast<Function>(CS->getOperand(0)->getOperand(0));
// the second field is a pointer to a global constant Array that holds the string
if (GlobalVariable *GAnn =
dyn_cast<GlobalVariable>(CS->getOperand(1)->getOperand(0))) {
if (ConstantDataArray *A =
dyn_cast<ConstantDataArray>(GAnn->getOperand(0))) {
// we have the annotation! Check it's an epona annotation and process
StringRef AS = A->getAsString();
if (AS.startswith(Marker)) {
FuncAnnotations[AnnotatedFunction].emplace_back(AS);
}
}
}
}
}
}
}
}
}
@0x410c
Copy link

0x410c commented May 17, 2023

/usr/lib/llvm-16/include/llvm/IR/User.h:170: Value *llvm::User::getOperand(unsigned int) const: Assertion `i < NumUserOperands && "getOperand() out of range!"' failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment