elf in guile

19 January 2014 7:55 PM (guile | scheme | igalia | gnu | elf | dwarf | object file formats | compilers | memory)

Good evening, gentle hackfolk!

Today I'd like to wrap up my three-part series of articles on what's new in Guile 2.2's compiler and runtime. I talked about the virtual machine a couple months ago, and the compiler internals just last Sunday. Today's article is about the object file format.

Sounds boring, right? Well, probably for most humans. But hackers, Guile compiles to ELF. Pretty rad, amirite? I thought so too. Read on, nerdy readers, read on!

object files

So let's consider the problem: Guile compiles to bytecode for a custom virtual machine. In the future we want to do native compilation. In both cases we'll need to write bytes out to disk in some format, and be able to load that code back into Guile. What should be the format for those bytes? For our purposes, a good object file format has a number of characteristics:

  • Above all else, it should be very cheap to load a compiled file.

  • It should be possible to statically allocate constants in the file. For example, a bytevector literal in source code can be emitted directly into the object file.

  • The compiled file should enable maximum code and data sharing between different processes.

  • The compiled file should contain debugging information, such as line numbers, but that information should be separated from the code itself. It should be possible to strip debugging information if space is tight.

These characteristics are not specific to Scheme. So why not just steal from the C and C++ people? They use the flexible ELF object file format, and so does Guile.

Note that although Guile uses ELF on all platforms, we do not use platform support for ELF. Guile implements its own linker and loader. The advantage of using ELF is not sharing code, but sharing ideas. ELF is simply a well-designed object file format.

An ELF file has two meta-tables describing its contents. The first meta-table is for the loader, and is called the program table or sometimes the segment table. The program table divides the file into big chunks that should be treated differently by the loader. Mostly the difference between these segments is their permissions.

Typically all segments of an ELF file are marked as read-only, except that part that represents modifiable static data or static data that needs load-time initialization. Loading an ELF file is as simple as mmapping the thing into memory with read-only permissions, then using the segment table to mark a small sub-region of the file as writable. This writable section is typically added to the root set of the garbage collector as well.

The other meta-table in an ELF file is the section table. Whereas the program table divides an ELF file into big chunks for the loader, the section table specifies small sections for use by introspective tools like debuggers or the like. One segment (program table entry) typically contains many sections. There may be sections outside of any segment, as well.

I know, this is a bit dry. So I made you a picture; or rather, a program that makes pictures. Here's an example of one of the pictures:

What we see here is a page map of assembler.go, a module in Guile's compiler. The upper part of the top shows each section in a different color, in the place they appear in the file. Each horizontal line is one four-kilobyte page.

As you can see, some pages contain multiple sections or parts of sections; that is the case for pages that have sections with the same permissions (read-only versus read-write), and which don't have special alignment requirements. About halfway down after the small red .dynamic section there is a gap, indicating that the next section, .data needs to start on a separate page -- in this case because it is writable.

This page map shows the sections of the ELF file. (There are only three segments; the first read-only part, a small "dynamic" segment holding the .dynamic section, used when the file is loaded; and the final read-write section. You can't see this from the visualization, but actually everything after .data is in no segment at all -- because it's not strictly necessary at run-time. That's the ELF convention.)

(Why is this file so big, you ask? It's a complicated answer. Part of it is because much of the assembler is itself a generated program; it uses Scheme procedural macros to define emit-foo procedures for each kind of instruction, based information extracted from the VM code (link). Guile doesn't do phasing, so these macros are residualized into the object file, and because of datum->syntax, macros have a lot of associated constant literals. That probably explains the huge size of .data, as syntax objects contain vectors and lists and symbols needing run-time relocation. I would have to check, but I think .rtl-text is big mostly because of a number of dynamic type checks in this particular module that the optimizer is (rightly) unable to elide. Alack. Surely both of these can be fixed eventually.)

But enough of problems. Did I mention that I made a program? And how! I think this may be the only blog post you have ever read that has a form in it, but that's how I am rolling today. Select an ELF file on your system, click submit, and you can have your very own page map. It works on any ELF file, not just Guile's files, so you can send your or whatever.

If you don't see a form above, your blog reader must have stripped it out. Click through to the blog post itself, or go visit the tool directly. And of course the program is written in Guile -- the file upload handling, the ELF parsing, and the pixel munging (using Cairo and a homebrew charting library).

Anyway, here's another example, this time of a small file:

Here we see that this file has the same sections as our earlier bloated example, only they are smaller.

Allow me to point out some details.

Firstly, the .rtl-text section holds the bytecode. Usually this would be called .text, but I didn't want to mess with people's expectations, and the new VM used to be called the "RTL" VM. (No longer, that was a silly name.)

The .data section holds data that needs initialization, or which may be modified at runtime. .rodata holds statically allocated data that needs no run-time initialization, and which therefore can be shared between processes. The initializations themselves, like relocations, are compiled to a procedure in the .rtl-text section, linked to from the dynamic section.

And that's all the sections! Except for the introspection and debugging-related sections, that is. These sections are used by the Guile runtime to be able to answer questions like "what is the name of this function?" or "what is the source position corresponding to bytecode position X?" or "what is the documentation string for this function?" or, well, I think you get the point. None of this is usually needed at runtime, and because it is all allocated at the end of the file, that means that usually none of it is ever paged into memory.

Note that for some of this metadata we use the standard DWARF format. I think we are one of the few dynamic language runtimes that does this nifty thing.

Also, all read-only data in these ELF files is ripe for sharing between processes, paging out to disk if under memory pressure, etc. For example, if I look on the smaps file for one of the two web processes running on this server, I see:

Size:                132 kB
Rss:                 124 kB
Pss:                  62 kB

meaning that for this particular file, almost all of it is not only shareable but being shared. Good times.

Finally, all of this has a positive impact on start-up time. While I can get Guile 2.0 to start up in 11 milliseconds, with Guile 2.2 I am down to 8 milliseconds. Likewise guile -c '(sleep 100)' in Guile 2.0 uses 3144 kB of private dirty memory, compared to 1852 kB with Guile 2.2. There's still improvements to be made, but things are going well.

Well, again I find myself rambling. Check out the little ELF mapper tool I have above and let me know of any curious results. Do send your questions as well; though I've been derelict at responding, who knows, new year, new leaf right? Until next time, happy hacking.

41 responses

  1. funny_falcon says:

    In presence of Go language, choosing 'go' for file extension seems to be unlucky decision.

  2. funny_falcon says:

    Having shared compiled code for scripting/dynamic language is a great idea!

  3. Arne Babenhauserheide says:

    8 ms startup time is awesome! (with python I’m at 30ms, and that can be noticeable: for example when running a script multiple times in a for-loop in bash)

    @funny_falcon: In presence of the .go extension of guile object files, choosing Go as language name seems to be an unlucky decision ☺ (cuts both ways)

  4. Colin Walters says:

    Interesting post. Does Guile support Windows at all? If so, do you still use ELF there? I guess you could since you're just using it as a container, right?

  5. Walter Tetzner says:

    @Colin Walters, I don't know about Windows support in Guile, but, yes, he said that Guile implements its own linker and loader, so that shouldn't be a problem.

  6. Cerification Desk says:

    hello it was the exceptional post and very cool to read

  7. Jonah says:

    Our icloud unlock experts can show you how to get your files back. It's easy.

  8. free klondike solitaire says:

    I visit this page and then comes to know that free klondike solitaire the one of the most entertaining card game I had ever I suggest others to play share and invite for this game.

  9. CRT-160 Exam dumps says:

    Hey admin your sharing way is unique. This is fantastic site.I enjoyed reading your articles. This is truly a great read for me.

  10. VCE Exams Test says:

    I guide the students that how they can pass the exams and how can they get the certification for the latest knowledge this certification exam and better for your bright future and will helpful to attain the IT certification.

  11. ACRT-160 Exam Questions And Answers says:

    I loved this informative article. This is some deep and great information about technology. keep sharing such good stuff.
    If you want to pass ACRT-160 exam in first attempt
    You can get ACRT-160 Exam Questions and answers here.

  12. Microsoft MCSA Certification Exam says:

    VceExamsTest have always verified and updated dumps which helps you to prepare your IT Certifications exam with less effort in very short time. It has latest and relevant study guide material which is useful for you to get prepare for Microsoft Dynamics 365 Exam Dumps Question exam with ease. I can recommend everyone a site where you can download and read recent study VCE Dumps in PDF and VCE Practice Software. VceExamsTest is providing original questions of Microsoft Certified Solutions Associate Exam Dumps.

  13. MCSA SQL Server 2016 Database Development Exam says:

    If you have short time and want to appear in MCSA SQL Server 2016 Database Development Exam. Then I will suggest you to use MCSA SQL Server 2016 Database Development Dumps. I have aced my exam with guaranteed results with MCSA: SQL 2016 Database Development Dumps. I simply practice all the questions and found almost same questions in the final test. I am thankful to for carving such a handy and useful study material.

  14. says:

    This is a brilliant blog! I'm very happy with the comments!..

  15. says:

    This is a brilliant blog! I'm very happy with the comments!..

  16. thiet ke in an says:

    Wow what a Great Information about World Day its very nice informative post. thanks for the post.

  17. Our site says:

    Wow! you really did your part in giving us a nice taste of information, I appreciate your effort and your ideas. Thanks!

  18. says:

    Cool blog. I found so much information for my works for Please, keep posting, I love reading your blog.

  19. towing service says:

    very impressive about your post....

  20. says:

    Your post is awesome, continue updating thanks!

  21. happy wheels says:

    The things you write are great, this knowledge helps me a lot in my life, thank you for all these great things.

  22. boxnovel says:

    This article is very detailed and meticulous, I have read many articles on this topic, but for this article, you leave me a deep impression and practical application in my life. Thank you for sharing.

  23. IDN Poker says:

    this post is tha very very use full

  24. shell shockers says:

    Thank you for sharing this useful article information. I am really impressed with the article you provided.

  25. james watson says:

    ihave read your articals that is helpfull and amazing content . please keep posting as regularly. thanks

  26. harry jack says:

    i have pass the cisco 200-901 exam in first attempt, i suggest you to visit this site ,just for sharing my exprience.

  27. Teatv apk says:

    Thank you for your nice comment on my blog!

  28. says:

    Thanks for sharing app

  29. spin rewriter free download says:

    we will always appreciate all you have done here because I know you are very concerned with our

  30. LivetheOrangeLife sign in says:

    Thank you for sharing the post!

  31. PSE-Strata says: is a well-known trusty PSE-Strata certification exam study material provider. Many IT pros have used their quality and updated study material for seizing the success in an exam at their first attempts with good grades. Therefore, if you do not want to waste your time and money then wisely

  32. johny says:

    CertificationGenie is offers updated Microsoft DA-100 dumps pdf questions for Analyzing Data with Microsoft Power BI exam. Most of the people want to pass the Microsoft DA-100 exam and I recommend they should use Microsoft DA-100 Questions for better exam preparation.

  33. DumpsHq says:

    DumpsHQ provides the best help for your cisco 200-301 dumps problem.And also clear all your doubts in your mind.

  34. deandarren says:

    visit our website:

  35. warsh dean says:

    I can only aspire to write material like this. I am extremely impressed with your presentation. Thank you for writing this engaging content. tent in a backpack.

  36. Fiona Smith says:

    I adore your post, but somewhere I found it grotesque and intricate to get it for newbies. I am your loyal and straight forward reader. I hope you will consider my feedback.

  37. Hostgator Black Friday says:

    can you tell, my computer screen turns blue and restart again and again. what should to do to solve this problem.

  38. douxltech54 says:

    Douxl technologies is a full-administration Best Web Development Company in Dubai. We are the main organization who provides a wide range of web development Services with the full access of our group. All our staff is having experienced and working with us for so long and all are proficient, legal and dependable.We observe the worldwide web design standard with regards to dealing with the progress of your business. Our engineers utilize the most recent innovation and structures to fabricate the agreement. We are the best web design company in dubai who work for the betterment of your business using innovative ideas and techniques.

  39. homediart says:

    Homediart is online Best handmade paintings store moslty customer comes through referral contact due to supreme quality.
    Visit website for Detail.

  40. exam dumps says:

    Dumps4Expert provides you the best opportunity to build your career by obtaining industry-demanding IT certifications. If you chose to stay with Dumps4Expert’ Exam dumps, there are 100% chances to get certified on the first attempt with high grades.

  41. certsquestions says:

    Certsquestions presents the exam preparation product to enable the candidate to pass the certification exams with good grades. We are offering genuine exam dumps with real PDF questions. Certsquestions offers the most recent and 100% updated test material for the Prepration of Certifications Exams.

Leave a Reply