Skip to content

Commit 0e3f9e4

Browse files
committed
fix mcount
1 parent dc89255 commit 0e3f9e4

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

src/libc/crt0/makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ SRC += c1args.c
1212
SRC += c1loadef.c
1313
SRC += c1pglob.c
1414
SRC += crt1.c
15-
SRC += mcount.c
15+
SRC += mcount_i.c
16+
SRC += mcount.S
1617
SRC += memhandl.c
1718
SRC += rfinfo.c
1819
SRC += dfinfo.c
@@ -40,5 +41,5 @@ $(LIB)/gcrt0.o : gcrt0.S crt0.S exit16.ah sbrk16.ah
4041
$(MISC) rm gcrt0.o
4142
sed 's@gcrt0.o@$(LIB)/gcrt0.o@' gcrt0.d > gcrt02.d
4243

43-
mcount.o : mcount.c
44+
mcount_i.o : mcount_i.c
4445
$(XNOPGGCC) -c $<

src/libc/crt0/mcount.S

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.intel_syntax noprefix
2+
.text
3+
.globl _mcount
4+
_mcount:
5+
cmp esp, offset _etext
6+
jb Lstack_overflow
7+
push eax
8+
push ecx
9+
mov eax, [esp+8] # Our return address (callee)
10+
mov ecx, [ebp+4] # Callee's return address (caller)
11+
call ___mcount_internal
12+
pop ecx
13+
pop eax
14+
ret
15+
Lstack_overflow:
16+
ud2

src/libc/crt0/mcount.c renamed to src/libc/crt0/mcount_i.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct MTAB {
3838

3939
static header h;
4040
static short *histogram;
41-
static int mcount_skip = 1;
41+
static volatile unsigned char mcount_skip = 1;
4242
static int histlen;
4343
static MTAB *mtab=0;
4444

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

4949
static int profiling_p;
5050

51-
/* called by functions. Use the pointer it provides to cache
51+
/* called by mcount. Use the pointer it provides to cache
5252
** the last used MTABE, so that repeated calls to/from the same
5353
** pair works quickly - no lookup.
5454
*/
55-
void mcount(int _to);
56-
void mcount(int _to)
55+
__attribute__((regparm (3))) /* EAX, EDX, ECX */
56+
void __mcount_internal(unsigned long to, MTABE **cache, unsigned long from);
57+
__attribute__((regparm (3)))
58+
void __mcount_internal(unsigned long to, MTABE **cache, unsigned long from)
5759
{
5860
MTAB *m;
5961
int i;
60-
unsigned int to;
61-
int ebp;
62-
unsigned int from;
6362
int mtabi;
64-
MTABE **cache;
65-
66-
/* obtain the cached pointer */
67-
__asm__ __volatile__ ("movl %%edx,%0" : "=g" (cache));
6863

6964
mcount_skip = 1;
7065
/* Do nothing if profiling is disabled. */
7166
if (!profiling_p)
7267
return;
7368

74-
if (&_to < &etext)
75-
*(int *)(-1) = 0; /* fault! */
69+
to -= 12;
7670

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

0 commit comments

Comments
 (0)