I have a problem with linking class to specific memory region via Linker Script
I've figure out how to link variables and functions that are out of the class but I have no idea how to link the class into the memory region specified in the linker script
My linker script is very simple:
SECTIONS
{
. = 0x1000000;
.text : { *(.text) }
. = 0x8000000;
.data : { *(.data) }
.bss : { *(.bss) }
}
And the main code:
#include <iostream> // system
#include "mem.h"
using namespace std;
unsigned int A = 0xFFFF1234;
unsigned int B = 0x00001234;
void Function_A(void)
{
printf("This is execution of the void Function A \n");
}
void Function_B(int x)
{
x = x 1;
printf("This is execution of the void Function B with variable x:%d \n",x);
}
int main(int argc, char* argv[])
{
system("clear");
unsigned int * pA = &A;
unsigned int * pB = &B;
printf("Original Data A ---> 0xx\n",A);
printf("Original Data B ---> 0xx\n\n",B);
printf("Pointer to A variable ---> %p\n",pA);
printf("Pointer to B variable ---> %p\n\n",pB);
void (*pFunction_A)(void) = &Function_A;
void (*pFunction_B)(int) = &Function_B;
MemAssembly * pMemAssembly;
pMemAssembly->MemDump();
// Testing function pointers
pFunction_A();
pFunction_B(100);
(*pFunction_B)(200);
printf("Pointer to Function_A ---> %p\n",pFunction_A);
printf("Pointer to Function_B ---> %p\n\n",pFunction_B);
// Address is not linked !!!????
printf("Pointer to MemAssembly Class ---> %p\n\n",pMemAssembly);
return 0;
}
Now, when I execute I get this:
Original Data A ---> 0xffff1234
Original Data B ---> 0x00001234
Pointer to A variable ---> 0x8000004
Pointer to B variable ---> 0x8000008
RAX_Accumulator ---> Read A : 0x00000000ffff1234
RAX_Accumulator ---> Read B : 0x0000000000001234
This is execution of the void Function A
This is execution of the void Function B with variable x:101
This is execution of the void Function B with variable x:201
Pointer to Function_A ---> 0x10000e9
Pointer to Function_B ---> 0x1000103
Pointer to MemAssembly Class ---> 0x7f282ab46764
The mem.h looks as follows:
#ifndef MEM_H
#define MEM_H
#include <inttypes.h>
extern "C" unsigned long _MEMORY_READ_A(void);
extern "C" unsigned long _MEMORY_READ_B(void);
class MemAssembly{
public:
MemAssembly();
~MemAssembly();
int AssemblyMemoryDump(void);
void MemDump(void)
{
printf("RAX_Accumulator ---> Read A : %#018" PRIx64 " \n",_MEMORY_READ_A());
printf("RAX_Accumulator ---> Read B : %#018" PRIx64 " \n\n",_MEMORY_READ_B());
}
};
#endif /* MEM_H */
And the assemblies
section .text
global _MEMORY_READ_A
_MEMORY_READ_A:
mov eax, [0x08000000 4]
ret
global _MEMORY_READ_B
_MEMORY_READ_B:
mov eax, [0x08000000 8]
ret
So my question is: How should I modify linker script, mem.h or the assemblies in order to load my class into specific memory region rather than using dynamic allocation ???
Instead of this one:
Pointer to MemAssembly Class ---> 0x7f282ab46764
Read address specified in the linker script ???
CodePudding user response:
Thanks Peter for the help
Here what I did:
Added MemAssembly section in ASM
section .MemAssembly
global global_MemAssembly
global_MemAssembly:
Link it with 0xb000000
SECTIONS
{
. = 0x1000000;
.text : { *(.text) }
. = 0x8000000;
.data : { *(.data) }
.bss : { *(.bss) }
. = 0xB000000;
.MemAssembly : { *(MemAssembly)}
}
Using Test class
class MemAssembly{
public:
MemAssembly();
~MemAssembly();
int x,y,z,t;
long a,b,c,d;
long long dx,dy,dz,dt;
void MemDump(void)
{
printf("x ---> %p y ---> %p z ---> %p t ---> %p\n", &x, &y, &z, &t);
printf("a ---> %p b ---> %p c ---> %p d ---> %p\n", &a, &b, &c, &d);
printf("dx ---> %p dy ---> %p dz ---> %p dt ---> %p\n\n", &dx, &dy, &dz, &dt);
}
};
When I call MemDump() using usign dynamic:
MemAssembly * pMemAssembly
I get:
x ---> 0x7ffd7cc36040 y ---> 0x7ffd7cc36044 z ---> 0x7ffd7cc36048 t ---> 0x7ffd7cc3604c
a ---> 0x7ffd7cc36050 b ---> 0x7ffd7cc36058 c ---> 0x7ffd7cc36060 d ---> 0x7ffd7cc36068
dx ---> 0x7ffd7cc36070 dy ---> 0x7ffd7cc36078 dz ---> 0x7ffd7cc36080 dt ---> 0x7ffd7cc36088
But when I've used:
extern MemAssembly global_MemAssembly;
MemAssembly * pMemAssembly = &global_MemAssembly;
I get:
x ---> 0xb000000 y ---> 0xb000004 z ---> 0xb000008 t ---> 0xb00000c
a ---> 0xb000010 b ---> 0xb000018 c ---> 0xb000020 d ---> 0xb000028
dx ---> 0xb000030 dy ---> 0xb000038 dz ---> 0xb000040 dt ---> 0xb000048
Which is what I've expected