CNUproject/코드 동일성 검사 도구

15_LLVM 플러그인 적용 오류

YUMMMM 2023. 8. 31. 09:37

makefile

LIBRARY=NamePrinter.so
SOURCES=NamePrinter.cpp NamePrinter.cpp


ifneq ($(LIBRARY),)
EXTRA_FLAGS=-fpic -fno-rtti
endif

OBJECTS=$(SOURCES:.cpp=.o)

.PHONY: all

all: $(LIBRARY) $(EXECUTABLE)

$(LIBRARY): $(OBJECTS)
	clang++ -shared -o $@ $^

$(EXECUTABLE): $(OBJECTS)
	clang++ $^ -o $@ `llvm-config --ldflags --libs --system-libs` 

%.o: %.cpp
	clang++ -c $(EXTRA_FLAGS) `llvm-config --cppflags` $^ -o $@

.PHONY: clean

clean:
	rm -f $(LIBRARY) $(EXECUTABLE) $(OBJECTS)

 

NamePrinter.cpp

//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements two versions of the LLVM "Hello World" pass described
// in docs/WritingAnLLVMPass.html
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Debug.h"
#include "NamePrinter.h"

#define DEBUG_TYPE "hello"

bool NamePrinter::runOnFunction(Function &F) {
  dbgs() <<"Hello: "<< F.getName()<<"\n";
  return false;
}

void NamePrinter::getAnalysisUsage(AnalysisUsage &AU) const {
  AU.setPreservesAll();
}

char NamePrinter::ID = 0;
static RegisterPass<NamePrinter> Y("NamePrinter", "Hello World Pass ");

 

NamePrinter.h

#ifndef LLVM_TUTORIAL_OPTIMIZATION_TEMPLATE_HELLO_FUNCTION_H
#define LLVM_TUTORIAL_OPTIMIZATION_TEMPLATE_HELLO_FUNCTION_H

#include "llvm/IR/Function.h"
#include "llvm/Pass.h"

using namespace llvm;

namespace {
  struct NamePrinter : public FunctionPass {
    static char ID; // Pass identification, replacement for typeid
    NamePrinter() : FunctionPass(ID) {}

    bool runOnFunction(Function &M) override;

    void getAnalysisUsage(AnalysisUsage &AU) const override;
  };
}

#endif

 

test.c

#include <stdio.h>
int add(int x, int y) {
    return x + y;
}

int main() {
    int x = 1;
    int y = 2;
    return add(x, y);
}

 

간단히 c파일에서 함수 이름을 출력해주는 패스를 적용하여 실행하는 것을 해보려고 하였으나, .so 파일을 만드는 과정에서 오류가 발생한다.

 

아직 해결방법을 알아내지 못 했다. 아키텍처쪽 문제인 것 같다.