delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2024/09/27/14:30:27

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 48RIUEcN902123
Authentication-Results: delorie.com;
dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=LAuOK6uw
X-Recipient: djgpp AT delorie DOT com
X-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1727461812; x=1728066612; darn=delorie.com;
h=content-transfer-encoding:subject:from:to:content-language
:user-agent:mime-version:date:message-id:from:to:cc:subject:date
:message-id:reply-to;
bh=Cszcnq0b9dwVbb6PY4mz2Yqkzw/EqFnEqn/Vp8xIIoo=;
b=LAuOK6uwxuan2nzg5hOpYAk3b4u8TG33Ay5tvIb1+xEJ6lf2S7JqgNFD4y4IOLNuki
jdTwI4NC2BN4FrSYdFR1eniBlmx8YyQDxpfgd+tw+X9T6EuyxpQQs6K46BVZhclmyx5f
bYVQTRFxQcxkrxuJM+oFrVFNElt6uyErBDrOeNfeSbo1OxdxWtocKj1CPCwkJOqvLuwl
bwzYX2BUDj2Z+uze04CRZzOfMfRwoJYDLH0UCjxgxLuQGnaHatMM8P/823zxniTKGXyW
hxEKJPr0TkVxnsXK5DvgPT4zc1lerHSh7Gj3LfuEj+yysAZDVDfpCMz2FaJGtdWdSoXa
MYEQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1727461812; x=1728066612;
h=content-transfer-encoding:subject:from:to:content-language
:user-agent:mime-version:date:message-id:x-gm-message-state:from:to
:cc:subject:date:message-id:reply-to;
bh=Cszcnq0b9dwVbb6PY4mz2Yqkzw/EqFnEqn/Vp8xIIoo=;
b=ayZPEsRVt5BPR6y0455+s4hKFeItUP91oEtOmDBHgHsVTgFsTYr6siCXlT81RwgISr
NpQUn9MZZt6k3UoJ7VjfnIjhVKWX+PxsaHHjWSS+3b0hhPfTk7r9n+Y1Ni3BnflMVL48
9Wl2Gxegw2EfJUHwbzPvsc1C2+JxokzrLpGlBwXn76llh9lyAOVz7eaIB8DFCg6K79HJ
OVdmVtU/0SW+LIWTQVnULW9tQr/kJZSKo/sfCluWRcLLUQ4pXQRHoQOvj11VZ77anOsD
UmKhjtaWPrue+5BMcGA2IGLCYSzTCageR7GrnSfNCNs9GKEVia5V4cQvW9kXyrcj6pLS
4OIQ==
X-Gm-Message-State: AOJu0Yy2+QsFqGZS7tJJOn3nkJ5uNjZbHogbmCd2H1MzTirWIcKTMEXy
f83BtcJUH82vtBHotzVSJoPWO0YpGXwRzNB514a8SD1XaUwLBc8izExWIw==
X-Google-Smtp-Source: AGHT+IFfjK3NaRMspagGjgcc08Tc1xAA3XMeqC3/nZCfqTWyXD+hY4eku2HSNTEAOyyKxFgxl8vSZg==
X-Received: by 2002:a17:906:4fd4:b0:a8a:4e39:a462 with SMTP id a640c23a62f3a-a93c48f90c2mr400577666b.7.1727461811043;
Fri, 27 Sep 2024 11:30:11 -0700 (PDT)
Message-ID: <fcc830d6-84e5-4c9b-9189-6a270234d213@gmail.com>
Date: Fri, 27 Sep 2024 20:30:10 +0200
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
To: djgpp AT delorie DOT com
From: "J.W. Jagersma (jwjagersma AT gmail DOT com) [via djgpp AT delorie DOT com]" <djgpp AT delorie DOT com>
Subject: mcount clobbers argument pointer
Reply-To: djgpp AT delorie DOT com

User "pif17" on Github discovered the following problem.  Given a simple
test case:

    #include <stdio.h>
    int main (int, char **argv)
    {
      puts (argv[0]);
    }

When compiled with '-pg', the above code segfaults.  This happens
because gcc aligns the stack on entering 'main', *before* emitting the
call to 'mcount':

00001760 <_main>:
    1760:	8d 4c 24 04          	lea    ecx,[esp+0x4]
    1764:	83 e4 f0             	and    esp,0xfffffff0
    1767:	ff 71 fc             	push   DWORD PTR [ecx-0x4]
    176a:	55                   	push   ebp
    176b:	89 e5                	mov    ebp,esp
    176d:	51                   	push   ecx
    176e:	83 ec 04             	sub    esp,0x4
    1771:	ba e0 16 01 00       	mov    edx,0x116e0
    1776:	e8 c5 3b 00 00       	call   5340 <_mcount>
    177b:	89 c8                	mov    eax,ecx
    177d:	8b 40 04             	mov    eax,DWORD PTR [eax+0x4]
    1780:	8b 00                	mov    eax,DWORD PTR [eax]
    1782:	83 ec 0c             	sub    esp,0xc
    1785:	50                   	push   eax
    1786:	e8 25 29 00 00       	call   40b0 <_puts>
    178b:	83 c4 10             	add    esp,0x10
    178e:	b8 00 00 00 00       	mov    eax,0x0
    1793:	8b 4d fc             	mov    ecx,DWORD PTR [ebp-0x4]
    1796:	c9                   	leave
    1797:	8d 61 fc             	lea    esp,[ecx-0x4]
    179a:	c3                   	ret

Here the argument pointer is saved in ECX, which is then clobbered by
mcount.

GCC has performed this automatic stack alignment since version 4.1, and
it apparently hasn't caused problems for other targets.  So this leads
me to conclude that djgpp's mcount is at fault, and it should have saved
all registers.

I can think of three possible solutions (in increasing order of
difficulty):

* Mark 'mcount' with attribute 'no_caller_saved_registers'.  This should
  do the right thing.  But note, this attribute was only introduced in
  gcc 7.
* Rename 'mcount' -> 'mcount_internal', then write an asm wrapper around
  it that saves all registers.
* Convince the gcc people that their compiler emits code in the wrong
  order :)

The second option is probably best, since it also makes retrieving EDX
less fragile.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019