{"id":105770,"date":"2021-10-06T07:00:00","date_gmt":"2021-10-06T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105770"},"modified":"2021-10-06T06:43:40","modified_gmt":"2021-10-06T13:43:40","slug":"20211006-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20211006-00\/?p=105770","title":{"rendered":"Why is the <CODE>main()<\/CODE> function always at address 0x00401000 in a simple program?"},"content":{"rendered":"<p>If you compile a simple C or C++ program, and then load it into the debugger as a dump file (or if you execute the program with <a href=\"https:\/\/en.wikipedia.org\/wiki\/Address_space_layout_randomization\"> ASLR<\/a> disabled), you&#8217;ll find that the <code>main<\/code> function is at offset <code>0x00401000<\/code>. What is so special about this address?<\/p>\n<p>It&#8217;s the result of multiple technical decisions that add together, literally.<\/p>\n<p>Your simple C or C++ program has only one function: <code>main<\/code>. It is therefore the function at the start of your code section, and the address of the <code>main<\/code> function is the address of the code section.<\/p>\n<p>Traditionally, the code section is the first section of a Windows Portable Executable file. There&#8217;s no technical reason for it, but <i>somebody<\/i> has to go first, and code seems to be the natural choice since it&#8217;s almost always the most important part of the module. (&#8220;Primary reason for existence&#8221; in most cases.)<\/p>\n<p>Sections are page-aligned because each section specifies its protection, and memory protection is applied at the page level. Therefore, the offset of the code section must be a multiple of the page size, which <a title=\"What are the page sizes used by Windows on various processors?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210510-00\/?p=105200\"> for x86 is 4KB<\/a>.<\/p>\n<p>The page at offset zero contains the module header information.<\/p>\n<p>Therefore the first page available for the code section is the page at offset <code>0x1000<\/code>.<\/p>\n<p>The last piece of the puzzle is that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20141003-00\/?p=43923\"> <code>0x00400000<\/code> is the default base address for executables<\/a> on x86.<\/p>\n<p>Put all of these decisions together (some technical, some arbitrary), and you find that the address of the <code>main()<\/code> function in a simple program is always <code>0x00401000<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When multiple technical decision add up, literally.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-105770","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>When multiple technical decision add up, literally.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=105770"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105770\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=105770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}