Skip to content

Commit 94e0e44

Browse files
committed
fix mcount
1 parent dc89255 commit 94e0e44

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (C) 2024 DJ Delorie, see COPYING.DJ for details */
2+
3+
.intel_syntax noprefix
4+
.text
5+
.globl _mcount
6+
_mcount:
7+
push ebp
8+
mov ebp, esp
9+
push ecx
10+
push eax
11+
cmp ebp, offset _etext
12+
jb Lstack_overflow
13+
sub esp, 4 # Keep stack aligned
14+
mov ecx, [ebp]
15+
mov eax, [ebp+4]
16+
mov ecx, [ecx+4]
17+
push edx # Pointer to cached MTAB entry
18+
push eax # Our return address (callee)
19+
push ecx # Callee's return address (caller)
20+
call ___mcount_internal
21+
add esp, 16
22+
pop eax
23+
pop ecx
24+
pop ebp
25+
ret
26+
Lstack_overflow:
27+
ud2

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Copyright (C) 2024 DJ Delorie, see COPYING.DJ for details */
12
/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
23
/* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
34
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
@@ -38,7 +39,7 @@ typedef struct MTAB {
3839

3940
static header h;
4041
static short *histogram;
41-
static int mcount_skip = 1;
42+
static volatile unsigned char mcount_skip = 1;
4243
static int histlen;
4344
static MTAB *mtab=0;
4445

@@ -48,35 +49,24 @@ extern int etext __asm__("etext");
4849

4950
static int profiling_p;
5051

51-
/* called by functions. Use the pointer it provides to cache
52+
/* called by mcount. Use the pointer it provides to cache
5253
** the last used MTABE, so that repeated calls to/from the same
5354
** pair works quickly - no lookup.
5455
*/
55-
void mcount(int _to);
56-
void mcount(int _to)
56+
void __mcount_internal(unsigned long from, unsigned long to, MTABE **cache);
57+
void __mcount_internal(unsigned long from, unsigned long to, MTABE **cache)
5758
{
5859
MTAB *m;
5960
int i;
60-
unsigned int to;
61-
int ebp;
62-
unsigned int from;
6361
int mtabi;
64-
MTABE **cache;
65-
66-
/* obtain the cached pointer */
67-
__asm__ __volatile__ ("movl %%edx,%0" : "=g" (cache));
6862

6963
mcount_skip = 1;
7064
/* Do nothing if profiling is disabled. */
7165
if (!profiling_p)
7266
return;
7367

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

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

0 commit comments

Comments
 (0)