How to create a global pointer variable for a custom structure in llvm - llvm

I define a structure in the header file types.h:
typedef struct{
char str [10].
void *p.
}for_test;
In Pass, I first refer to the header file. I want to create a global pointer in pass to access the structured array.
#include ".. /types.h"
...
static StructType *fp_type = llvm::StructType::getTypeByName(C,"for_test");
GlobalVariable *F_Pointer = new GlobalVariable(
M, PointerType::get (fp_type, 0), false,
GlobalValue::ExternalLinkage, 0, "f_p");
However, PointerType::get (fp_type,0) has an error and I don’t know how to fix it.
Error:

Related

Pointer in function argument seems not working

I'm developing in C++. I have 2 objects : which are the StandaloneAgent and the ConfigManager.
I want from the Standalone Agent to reach a char * variable in the ConfigManager with a function .
int32_t StandaloneAgent::getConfigFile(char * file){
int32_t code = mConfigMgr->getConfigFile(file);
return code;
}
int32_t ConfigManager::getConfigFile(char * file){
file = mConfigFile;
return 0;
}
Here, the ConfigManager objet is an attribute of the StandaloneAgent with the
ConfigManager * mConfigMgr; variable.
And the file I want to reach is located in the mConfigFile attribute of the ConfigManager.
The problem is :
While putting std::cout,
the mConfigFile in the is OK and
the file variable after the
file = mConfigFile;
is OK
But when I return in the getConfigFile() function of the standalone agent, the file variable is a null pointer and I don't know why.
Is there a C++ specification that I am missing ?
Arguments to functions are either passed by-value or by-reference. Any variable passed by-value will be local to the function and any changes made to it is local to the function too. This means that when to assign a new value to the pointer (file = mConfigFile) you only change the pointer in the function. The pointer you passed as an argument is unaffected.
When taking arguments by-reference, the variable inside the function references the variable used to call the function. Any changes made to that variable will be directly made to the variable used in the call to the function.
In your case, that means that you need to change the ConfigManager::getConfigFile function:
int32_t ConfigManager::getConfigFile(char*& file){
file = mConfigFile; // the change is made to the variable used as an argument
return 0;
}
If the pointer passed to StandaloneAgent::getConfigFile is also supposed to be changed (I assume it is), you need the same change there:
int32_t StandaloneAgent::getConfigFile(char*& file){
int32_t code = mConfigMgr->getConfigFile(file);
return code;
}

Any way to grab struct from DLL using dlsym (Dynamic Linking) without header file (.h)?

I have been looking everywhere for this question, but I cannot seem to get the answer for this.
So, every example I have been to is simply how to grab function pointers from SO file, which I have done. However, let just say I have this compiled as my .SO file:
(these codes compiled into libsampleso.so):
sampleso.hpp:
#IFNDEF SAMPLESO_HPP__
#DEFINE SAMPLESO_HPP__
struct carDescriptor
{
const char* model;
int prod_year;
int wheel_num;
const char* license_plate;
const char* colour;
} car;
carDescriptor fillCarID(void);
#ENDIF
sampleso.cpp:
#include "sampleso.hpp"
//Struct car fillup function
carDescriptor fillCarID(void)
{
car.model = "Ford Fiesta";
car.prod_year = 2014;
car.wheel_num = 4;
car.colour = "Midnight Blue";
car.license_plate = "D 1234 ABC";
return car;
}
now, compiled, I've got these out of objdump -T libsampleso.so: car (the struct) and _Z9fillCarIDv (the function).
if the return value of _Z9fillCarIDv() (aka fillCarID()) is normal datatype (e.g. string or int), it's easy with dlsym. However, for this I need to cast the struct first and then the function. I've tried creating an empty struct, and then cast dlsym to it:
struct carDesc * cars;
cars = reinterpret_cast<struct carDesc*>(dlsym(hHandler,"car"));
but seems to be impossible to use that as the datatype for the function:
carDesc(*__fn_fillCarIDv)(void);
__fn_fillCarIDv = reinterpret_cast<carDesc(*)(void)>(dlsym(hHandler,"_Z9fillCarIDv"));
cars = __fn_fillCarIDv(); //doesn't work, error: function returns incomplete type "carDesc" C/C++(862)
Anyone ever tried to grab a struct using dlsym without .h file?
Also, how do you deal with the elements from the struct grabbed by dlsym? I know that you don't have the proper elements listed without .h file, and obviously some pointer shifting is necessary, but can anyone give me an example of how to do it?
Thanks

Can one print the QualType of function pointers with a name using Clang AST?

Is there any easy and reliable way (a.k.a. not regex) to get the QualType of a function pointer declaration as a string but with a name attached to it? I tried looking into leveraging QualType::getAsString(const PrintingPolicy &Policy), but unfortunately, there is no option with that configuration.
e.g.
int (*)(void) ----> int (*whatever_name_here)(void)
You can create VarDecl and print that.
Assuming ctx is your ASTContext and type is your QualType, here is what you can do:
// Create identifier.
IdentifierInfo id = ctx.Idents.get("whatever_name_here");
// Create variable declaration.
VarDecl *vd = VarDecl::Create(ctx, ctx.getTranslationUnitDecl(),
SourceLocation(), SourceLocation(), &id, type, nullptr, StorageClass:SC_None);
// Print it.
vd->print(/* put your llvm::raw_stream here */, ctx.getPrintingPolicy());

How to initialize a class type used as a function parameter

I am trying to get a handle on HDL to C++ conversions and have hit a bit of a snag.
The conversion using Verilator on Ubuntu is easy enough but one data type is annoying me.
The top code in the hierarchy is...
#include <iostream>
#include "VDorQ24Syms.h"
#include "VDorQ24.h"
using namespace std;
// FUNCTIONS
VDorQ24Syms::VDorQ24Syms(VDorQ24* topp, const char* namep)
// Setup locals
: vm_namep(namep)
, vm_activity(false)
, vm_didInit(false)
// Setup submodule names
{
// Pointer to top level
tOPp = topp;
// Setup each module's pointers to their submodules
// Setup each module's pointer back to symbol table (for public functions)
tOPp->Vconfigure(this, true);
// Setup scope names
}
Passing data to the function
VDorQ24Syms::VDorQ24Syms(VDorQ24* topp, const char* namep)
is what I'm not getting. The second parameter is easy to understand. The first, not so much.
By this I mean, what is the compiler expecting me to pass? Which data type?
I want to pass data like so...
VDorQ24* randomCharacter;
if (VDorQ24Syms(randomCharacter, szAscii) == /*condition*/)
{
return /*value*/;
}
But 'randomCharacter' is uninitialized.
VDorQ24* randomCharacter = /*How do I initialize this?*/;
You example is not complete, but this might help you.
Your variable randomCharacter is not an instance of your class VdorQ24, it's a pointer to your class.
If you want to initialize your variable, set it to nullptr:
VdorQ24* randomCharacter = nullptr; // now you can be 100% certain that it's null.
If you acually wanted to create a new instance of VdorQ24, you can simply forget about the pointer and use values. Here we call the default constructor:
// Not a pointer, initialize the instance of your class directly.
VDorQ24 randomCharacter;
// v---- Here we send a pointer to your variable using the '&' operator.
if (VDorQ24Syms(&randomCharacter, szAscii) == /*condition*/)
{
return /*value*/;
}
If you want to send parameter to the constructor, you can use this syntax:
VDorQ24 randomCharacter{param1, param2};
In fact, any type can be initialized with this syntax, even int and arrays:
int a{1};
float b[] = {1.f, 2.f, 3.f};

C++ - What do these lines of code mean?

I'm looking at some source code and don't understand what is going on. Here is some code I've put together from that source code (definitions came from all over the place and I've included only what's necessary):
#define TOC 0x1C75288
typedef unsigned int uint32_t;
typedef unsigned int uint;
struct opd_s
{
uint32_t Sub;
uint32_t Toc;
};
namespace Offsets{
enum Address{
GET_PLAYER_NAME = 0x421974
};
}
opd_s GET_PLAYER_NAME_t = { Offsets::GET_PLAYER_NAME, TOC };
char*(*GET_PLAYER_NAME)(uint PlayerID) = (char*(*)(uint))&GET_PLAYER_NAME_t;
Specifically, what do these last 2 lines mean and do? :
opd_s GET_PLAYER_NAME_t = { Offsets::GET_PLAYER_NAME, TOC };
char*(*GET_PLAYER_NAME)(uint PlayerID) = (char*(*)(uint))&GET_PLAYER_NAME_t;
Later in the source code I see a usage of GET_PLAYER_NAME and it looks like this:
char* player = GET_PLAYER_NAME(0);
So is GET_PLAYER_NAME some sort of a function that takes an argument of an integer?
I'm just really confused about this and am trying to understand it, so if someone could exaplain the meaning and syntax, that would be extremely helpful. Thanks!
opd_s GET_PLAYER_NAME_t = { Offsets::GET_PLAYER_NAME, TOC };
means create struct variable on stack with name GET_PLAYER_NAME_t of type opd_s and initialize it with fields Sub = Offsets::GET_PLAYER_NAME, i.e. 0x421974 and Toc = TOC, i.e. 0x1C75288.
So, there is a struct named GET_PLAYER_NAME_t with opd_s type, which is equal to { 0x421974, 0x1C75288 }.
char*(*GET_PLAYER_NAME)(uint PlayerID) = (char*(*)(uint))&GET_PLAYER_NAME_t;
It defines function pointer GET_PLAYER_NAME(uint PlayerID) which is pointed to former declared struct.
Actually GET_PLAYER_NAME(0) calls something with op-codes 0x421974, 0x1C75288, which we cannot know what it does, because we don't know the architecture it is compiled for (at least bitness and endianness of the architecture).
Surely it's not x86, DEP on x86 blocks executing stack data as code.
opd_s GET_PLAYER_NAME_t = { Offsets::GET_PLAYER_NAME, TOC };
This declares a variable named GET_PLAYER_NAME_t of type opd_s. This is initialized to { Offsets::GET_PLAYER_NAME, TOC } i.e. the Sub data member will be Offsets::GET_PLAYER_NAME and the Toc data member will be TOC.
char*(*GET_PLAYER_NAME)(uint PlayerID) = ...
This declares a variable named GET_PLAYER_NAME. Its type is: pointer to a function taking an uint as argument and returning `char *.
(char*(*)(uint))&GET_PLAYER_NAME_t;
This casts the adress of GET_PLAYER_NAME_t to a pointer ... see above.
This looks very suspicious as the contents of the GET_PLAYER_NAME_t variable which is a struct will be interpreted as the first instruction(s) in a function call via GET_PLAYER_NAME.
This is how functions are called on PS3 via a prx module. This code runs on an external plugin know as a prx module. Think of it kind of like a DLL. You can load the game's executable into IDA and get the address of the function you want to call. So in this case, 0x421974 is being called. Since we don't actually have the game's source, you need to define the function like this:
char*(*GET_PLAYER_NAME)(uint PlayerID) = (char*(*)(uint))&GET_PLAYER_NAME_t;
The opd structure is just an int array which is specific to the cell processor.
On Xbox it would just be like this:
char*(*GET_PLAYER_NAME)(uint PlayerID) = (char*(*)(uint))0x421974;
Pretty much all it does is call 0x421974 which is GET_PLAYER_NAME on GTA5 and gets a players name from their client index.
The processor is powerpc.