{"id":1016,"date":"2010-07-14T18:37:13","date_gmt":"2010-07-14T16:37:13","guid":{"rendered":"http:\/\/glandium.org\/blog\/?p=1016"},"modified":"2024-10-15T09:13:09","modified_gmt":"2024-10-15T00:13:09","slug":"io-patterns-on-elf-binary-initialization","status":"publish","type":"post","link":"https:\/\/glandium.org\/blog\/?p=1016","title":{"rendered":"I\/O patterns on ELF binary initialization"},"content":{"rendered":"<p>[ I meant to finish this article much earlier, but i had to leave it in a half-written draft state for quite some time due to other activities ]<\/p>\n<p>One particular issue that arises with big binary files is that of I\/O patterns. It looks like it is not something under the usual scope for both <code>ld.so<\/code> and programs\/libraries, but it can have a dramatic influence on startup times. My analysis is far from complete, would need to be improved by actually investigating <code>ld.so<\/code> code further, and so far has been limited to libxul.so built from mozilla-central, with the help of icegrind, on x86-64 linux.<\/p>\n<p>As I <a href=\"\/blog\/?p=1008\">wrote recently<\/a>, icegrind allows to track memory accesses within <code>mmap()<\/code>ed memory ranges. As <code>ld.so<\/code> does <code>mmap()<\/code> the program and library binaries, tracking the memory accesses within these <code>mmap()<\/code>s allows to get a better idea at I\/O patterns when an ELF binary is initialized. I only focused on local accesses within a single given binary file (libxul.so) because the case of Firefox loading many files at startup is already being scrutinized, and because most of the non-Firefox files being read at Firefox startup (system and GNOME libraries) are most likely already in the page cache. I also focused on that because it was an interesting case that could be helpful to understand how big (and maybe less big) binaries may be affected by the toolchain (compiler, linker and dynamic linker) and possibly by some coding practices.<\/p>\n<p>For the record, what I did to get these results is to use <a href=\"\/blog\/?p=1008\">icegrind and elflog<\/a>, with further additions to the &quot;sections&quot; file with some output from &quot;<code>objdump -h<\/code>&quot;: I added sections that elflog wouldn't give (such as <code>.plt<\/code>, <code>.hash<\/code>, <code>.dynsym<\/code>, etc.), and even down to the entry level for the <code>.rela.dyn<\/code> section. The latter is particularly interesting because icegrind only outputs the first access for any given section. To see sequential accesses within that section, you need to split it in smaller pieces, which I did for <code>.rela.dyn<\/code> entries (each one being 24 bytes on x86-64). Uncommenting some parts of the icegrind code was also useful to track where some accesses were made from, code-wise.<\/p>\n<p>Now, the interesting data:<\/p>\n<p>One of the first accesses is a nullification of a few variables within the <code>.bss<\/code> section. The <code>.bss<\/code> section is usually an anonymous piece of memory, that is, a range of <code>mmap()<\/code>ed memory that is not backed by a file, and filled with zeroes by the kernel (I think it even does that lazily). It is used for e.g. variables that are initialized with zeroes in the code, and obviously, any code accessing these variables would be addressing at some offset of the <code>.bss<\/code> section. It means the section needs to start in memory at the offset it has been assigned by the linker at build time. This is actually where problems begin.<\/p>\n<p>When the <code>.bss<\/code> section offset as assigned by the linker doesn't align on a page (usually 4KB), the <code>mmap()<\/code>ed <code>.bss<\/code> can't be at that location, and really starts on the next page. The remainder is still <code>mmap()<\/code>ed from the binary file, and <code>ld.so<\/code> will itself fill that part. As the <code>.bss<\/code> section doesn't start on a page boundary, any write at this location will trigger the kernel reading the entire page. This means one of the first set of data being read from the file is the end of the section preceding <code>.bss<\/code>, and the beginning of the following one. Most likely, respectively the <code>.data<\/code> and <code>.comment<\/code> sections.<\/p>\n<p>While this probably doesn't matter much when the binary file is small, when it is big enough, reading in a non sequential manner will trigger hard disk seeks, and we all know how they can hurt performance. Although thankfully, cheap SSDs should be coming some day, in the meanwhile, we still need to cope with the bad performance. The interesting part is, the <code>.bss<\/code> section is really empty in the binary file, so its &quot;virtual&quot; memory address could be anywhere. Why the linker wouldn't align it at a page boundary without having to resort to a linker script is beyond me.<\/p>\n<p>The next accesses go back and forth between <code>.dynamic<\/code>, <code>.gnu.hash<\/code>, <code>.dynstr<\/code>, <code>.gnu.version_r<\/code>, <code>.gnu.version<\/code> and <code>.dynsym<\/code>. These are probably all related to symbol version resolution and DT_NEEDED library loading. While most of these sections are at the beginning of the file (not necessarily in the order they are read from), the <code>.dynamic<\/code> section is much nearer to the end, but way before <code>.data<\/code>, so that it won't even have been loaded as a by-product of the <code>.bss<\/code> section loading.<\/p>\n<p>After that, the <code>.rela.dyn<\/code> section is read, and for each relocation entry it contains, the relocations are being applied. Relocations is one of the mechanisms by which position independent code (PIC) is made possible. When a library is loaded in memory, it is not necessarily loaded at the same address every time. The code and data contained in the library thus need to cope with that constraint. Fortunately, while the base address where the library is <code>mmap()<\/code>ed in memory is not necessary constant, the offsets of the various sections are still as codified in the binary. The library code can thus directly access data at the right offset if it knows the base offset of the library (which is what is done on the x86 ABI), or if it knows where the current instruction is located (which is what is done on the x86-64 ABI).<\/p>\n<p>&quot;Static&quot; data (initialized in e.g. a <code>const<\/code> or a global variable, or, in C++ case, vtables), on the other hand, may contain pointers to other locations. This is where relocation enters the scene. The <code>.rela.dyn<\/code> section contains a set of rules describing where in the binary some pointers need to be adjusted depending on the base library offset (or some other information), and how they should be updated. <code>ld.so<\/code> thus reads all <code>.rela.dyn<\/code> entries, and applies each relocation, which means that while <code>.rela.dyn<\/code> is being read sequentially, reads and writes are also performed at various places of the binary, depending on the content of the <code>.rela.dyn<\/code> entries.<\/p>\n<p>This is where this gets ugly for Firefox: there are near 200000 such relocations. On x86-64 an entry is 24 bytes (12 on x86), and each of these is going to read\/write a pointer (8 bytes on x86-64, 4 on x86) at some random (though mostly ordered) location. The whole <code>.rela.dyn<\/code> section not being read ahead, what actually happens is that it is read in small batches, with seeks and reads of other data at various locations in between. In libxul.so case, this spreads over <code>.ctors<\/code>, <code>.data.rel.ro<\/code>, <code>.got<\/code>, and <code>.data<\/code>. The relocation entries are somehow ordered by address to be rewritten, though they occasionally jump backwards. Some of these relocations also appear to be touching to <code>.gnu.version<\/code>, <code>.dynsym<\/code> and <code>.dynstr<\/code>, because their type involves a symbol resolution.<\/p>\n<p>Once <code>.rela.dyn<\/code> relocation have been dealt with comes <code>.rela.plt<\/code>'s turn. The principle is the same for this section: entries describe what kind of relocation must be done where, and how the result must be calculated. The scope of this section, though, is apparently limited to <code>.got.plt<\/code>. But before explaining these relocations, I'll explain what happens with the PLT.<\/p>\n<p>The PLT (Procedure Linkage Table) is used when calling functions from external libraries. For example, in a Hello World example, the PLT would be used for calls to the <code>puts<\/code> function. The function making the call would in fact call the corresponding PLT location. The PLT itself, on x86 and x86-64, at least, consists of 3 instructions (I'll skip the gory details, especially for x86, where the caller needs to also set some register before calling the PLT). The first instruction is the only one to be called most of the time: it reads the final destination in the <code>.got.plt<\/code> section, and jumps there. That final destination, obviously, is not fixed in the library file, since it needs to be resolved by its symbol. This is why the two subsequent instructions exist : originally, the destination &quot;stored&quot; in the <code>.got.plt<\/code> section points back to the second instruction ; the first instruction will effectively be a nop (no operation), and the following instructions will be executed. They will jump into code responsible for symbol resolution, update of the <code>.got.plt<\/code> entry for the next call, and call of the real function.<\/p>\n<p>But pointing back to the second instruction is like the pointers in static data we saw above : it's not possible in position independent code. So, the <code>.rela.plt<\/code> relocations are actually filling the <code>.got.plt<\/code> section with these pointers back to the PLT. There are a tad more than 3000 such relocations.<\/p>\n<p>All these relocations should be going away when prelinking the binaries, but from my several experimentations, it looks like prelinking only avoids relative relocations, and not the others, while it technically could skip all of them. <code>prelink<\/code> even properly applies all the relocations in the binary, but executing the binary rewrites the same information at startup for all but relative relocations. That could well be a bug in either <code>ld.so<\/code> not skipping enough relocations or <code>prelink<\/code> not marking enough relocations to be skipped. I haven't dug deep enough in the code to know how prelinking works exactly. Anyways, prelinking is not a perfect solution, as it also breaks <a href=\"http:\/\/en.wikipedia.org\/wiki\/Address_space_layout_randomization\">ASLR<\/a>. Sure, prelink can randomize library locations, but it relies on the user or a cron job doing so at arbitrary times, but that's far from satisfying.<\/p>\n<p>An interesting thing to note, though, is that a good part of the relocations prelinking doesn't rid us of in libxul.so (more than 25000) are due to the <code><strong>cxa_pure_virtual<\/strong><\/code> symbol, which is used for, well, pure virtual methods. In other words, virtual methods that don't have an implementation in a given class. The <code>cxa_pure_virtual<\/code> function is set as method in the corresponding field(s) of the class VTABLE in the <code>.data.rel.ro<\/code> section. This function is provided by libstdc++, and as such, is dynamically linked. But this function is just a dummy function, doing nothing. Defining an empty <code>__cxa_pure_virtual<\/code> function to be included in libxul.so makes these relocations become relative, thus taken care of by prelinking.<\/p>\n<p>After all relocations occur, the library initialization itself can begin, and the content of the <code>.init<\/code> section is executed. That section, indirectly, executes all functions stored in the <code>.ctors<\/code> section. This includes static initializers, which are unfortunately called backwards, as <a href=\"http:\/\/blog.mozilla.com\/tglek\/2010\/05\/27\/startup-backward-constructors\">Taras pointed out already<\/a>. Each of these static initializers are also accessing various locations in the <code>.data<\/code> or <code>.bss<\/code> sections, which may or may not have already been loaded during the relocation phase. The execution of these initializers will also (obviously) read various pieces of the <code>.text<\/code> section  (despite its name, it contains executable sections, i.e. functions code).<\/p>\n<p>The initialization of the library ends there, and no access should happen until a function from the library is called. In libxul.so case, XRE_main is first called, then many other functions, but that's another story. All that needs to be remembered about the startup process past this point is that the <code>.text<\/code> section will be read heavily, as well as the various data, got, plt and dynamic symbol sections, in a very scattered way. While most of these sections may have been retrieved in memory already, as a byproduct of the various initialization processes described above, some may have not, increasing even more the need to seek at all places in the binary file.<\/p>\n<p>Now the main problem with all these I\/O patterns at startup, is that it seems the only way reorganizing the binary layout may have a visible impact is by considering <em>all<\/em> the above, and not only a part of it, because only addressing a part of it is very likely to only move part of the problem to a different layer.<\/p>\n<p>All in all, making sure the relevant sections of libxul.so are read by the kernel before <code>ld.so<\/code> enters the game is a good short-term solution to avoid many seeks at startup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ I meant to finish this article much earlier, but i had to leave it in a half-written draft state for quite some time due to other activities ] One particular issue that arises with big binary files is that of I\/O patterns. It looks like it is not something under the usual scope for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,25],"tags":[23],"class_list":["post-1016","post","type-post","status-publish","format-standard","hentry","category-mozilla","category-planet-mozilla","tag-en"],"_links":{"self":[{"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1016"}],"version-history":[{"count":19,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1016\/revisions"}],"predecessor-version":[{"id":4369,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1016\/revisions\/4369"}],"wp:attachment":[{"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}