[C] Problem with read value of ptr by calculated ptr
Posted: Sun Nov 01, 2020 6:34 am
Code: Select all
/*
KISS'EM - a little operating system.
Copyright (C) 2018 Sławomir Lach <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/
#include "../Include/types.h"
#define YES 1
#define NO 0
// For nice mode only
// Mixed mode is provided to allow handle problems before messages subsystem was initialized. You must take in mind, nice mode uses messages subsystem, so it will also depends probably on console module.
#define MIXED 2
#define DEBUG_ENABLED NO
// Change to no if there's problem with console module
#define ALLOW_NICE_MESSAGES YES
#ifndef HELPER_INCLUDED
#error To use debug header you must include helper
#endif
#if DEBUG_ENABLED == 0
#ifdef __DEBUG__
#undef __DEBUG__
#endif
#endif
#ifdef __DEBUG__
#define __MODULE_DEBUG_INIT
#define __print_int(x) { \
int stcount; \
int count = 0;\
int y = x; \
if (y == 0) { \
\
*__DEBBUGED___dbA = '0'; \
++__DEBBUGED___dbA; \
++__DEBBUGED___dbA; \
} \
else { while (y) { \
++count; y = y / 10;} \
y = x; stcount = count;\
if ((__DEBBUGED___dbA - ((char*)0xB8000)) + count > 80 * 25 * 2) { \
__DEBBUGED___dbA = 0xB8000; \
} \
--count; \
while (y) { \
__DEBBUGED___dbA[count * 2] = (char)(y % 10 + '0'); --count; y = y / 10;} \
__DEBBUGED___dbA += stcount * 2 ;} \
}
#define ____NO_DEBBUGED__ \
char __DEBBUGED__color = 'A'; \
char* __DEBBUGED___dbA = ((char*)0xB8000);
#define ____DEBBUGED__ \
char __DEBBUGED__color = 'A'; \
char* __DEBBUGED___dbA = ((char*)0xB8000);
#define __print_debug_info { if (__IS_DEBBUGED__) {if (__DEBBUGED___curr > 'Z') __DEBBUGED___curr = 'A'; *__DEBBUGED___dbA = __DEBBUGED___curr; ++__DEBBUGED___dbA ; *__DEBBUGED___dbA = __DEBBUGED__color;++__DEBBUGED___dbA; print_int(__COUNTER__) ; ++__DEBBUGED___dbA ; ++__DEBBUGED___dbA; ++__DEBBUGED___curr; } }
#define __putsD(x) do { if (__IS_DEBBUGED__) {char *a = x; while ('\0' != *a) {*__DEBBUGED___dbA = *a; ++a; ++__DEBBUGED___dbA; ++__DEBBUGED___dbA;} ++__DEBBUGED___dbA; ++__DEBBUGED___dbA;} } while (0)
#define __clsD \
{ \
if (__IS_DEBBUGED__) { \
int i = 0;\
for (; i < 80 * 25 * 2; ++i) *((char*)(0xB8000 + i)) = ' '; \
__DEBBUGED___dbA = (char*) 0xB8000; \
} \
}
#if (ALLOW_NICE_MESSAGES == YES)
#define M____DEBBUGED__ ;
#define M____NO_DEBBUGED__ ;
#define M__print_int(x) ;
#define M__print_debug_info ;
#define M__putsD(x) ;
#define M__clsD ;
#else
#define M____DEBBUGED__ ____DEBBUGED__
#define M____NO_DEBBUGED__ ____NO_DEBBUGED__
#define M__print_int(x) __print_int(x)
#define M__print_debug_info __print_debug_info
#define M__putsD(x) __putsD(x)
#define M__clsD __clsD
#endif
#if (defined DRIVER_messages_NUMBER) && ((ALLOW_NICE_MESSAGES == YES) || (ALLOW_NICE_MESSAGES == MIXED))
static void (*__DEBUG__global_print_fnc)(int fmt, void *parameters) = 0;
static void (*__DEBUG__global_malloc_fnc)(int size) = 0;
#define __NO_DEBBUGED__ M____NO_DEBBUGED__; char __IS_DEBBUGED__ = 0; char __DEBBUGED___curr = 'A';
#define __DEBBUGED__ M____DEBBUGED__; char __IS_DEBBUGED__ = 1; char __DEBBUGED___curr = 'A'; putsD(KISS_STRING(__FILE__)); putsD(KISS_STRING(__FUNCTION__));
#define putsD(x) { M__putsD(x); if (__IS_DEBBUGED__ && *((void**)KISS_POINTER(&__DEBUG__global_print_fnc))) { ((*(void(**)(int, intptr_t *))KISS_POINTER(&__DEBUG__global_print_fnc)))(-1, x); } }
#define print_int(x) { M__print_int(x); if (__IS_DEBBUGED__ && *((void**)KISS_POINTER(&__DEBUG__global_print_fnc))){ ((*(void(**)(int, intptr_t *))KISS_POINTER(&__DEBUG__global_print_fnc)))(-2, x); } }
/* TODO: We need to free this memory */
#define print_debug_info {M__print_debug_info; if (__IS_DEBBUGED__ && *((void**)KISS_POINTER(&__DEBUG__global_print_fnc)) && *((void**)KISS_POINTER(&__DEBUG__global_malloc_fnc))) {intptr_t *parameters = ((*(void*(**)(int))KISS_POINTER(&__DEBUG__global_malloc_fnc)))(sizeof(*parameters) * 3); if (0 != parameters) { parameters[0] = KISS_STRING("%c %d");parameters[1] = __DEBBUGED___curr; parameters[2] = __COUNTER__; ((*(void(**)(int, intptr_t *))KISS_POINTER(&__DEBUG__global_print_fnc)))(-3, parameters); } } };
#ifdef DRIVER_utils_NUMBER
#define MODULE_DEBUG_INIT *((void**)KISS_POINTER(&__DEBUG__global_print_fnc)) = dr[DRIVER_messages_NUMBER].get_export_tables()->api_version_exports->symbols[0].procedure; if (dr[DRIVER_utils_NUMBER].get_export_tables) *((void**)KISS_POINTER(&__DEBUG__global_malloc_fnc)) = dr[DRIVER_utils_NUMBER].get_export_tables()->api_version_exports->symbols[1].procedure;
#else
#define MODULE_DEBUG_INIT *((void**)KISS_POINTER(&__DEBUG__global_print_fnc)) = dr[DRIVER_messages_NUMBER].get_export_tables()->api_version_exports->symbols[0].procedure;
#endif
#else
#define MODULE_DEBUG_INIT
#define print_int(x) __print_int(x)
#define __NO_DEBBUGED__ ____NO_DEBBUGED__; char __IS_DEBBUGED__ = 0; char __DEBBUGED___curr = 'A';
#define __DEBBUGED__ ____DEBBUGED__; char __IS_DEBBUGED__ = 1;char __DEBBUGED___curr = 'A'; M__putsD(KISS_STRING(__FILE__)); M__putsD(KISS_STRING(__FUNCTION__));
#define print_debug_info __print_debug_info
#define putsD(x) __putsD(x)
#define clsD M__clsD
#endif
#else
#define MODULE_DEBUG_INIT
#define __DEBBUGED__
#define __NO_DEBBUGED__
#define print_int(x)
#define print_debug_info
#define putsD(x)
#define clsD
#endif
I must calculate address to these variables, because modules are loaded by GRUB2 and I don't known how to compile position independent executable and make my kernel aware of this. Solution is simple: send pointer to start of module in memory via message/procedure call, so module can calculates position of each global variable.