Files
mkrtos-real/mkrtos_user/lib/mlibc/crt/reloc.c

67 lines
1.6 KiB
C
Raw Normal View History

2023-12-05 22:39:15 +08:00
/**
* @brief
*
*/
2023-08-23 00:42:36 +08:00
extern int __text_start__;
2025-02-01 12:26:56 +08:00
extern int __data_start__;
2023-08-23 00:42:36 +08:00
extern int __got_start__;
extern int __got_end__;
extern int __rel_start__;
extern int __rel_end__;
2025-02-01 12:26:56 +08:00
/**
* gbase:
* tbase: text的首地址
*/
2023-08-23 00:42:36 +08:00
void _reloc(unsigned int *gbase, unsigned int tbase)
{
int i;
unsigned int s;
unsigned int offset;
unsigned int gs, ge, ts;
unsigned int *rs, *re;
unsigned int *pointer;
2025-02-01 12:26:56 +08:00
unsigned int ds;
2023-08-23 00:42:36 +08:00
2025-02-01 12:26:56 +08:00
ds = (unsigned int)&__data_start__;
2023-08-23 00:42:36 +08:00
gs = (unsigned int)&__got_start__;
ge = (unsigned int)&__got_end__;
ts = (unsigned int)&__text_start__;
for (i = 0, s = gs; s < ge; s += 4, i++)
{
2025-02-01 12:26:56 +08:00
if (gbase[i] >= ds)
2023-08-23 00:42:36 +08:00
{
2025-02-01 12:26:56 +08:00
offset = gbase[i] - ds;
2023-08-23 00:42:36 +08:00
gbase[i] = offset + (unsigned int)gbase;
}
else
{
offset = gbase[i] - ts;
gbase[i] = offset + tbase;
}
}
rs = (unsigned int *)&__rel_start__;
re = (unsigned int *)&__rel_end__;
// rel
for (i = 0, s = (unsigned int)rs; s < (unsigned int)re; s += 8, i += 2)
{
if (rs[i + 1] == 0x00000017)
{
2025-02-01 12:26:56 +08:00
offset = rs[i] - ds;
2023-08-23 00:42:36 +08:00
pointer = (unsigned int *)((unsigned int)gbase + offset);
2025-02-01 12:26:56 +08:00
if (*pointer >= ds)
2023-08-23 00:42:36 +08:00
{
2025-02-01 12:26:56 +08:00
offset = *pointer - ds;
2023-08-23 00:42:36 +08:00
*pointer = offset + (unsigned int)gbase;
}
else
{
offset = *pointer - ts;
*pointer = offset + (unsigned int)tbase;
}
}
}
return;
}