Skip to content

Instantly share code, notes, and snippets.

@MichaReiser
Last active August 8, 2022 13:20
Show Gist options
  • Save MichaReiser/a15a9af8856d17d2456f02795f80d6f1 to your computer and use it in GitHub Desktop.
Save MichaReiser/a15a9af8856d17d2456f02795f80d6f1 to your computer and use it in GitHub Desktop.
LLVM CreateGlobalStringPtr segmentation fault
clang++-7 -g -O3 main.cpp `llvm-config-7 --cxxflags --ldflags --system-libs --libs all` -o main
#include <iostream>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/Support/raw_os_ostream.h>
int main() {
llvm::raw_os_ostream raw_os_ostream { std::cout };
llvm::LLVMContext context {};
llvm::Module* module = new llvm::Module { "test", context };
auto* builder = new llvm::IRBuilder<> { context };
auto* fnType = llvm::FunctionType::get(llvm::Type::getVoidTy(context), false);
auto* fn = llvm::Function::Create(fnType, llvm::GlobalValue::LinkageTypes::ExternalWeakLinkage, "main", module);
auto* entry = llvm::BasicBlock::Create(context, "entry", fn);
builder->SetInsertPoint(entry);
std::cout << "CreateGlobalString" << std::endl;
auto* globalString = builder->CreateGlobalString("Hello World", "str1", 0);
globalString->print(raw_os_ostream);
std::cout << std::endl << "CreateGlobalStringPtr" << std::endl;
auto* globalStringPtr = builder->CreateGlobalStringPtr("Hello World", "str2", 0);
globalStringPtr->print(raw_os_ostream);
delete module;
delete builder;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment