TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Main is usually a function. So then when is it not? (2015)

127 点作者 japrozs将近 2 年前

13 条评论

gumby将近 2 年前
Well, if you really want to confound things you could write your own __start which is typically the entry point called by the kernel, which in turn calls `main()`.<p>But actually that&#x27;s just by convention: C (and C++) programs drop `__start` into the binary and then tell the linker to mark the binary (mostly an ELF file these days, thank goodness) to indicate that the entry point is the symbol `__start`. There can be different versions depending on how many arguments to `main()` are requested, or it can simply pass all three and let the function body ignore the unwanted ones.<p>If you <i>really</i> want to confuse the TA, write the following program:<p><pre><code> #include &lt;stdio.h&gt; void foo(); int main() { printf (&quot;I&#x27;m in main\n&quot;); foo(); printf (&quot;I&#x27;m still in main\n&quot;); } void foo() { printf (&quot;I&#x27;m in foo!\n&quot;); } </code></pre> Then <i>tell the linker to mark `foo` as your entry point</i>. You could do this from the command line or even a custom linker script.<p>Of course depending on what your OS needs from `__start` you may have to do some extra setup, sorry. But this might work on Linux.<p>I&#x27;ve actually been thinking of modifying gcc&#x27;s `__start` to pass the arguments and env vars as a span (well, the function __start calls that calls `main()`.)
noduerme将近 2 年前
This is fun. On a broader note though, entry point main-type functions have always bothered me. Maybe because I grew up as a simple script kiddie with BASIC and Bash and PHP. I like code that starts executing from the first line and then runs whatever functions it wants to run. I realize that&#x27;s just an abstraction of main() but it&#x27;s a pleasant one for me. There&#x27;s something more constricting about there being one function to bootstrap everything than there is about one file.
评论 #37074424 未加载
评论 #37074121 未加载
评论 #37074643 未加载
评论 #37075937 未加载
评论 #37075055 未加载
muxator将近 2 年前
Complete final program for the laziest of us (after incorporating @10000truths&#x27;s advice):<p><pre><code> const int main[] __attribute__ ((section(&quot;.text&quot;))) = { -443987883, 440, 113408, -1922629632, 4149, 899584, 84869120, 15544, 266023168, 1818576901, 1461743468, 1684828783, -1017312735 }; </code></pre> Compilation (gcc 13):<p><pre><code> $ gcc -Wall main.c -o main main.c:1:11: warning: ‘main’ is usually a function [-Wmain] 1 | const int main[] __attribute__ ((section(&quot;.text&quot;))) = { | ^~~~ &#x2F;tmp&#x2F;ccsWmdiD.s: Assembler messages: &#x2F;tmp&#x2F;ccsWmdiD.s:4: Warning: ignoring changed section attributes for .text </code></pre> Execution:<p><pre><code> $ .&#x2F;main Hello World!</code></pre>
评论 #37073968 未加载
10000truths将近 2 年前
This won&#x27;t work anymore, as compilers will now place const arrays in the .rodata section, which is non-executable. Luckily, there&#x27;s an easy fix - just qualify the declaration of the array with:<p>__attribute__((section(&quot;.text&quot;))
评论 #37073974 未加载
smokel将近 2 年前
Back when compiler warnings possibly cost extra processing time to generate, it was possible to make gcc compile the craziest things.<p>After much experimentation, it turned out that the smallest program that would compile and run was only 5 bytes long:<p>main;
评论 #37073725 未加载
评论 #37076575 未加载
评论 #37073393 未加载
picadores将近 2 年前
We built a embedded &quot;Operating System&quot; during CS science courses, that was basically just a continous recursion, were the stack was eliminated and reset repeatetly. Main was just some assembly manipulating the instruction pointer to get the whole thing rolling. Good times, good crimes.
ackfoobar将近 2 年前
This reminds me of James Iry saying &quot;all your code and data are in one giant mutable array indexed by pointers, good luck&quot;. Half true, but insightful.
Kab1r将近 2 年前
(2015)
eschneider将近 2 年前
Looks like he&#x27;s independently re-invented PEEK and POKE from BASIC.
bee_rider将近 2 年前
5 points off for coding style.<p>5 points off for assuming too much about the system.
orra将近 2 年前
The article says `lea` helps calculate the array relative address on AMD64.<p>Why does the article say the problem would be tricky on 32-bit? `lea` is an old instruction. Thanks.
评论 #37073856 未加载
评论 #37073823 未加载
noam_k将近 2 年前
This reminds me of a great riddle: what is the shortest C code that compiles, but segfaults?<p>With the right flags: `main;` (implicitly a zeroed int).
eimrine将近 2 年前
main;<p>is a valid C program but main is not a function here.