<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx</link><description>And sometimes they get away with it. Until they don't.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173925</link><pubDate>Mon, 13 Jun 2011 14:41:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173925</guid><dc:creator>Steve Wolf</dc:creator><description>&lt;p&gt;There are an awful lot of comments that boil down to:&lt;/p&gt;
&lt;p&gt;But I don&amp;#39;t understand why hardware isn&amp;#39;t an idealized perfection without any trade-off compromises between infinite ease of use and infinite speed?&lt;/p&gt;
&lt;p&gt;And then they go on to whine that this is intolerable, and their ignorance proves that the hardware guys have it all wrong, and that they shouldn&amp;#39;t have to learn anything or think more deeply than their idealized projection of what should be.&lt;/p&gt;
&lt;p&gt;And I assume your code is this perfect, has never made trade-offs between any opposed and intrinsically exclusive possibilities in the real (as in reality) world? &lt;/p&gt;
&lt;p&gt;Go get and education. &amp;nbsp;Learn why alignment is an issue at the hardware level. &amp;nbsp;Become a more worthy programmer who understands his tools more deeply, instead of whining that they&amp;#39;re imperfect*.&lt;/p&gt;
&lt;p&gt;*BTW, news flash: the world is imperfect.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173925" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173705</link><pubDate>Sun, 12 Jun 2011 10:09:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173705</guid><dc:creator>Reid</dc:creator><description>&lt;p&gt;@ficedula: You are right. But at the same time, what Pierre B. says, also applies:&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;The simple law is &amp;quot;An unenforced rule is not a rule.&amp;quot;&lt;/p&gt;
&lt;p&gt;Works in software, works in real life, works when raising children.&lt;/p&gt;
&lt;p&gt;&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;As long as we talk about humans who write software, this is almost generally true.&lt;/p&gt;
&lt;p&gt;The final conclusion depends on the very details of the subject at hand.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173705" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173653</link><pubDate>Sat, 11 Jun 2011 19:23:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173653</guid><dc:creator>ficedula</dc:creator><description>&lt;p&gt;@Burak: It makes sense because virtually *all* architectures either insist on alignment or impose a speed penalty on unaligned access. So pretty much any &amp;quot;data must be aligned to X byte boundary&amp;quot; requirements in pretty much any structure or format has at the very least a *plausible* explanation.&lt;/p&gt;
&lt;p&gt;Also, bearing in mind that some Windows formats date back to the 386 days or earlier, you have the problem that enforcing every single rule (including the ones that are there for performance reasons) under every situation takes memory and time which kind of defeats the point of having them. Sure, the alignment one would be pretty easy to check, but if Windows had verified that from the beginning, Raymond would be posting about how some common non-MS resource compiler instead failed to enforce the other future-proofing rules (some of which would be quite expensive to check at runtime, like anything that involves actually examining the resource data: paging in a bunch of resources that might never actually be required by the program can be horrifically slow).&lt;/p&gt;
&lt;p&gt;Net result: Enforcing all the rules is often not possible, assuming sane objectives. The alignment one *could* have been enforced, but the general rule for early versions of Windows was &amp;quot;assume the programmer knows what they&amp;#39;re doing, we can&amp;#39;t afford to waste time and memory checking/cleaning up after them&amp;quot;.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173653" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173601</link><pubDate>Sat, 11 Jun 2011 10:38:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173601</guid><dc:creator>Neil</dc:creator><description>&lt;p&gt;@Ivo: Sure, everyone has access to BeginUpdateResource now, but older installer generators that needed to run on platforms that didn&amp;#39;t provide it would have had to have a custom resource compiler, and therefore older installers could have had unaligned resources.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173601" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173596</link><pubDate>Sat, 11 Jun 2011 09:32:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173596</guid><dc:creator>Burak KALAYCI</dc:creator><description>&lt;p&gt;@ficedula: How can you assume a certain hardware architecture and then say PE spec makes sense because of that unmentioned assumption?&lt;/p&gt;
&lt;p&gt;If there will be a performance hit, you make it a recommendation, not a rule. If you make it a rule, you enforce it. If you didn&amp;#39;t need to enforce it, it means the rule was not necessary in the first place. If you program using a high level language like C, not to mention any toy scripting languages that use a GC, mentioning a performance hit does not make sense.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173596" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173517</link><pubDate>Fri, 10 Jun 2011 21:40:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173517</guid><dc:creator>ficedula</dc:creator><description>&lt;p&gt;@Evan: The topic is specifically talking about *resources* in PE files needing alignment. So generally, we&amp;#39;re dealing with a section of a PE file that&amp;#39;s been copied (or, more likely, mapped) into memory that contains a number of different resource items; e.g. bitmaps, icons, menus, ...&lt;/p&gt;
&lt;p&gt;If you can mandate that each resource is aligned on (say) 32-bit boundaries, you can access that resource data (which probably has its own internal structure; a bitmap header at the start of a bitmap resource...) through a simple pointer and assume that all the structures within the resource in question are correctly aligned (your 32-bit field in the bitmap header is aligned to a 32-bit boundary). So you can access the data through a pointer *without* copying it around: that&amp;#39;s the point.&lt;/p&gt;
&lt;p&gt;If the resource could be arbitrarily aligned, then to access the data, you either have to copy it to an aligned destination (inefficient), or access every single field in the resource one-byte-at-a-time (unaligned data, so pointers to anything bigger than a byte won&amp;#39;t necessarily work). That&amp;#39;s quite a pain.&lt;/p&gt;
&lt;p&gt;On x86, of course, your pointers *will* still work on unaligned data - but they&amp;#39;ll be slower, so there&amp;#39;s a benefit to mandating alignment even there (although the consequence of failing to do so is a speed penalty rather than a crash). All things considered, the PE spec requiring alignment on resources makes a lot of sense: even on the CPUs of the time, it made things faster, and as an added benefit paved the way for running on architectures that weren&amp;#39;t so friendly to unaligned pointers.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173517" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173458</link><pubDate>Fri, 10 Jun 2011 19:16:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173458</guid><dc:creator>Ben Voigt</dc:creator><description>&lt;p&gt;Clearly we need a new acronym for PE files, since &amp;quot;portable, executable file&amp;quot; just isn&amp;#39;t accurate.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173458" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173408</link><pubDate>Fri, 10 Jun 2011 16:46:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173408</guid><dc:creator>Evan</dc:creator><description>&lt;p&gt;@Burak: &amp;quot;What&amp;#39;s next, compilers imposing code formatting rules? (Please don&amp;#39;t make me remember).&amp;quot;&lt;/p&gt;
&lt;p&gt;Ever used Python? Or to a lesser extent, Haskell?&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s my take: from a hardware side, alignment restrictions make sense. Allowing misaligned accesses means you&amp;#39;ve got more hardware to deal with it, and doing it *properly* (i.e. not a leaky abstraction, which it still is; see the performance degradation and atomicity issues with misaligned accesses) would take even more. Spend those transistors on something that actually matters and will help the end user, like a larger cache or something instead.&lt;/p&gt;
&lt;p&gt;From the PE side, it probably makes less sense. It&amp;#39;s not like PE files require a file-system block alignment so that the system can set up a mapping from a memory page to a block (or more; not sure how big blocks are) of the file. I&amp;#39;m not sure what the 4-byte alignment restriction gains in that case; I don&amp;#39;t know what&amp;#39;s going on behind the scenes well enough to know for sure. I guess when copying the file contents from one location to another in memory, it allows the system to assume that the source address will be aligned. That doesn&amp;#39;t sound quite convincing enough to me if that&amp;#39;s it.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173408" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173388</link><pubDate>Fri, 10 Jun 2011 16:13:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173388</guid><dc:creator>640k</dc:creator><description>&lt;p&gt;@Burak: 16mb is as useless as 4gb. 64-bit bit addressing is the way to go.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173388" width="1" height="1"&gt;</description></item><item><title>re: PE resources must be 4-byte aligned, but that doesn't stop people from trying other alignments</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2011/06/09/10172702.aspx#10173371</link><pubDate>Fri, 10 Jun 2011 15:57:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173371</guid><dc:creator>Yuhong Bao</dc:creator><description>&lt;p&gt;Daniel Colascione: And unfortunately the Windows kernel itself don&amp;#39;t turn the CR0.AM bit on that is required for EFLAGS.AC to work.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173371" width="1" height="1"&gt;</description></item></channel></rss>