From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id BB7213858CDA for ; Fri, 7 Oct 2022 11:30:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BB7213858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x629.google.com with SMTP id a26so10684063ejc.4 for ; Fri, 07 Oct 2022 04:30:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=3NPZK0LJFp3fFa2S5DNHlDyE4RSbJjyZBMeFyK4DqXU=; b=N6W5bszPPlxZMgMg0BoCn2voZjxSUW3G7APt0Ur+L0mkwQ0K5RC1go+5SIn/yEzP/0 Cz/QoknQ5BThCLKkoYkh3u4XV9bvESWhgSAj/OiTnDKCFKYNIMfh5rjb4Y9ewTQMwBFv vZhPRnjfwOQaNX5yUca4ujR3pbPhfB2RY4rw16zLr8vMFsb0/AGB+jWLhQIWmudSdRRq 1Qfw6mB5wfou5O6W0Kn7KCZUzwZU9LjTUIyr5WLCGO+YGT3eFBAUioVgusOBWhGz9zrf NFlnfbHAbiv2oA17sg5Er6pYXxl/rn8iO5QI1NV34qu8frHQjlTE0p7NzEQQpvNaeIPG M5cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3NPZK0LJFp3fFa2S5DNHlDyE4RSbJjyZBMeFyK4DqXU=; b=xPg3KTa908gC7vh8AdbfiKjMTCsva7aRhaagCzoFOedsqQN0zak7KO/F7ThgO0Fwn0 +baZzbvfPaCjb34tg9ndokM1byPq5WVg/nXSacFccIZiRlLorWW5xv+oSJA9awjw6V8I 5GBlbsgFJZy/9EaiP3FIGrytB/tUHlHVrU3GJFBuyHRkBxvjZigMGk2tTvSqaDy2ao6q mz4SveLTEmaORQQ/vkGaiRIghhksw/NZ9uhfZ5P5tVJUp4nHNeeJQf0Sf2gWkc6Vvi8w 0VCcF+BqtwRA1a+TNashfy0AACl7Y5Ze3BKr+pDyfwaOS5qtl3nFMw1cIiAEmX+vOdlW 8i9Q== X-Gm-Message-State: ACrzQf39ooh6vPrWLLtZ9lZ2wF/10YNznSHEbeUP2K08g23aXoUVSRgF gWAepiPLQbZqlXE4IEJqbzDxEjK0HMfaDtYLTR0= X-Google-Smtp-Source: AMsMyM54/ZLRFNYLK4TIYKEJw+zt7m1mygXOUut4o9fXXO3rTHYQJBcDjjHc24gff9guU705gSpNSB4mO53RyAgxVx4= X-Received: by 2002:a17:907:7f13:b0:783:6227:b939 with SMTP id qf19-20020a1709077f1300b007836227b939mr3535822ejc.416.1665142221463; Fri, 07 Oct 2022 04:30:21 -0700 (PDT) MIME-Version: 1.0 References: <44594c0c-db9d-459a-7ecc-29c4f5544b28@redhat.com> In-Reply-To: <44594c0c-db9d-459a-7ecc-29c4f5544b28@redhat.com> From: Jonathan Wakely Date: Fri, 7 Oct 2022 12:30:09 +0100 Message-ID: Subject: Re: Handling of main() function for freestanding To: Jason Merrill Cc: "gcc@gcc.gnu.org" , arsen@aarsen.me, Jakub Jelinek Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, 4 Oct 2022 at 23:25, Jason Merrill wrote: > > On 9/28/22 16:15, Jonathan Wakely wrote: > > As part of implementing a C++23 proposal [1] to massively increase the > > scope of the freestanding C++ standard library some questions came up > > about the special handling of main() that happens for hosted > > environments. > > > > As required by both C++ (all versions) and C (since C99), falling off > > the end of the main() function is not undefined, the compiler is > > required to insert an implicit 'return 0' [2][3]. However, this > > special handling only applies to hosted environments. For freestanding > > the return type or even the existence of main is > > implementation-defined. As a result, GCC gives a -Wreturn-type warning > > for this code with -ffreestanding, but not with -fhosted: > > > > int main() { } > > > > Arsen (CC'd) has been working on the libstdc++ changes for the > > freestanding proposal, and several thousand libstdc++ tests were > > failing when using -ffreestanding, because of the -Wreturn-type > > warnings. He wrote a patch to the compiler [4] to add a new > > -fspecial-main flag which defaults to on for -fhosted, but can be used > > with -ffreestanding to do the implicit 'return 0' (and so disable the > > -Wreturn-type warnings) for freestanding as well. This fixes the > > libstdc++ test FAILs. > > > > However, after discussing this briefly with Jason it occurred to us > > that if the user declares an 'int main()' function, it's a pretty big > > hint that they do want main() to return an int. And so having > > undefined behaviour do to a missing return isn't really doing anybody > > any favours. If you're compiling for freestanding and you *don't* want > > to return a value from main(), then just declare it as void main() > > instead. So now we're wondering if we need -fspecial-main at all, or > > if int main() and int main(int, char**) should always be "special", > > even for freestanding. So Arsen wrote a patch to do that too [5]. > > > > The argument against making 'int main()' imply 'special main' is that > > in a freestanding environment, a function called 'int main()' might be > > just a normal function, not the program's entry point. And in that > > case, maybe you really do want -Wreturn-type warnings. I don't know > > how realistic that is. > > > > So the question is, should Arsen continue with his -fspecial-main > > patch, and propose it along with the libstdc++ changes, or should gcc > > change to always make 'int main()' "special" even for freestanding? > > void main() and long main() and other signatures would still be > > allowed for freestanding, and would not have the implicit 'return 0'. > > I would rather not add a flag. No well-defined freestanding program is > affected by implicit return 0 from main, it should always be enabled. There are some tests that fail if we do that. For whatever reason, they're checking the current semantics. Arsen implemented Jakub's suggestion which is to add the implicit return by default, but add -fno-builtin-main to restore the previous behaviour. Is that acceptable? If not, can you and Jakub reach consensus so that Arsen knows what to do instead? His -fno-builtin-main patch is at https://gcc.gnu.org/pipermail/gcc-patches/2022-September/602644.html (This is the only one of his patch series not committed, and results in 100s of FAILs for libstdc++ when testing with -fffreestanding).