Skip to content

Fix mcount #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: jwt27
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/libc/crt0/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ SRC += c1args.c
SRC += c1loadef.c
SRC += c1pglob.c
SRC += crt1.c
SRC += mcount.c
SRC += mcount_i.c
SRC += mcount.S
SRC += memhandl.c
SRC += rfinfo.c
SRC += dfinfo.c
Expand Down Expand Up @@ -40,5 +41,5 @@ $(LIB)/gcrt0.o : gcrt0.S crt0.S exit16.ah sbrk16.ah
$(MISC) rm gcrt0.o
sed '[email protected]@$(LIB)/gcrt0.o@' gcrt0.d > gcrt02.d

mcount.o : mcount.c
mcount_i.o : mcount_i.c
$(XNOPGGCC) -c $<
16 changes: 16 additions & 0 deletions src/libc/crt0/mcount.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.intel_syntax noprefix
.text
.globl _mcount
_mcount:
cmp esp, offset _etext
jb Lstack_overflow
push eax
push ecx
mov eax, [esp+8] # Our return address (callee)
mov ecx, [ebp+4] # Callee's return address (caller)
call ___mcount_internal
pop ecx
pop eax
ret
Lstack_overflow:
ud2
23 changes: 7 additions & 16 deletions src/libc/crt0/mcount.c → src/libc/crt0/mcount_i.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct MTAB {

static header h;
static short *histogram;
static int mcount_skip = 1;
static volatile unsigned char mcount_skip = 1;
static int histlen;
static MTAB *mtab=0;

Expand All @@ -48,35 +48,26 @@ extern int etext __asm__("etext");

static int profiling_p;

/* called by functions. Use the pointer it provides to cache
/* called by mcount. Use the pointer it provides to cache
** the last used MTABE, so that repeated calls to/from the same
** pair works quickly - no lookup.
*/
void mcount(int _to);
void mcount(int _to)
__attribute__((regparm (3))) /* EAX, EDX, ECX */
void __mcount_internal(unsigned long to, MTABE **cache, unsigned long from);
__attribute__((regparm (3)))
void __mcount_internal(unsigned long to, MTABE **cache, unsigned long from)
{
MTAB *m;
int i;
unsigned int to;
int ebp;
unsigned int from;
int mtabi;
MTABE **cache;

/* obtain the cached pointer */
__asm__ __volatile__ ("movl %%edx,%0" : "=g" (cache));

mcount_skip = 1;
/* Do nothing if profiling is disabled. */
if (!profiling_p)
return;

if (&_to < &etext)
*(int *)(-1) = 0; /* fault! */
to -= 12;

to = *((&_to)-1) - 12;
ebp = *((&_to)-2); /* glean the caller's return address from the stack */
from = ((int *)ebp)[1];
/* Do nothing if the FROM address is outside the sampling range. */
if (from < h.low || from >= h.high)
return;
Expand Down