<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"><title>Technovelty</title><link href="http://www.technovelty.org/" rel="alternate" /><id>http://www.technovelty.org/</id><updated>2013-05-14T21:42:00-07:00</updated><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/technovelty" /><feedburner:info uri="technovelty" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><link rel="license" type="text/html" href="http://creativecommons.org/licenses/by-sa/3.0/" /><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><entry><title>Debugging puppetmaster with Foreman</title><link href="http://www.technovelty.org/linux/debugging-puppetmaster-with-foreman.html" rel="alternate" /><updated>2013-05-14T21:42:00-07:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2013-05-14:linux/debugging-puppetmaster-with-foreman.html</id><summary type="html">&lt;p&gt;This is a little note for anyone trying to get some debugging out of
the puppetmaster when deploying with &lt;a class="reference external" href="http://theforeman.org"&gt;Foreman&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The trick, much as it is, is that Foreman is running puppet via
Apache; so if you're trying to start a &lt;tt class="docutils literal"&gt;puppet master&lt;/tt&gt; daemon
outside that it won't be able to bind to port 8140.  You thus want to
edit the config file Apache is using to launch puppet
&lt;tt class="docutils literal"&gt;/etc/puppet/rack/config.ru&lt;/tt&gt;.  It's probably pretty obvious what's
happening when you look in there; simply add&lt;/p&gt;
&lt;pre class="literal-block"&gt;
ARGV &amp;lt;&amp;lt; &amp;quot;--debug&amp;quot;
&lt;/pre&gt;
&lt;p&gt;and you will start to get debugging output.  One issue is that this
goes to syslog (&lt;tt class="docutils literal"&gt;/var/log/messages&lt;/tt&gt;) by default and is a lot of
output; so much so that it might get throttled.  Although you can
certainly reconfigure your syslog daemon to split out puppet logs, an
easier way is to just skip syslog while you're debugging.  Don't be
&lt;a class="reference external" href="https://projects.puppetlabs.com/issues/20173"&gt;fooled&lt;/a&gt; by config
options; simply add&lt;/p&gt;
&lt;pre class="literal-block"&gt;
ARGV &amp;lt;&amp;lt; &amp;quot;--logdest&amp;quot; &amp;lt;&amp;lt; &amp;quot;/var/log/puppet/puppet-master.debug&amp;quot;
&lt;/pre&gt;
&lt;p&gt;to the same file to get the logs going to a separate file.  Don't
forget to restart Apache so the changes stick.&lt;/p&gt;
</summary></entry><entry><title>Shared libraries and execute permissions</title><link href="http://www.technovelty.org/linux/shared-libraries-and-execute-permissions.html" rel="alternate" /><updated>2013-02-06T23:34:00-08:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2013-02-06:linux/shared-libraries-and-execute-permissions.html</id><summary type="html">&lt;p&gt;In the few discussions you can find on the web about shared-libraries
and execute-permissions, you can find a range of various opinions but
not a lot about what goes when you execute a library.&lt;/p&gt;
&lt;p&gt;The first thing to consider is how the execute-permission interacts
with the dynamic loader.  When mapping a library, the dynamic-loader
doesn't care about file-permissions; it cares about mapping specific
internal parts of the &lt;tt class="docutils literal"&gt;.so&lt;/tt&gt;.  Specifically, it wants to map the
&lt;tt class="docutils literal"&gt;PT_LOAD&lt;/tt&gt; segments as-per the permissions specified by the
program-header.  A random example:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
 $ readelf --segments /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0

Elf file type is DYN (Shared object file)
Entry point 0x77c00
There are 7 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x00000000001b5ea4 0x00000000001b5ea4  R E    200000
  LOAD           0x00000000001b6b60 0x00000000003b6b60 0x00000000003b6b60
                 0x0000000000028dc4 0x000000000002c998  RW     200000
&lt;/pre&gt;
&lt;p&gt;The permissions to load the code and data segments are given the
&lt;tt class="docutils literal"&gt;Flags&lt;/tt&gt; output.  Code has execute-permissions, and data has
write-permissions.  These flags are mapped into flags for the &lt;tt class="docutils literal"&gt;mmap&lt;/tt&gt;
call, which the loader then uses to map the various segments of the
file into memory.&lt;/p&gt;
&lt;p&gt;So, do you actually &lt;em&gt;need&lt;/em&gt; execute-permissions on the underlying file
to &lt;tt class="docutils literal"&gt;mmap&lt;/tt&gt; that segment as executable?  No, because you can read it.
If I can read it, then I can copy the segment to another area of
memory I have already mapped with &lt;tt class="docutils literal"&gt;PROT_EXEC&lt;/tt&gt; and execute it there
anyway.&lt;/p&gt;
&lt;p&gt;Googling suggests that some systems &lt;em&gt;do&lt;/em&gt; require execute-permissions
on a file if you want to directly &lt;tt class="docutils literal"&gt;mmap&lt;/tt&gt; pages from it with
&lt;tt class="docutils literal"&gt;PROT_EXEC&lt;/tt&gt; (and if you dig into the kernel source, there's an
ancient system call &lt;tt class="docutils literal"&gt;uselib&lt;/tt&gt; that looks like it comes from &lt;tt class="docutils literal"&gt;a.out&lt;/tt&gt;
days, given it talks about loading libraries at fixed addresses, that
also wants this).  This doesn't sound like a terrible hardening step;
I wouldn't be surprised if some hardening patches require it.  Maximum
compatability and historical-features such as &lt;tt class="docutils literal"&gt;a.out&lt;/tt&gt; also probably
explains why &lt;tt class="docutils literal"&gt;gcc&lt;/tt&gt; creates shared libraries with execute permissions
by default.&lt;/p&gt;
&lt;p&gt;Thus, should you feel like it, you can run a shared-library.
Something trivial will suffice:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;pre class="literal-block"&gt;
$ gcc -fPIC -shared -o libfoo.so foo.c

$ ./libfoo.so
Segmentation fault
&lt;/pre&gt;
&lt;p&gt;This is a little more interesting (to me anyway) to dig into.  At a
first pass, why does this even vaguely work?  That's easy -- an ELF
file is an ELF file, and the kernel is happy to map those &lt;tt class="docutils literal"&gt;PT_LOAD&lt;/tt&gt;
segments in and jump to the entry point for you:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ readelf --segments ./libfoo.so

Elf file type is DYN (Shared object file)
Entry point 0x570
There are 6 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x000000000000070c 0x000000000000070c  R E    200000
  LOAD           0x0000000000000710 0x0000000000200710 0x0000000000200710
                 0x0000000000000238 0x0000000000000240  RW     200000
&lt;/pre&gt;
&lt;p&gt;What's interesting here is that the shared-library has an entry point
(&lt;tt class="docutils literal"&gt;e_entry&lt;/tt&gt;) at all.  ELF defines it as:&lt;/p&gt;
&lt;blockquote&gt;
This member gives the virtual address to which the system first
transfers control, thus starting the process. If the file has no
associated entry point, this member holds zero.&lt;/blockquote&gt;
&lt;p&gt;First things first, where did that entry point come from?  The &lt;a class="reference external" href="http://sourceware.org/binutils/docs/ld/Entry-Point.html#Entry-Point"&gt;ld
manual&lt;/a&gt; tells us that the linker will set the entry point based upon
the following hierarchy:&lt;/p&gt;
&lt;blockquote&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-e&lt;/span&gt;&lt;/tt&gt; entry command-line option;&lt;/li&gt;
&lt;li&gt;the &lt;tt class="docutils literal"&gt;ENTRY(symbol)&lt;/tt&gt; command in a linker script;&lt;/li&gt;
&lt;li&gt;the value of a target specific symbol&lt;/li&gt;
&lt;li&gt;the address of the first byte of the &lt;tt class="docutils literal"&gt;.text&lt;/tt&gt; section, if present;&lt;/li&gt;
&lt;li&gt;The address 0.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;We know we're not specifying an entry point.  The &lt;tt class="docutils literal"&gt;ENTRY&lt;/tt&gt; command is
interesting; we can check our default-link script to see if that is
specified:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ ld --verbose | grep ENTRY
ENTRY(_start)
&lt;/pre&gt;
&lt;p&gt;Interesting.  Obviously we didn't specify a &lt;tt class="docutils literal"&gt;_start&lt;/tt&gt;; so do we have
one?  A bit of digging leads to the &lt;tt class="docutils literal"&gt;crt&lt;/tt&gt; files, for &lt;strong&gt;C&lt;/strong&gt; &lt;strong&gt;R&lt;/strong&gt;un-&lt;strong&gt;T&lt;/strong&gt;ime.  These are little object files automatically linked
in by &lt;tt class="docutils literal"&gt;gcc&lt;/tt&gt; that actually do the support-work to get a program to
the point that &lt;tt class="docutils literal"&gt;main&lt;/tt&gt; is ready to run.&lt;/p&gt;
&lt;p&gt;So, if we go and check out the &lt;tt class="docutils literal"&gt;crt&lt;/tt&gt; files, one can find a
definition of &lt;tt class="docutils literal"&gt;_start&lt;/tt&gt; in &lt;tt class="docutils literal"&gt;crt1.o&lt;/tt&gt;&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ nm /usr/lib/x86_64-linux-gnu/crt1.o
crt1.o:
0000000000000000 R _IO_stdin_used
0000000000000000 D __data_start
                 U __libc_csu_fini
                 U __libc_csu_init
                 U __libc_start_main
0000000000000000 T _start
0000000000000000 W data_start
                 U main
&lt;/pre&gt;
&lt;p&gt;But do we have that for our little shared-library?  We can get a feel
for what &lt;tt class="docutils literal"&gt;gcc&lt;/tt&gt; is linking in by examining the output of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-dumpspecs&lt;/span&gt;&lt;/tt&gt;.  Remembering &lt;tt class="docutils literal"&gt;gcc&lt;/tt&gt; is mostly just a driver that
calls out to other things, a``specs`` file is what &lt;tt class="docutils literal"&gt;gcc&lt;/tt&gt; uses to
determine which arguments pass around to various stages of a compile:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ gcc -dumpspecs
...
*startfile:
%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}}
 crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}
&lt;/pre&gt;
&lt;p&gt;The format isn't really important here (of course you can &lt;a class="reference external" href="http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Spec-Files.html#Spec-Files"&gt;read about
it&lt;/a&gt;); but the gist is that various flags, such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-static&lt;/span&gt;&lt;/tt&gt; or
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-pie&lt;/span&gt;&lt;/tt&gt; get passed different run-time initailisation helpers to
link-in.  But we can see that if we're creating a shared library we
won't be getting &lt;tt class="docutils literal"&gt;crt1.o&lt;/tt&gt;.  We can double-confirm this by checking
the output of &lt;tt class="docutils literal"&gt;gcc &lt;span class="pre"&gt;-v&lt;/span&gt;&lt;/tt&gt; (cut down for clarity).&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ gcc -v -fPIC -shared -o libfoo.so foo.c
Using built-in specs.
 ...
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/collect2 -shared -o libfoo.so
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbeginS.o
/tmp/ccRpsQU3.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtendS.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crtn.o
&lt;/pre&gt;
&lt;p&gt;So this takes us further down &lt;tt class="docutils literal"&gt;ld&lt;/tt&gt;'s entry-point logic to pointing
to the first bytes of &lt;tt class="docutils literal"&gt;.text&lt;/tt&gt;, which is where the entry-point comes
from.  So that solves the riddle of the entry point.&lt;/p&gt;
&lt;p&gt;There's one more weird thing you notice when you run the library,
which is the faulting address in &lt;tt class="docutils literal"&gt;kern.log&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
libfoo.so[8682]: segfault at 1 ip 0000000000000001 sp 00007fffcd63ec48 error 14 in libfoo.so[7f54c51fa000+1000]
&lt;/pre&gt;
&lt;p&gt;The first thing is decoding &lt;tt class="docutils literal"&gt;error&lt;/tt&gt;; 14 doesn't seem to have any
relation to anything.  Of course everyone has the &lt;a class="reference external" href="http://download.intel.com/products/processor/manual/325462.pdf"&gt;Intel 64
Architecture Manual&lt;/a&gt; (or &lt;a class="reference external" href="http://lxr.free-electrons.com/source/arch/x86/mm/fault.c#L23"&gt;mm/fault.c&lt;/a&gt; that also mentions the flags)
to decode this into &lt;tt class="docutils literal"&gt;1110&lt;/tt&gt; which means &amp;quot;no page found for a
user-mode write access with reserved-bits found to be set&amp;quot; (there's
another post in all that someday!).&lt;/p&gt;
&lt;p&gt;So why did we segfault at &lt;tt class="docutils literal"&gt;0x1&lt;/tt&gt;, which is an odd address to turn up?
Let's disassemble what actually happens when this starts.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="mh"&gt;00000000000004a0&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nf"&gt;call_gmon_start&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;:&lt;/span&gt;
&lt;span class="x"&gt; 4a0:   48 83 ec 08             sub    $0x8,%rsp&lt;/span&gt;
&lt;span class="x"&gt; 4a4:   48 8b 05 2d 03 20 00    mov    0x20032d(%rip),%rax        # 2007d8 &amp;lt;_DYNAMIC+0x190&amp;gt;&lt;/span&gt;
&lt;span class="x"&gt; 4ab:   48 85 c0                test   %rax,%rax&lt;/span&gt;
&lt;span class="x"&gt; 4ae:   74 02                   je     4b2 &amp;lt;call_gmon_start+0x12&amp;gt;&lt;/span&gt;
&lt;span class="x"&gt; 4b0:   ff d0                   callq  *%rax&lt;/span&gt;
&lt;span class="x"&gt; 4b2:   48 83 c4 08             add    $0x8,%rsp&lt;/span&gt;
&lt;span class="x"&gt; 4b6:   c3                      retq&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We're moving something in &lt;tt class="docutils literal"&gt;rax&lt;/tt&gt; and testing it; if true we call that
value, otherwise skip and &lt;tt class="docutils literal"&gt;retq&lt;/tt&gt;.  In this case, &lt;tt class="docutils literal"&gt;objdump&lt;/tt&gt; is
getting a bit confused telling us that &lt;tt class="docutils literal"&gt;2007d8&lt;/tt&gt; is related to
&lt;tt class="docutils literal"&gt;_DYNAMIC&lt;/tt&gt;; in fact we can check the relocations to see it's really
the value of &lt;tt class="docutils literal"&gt;__gmon_start__&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ readelf --relocs ./libfoo.so

Relocation section '.rela.dyn' at offset 0x3f0 contains 4 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000200810  000000000008 R_X86_64_RELATIVE                    0000000000200810
0000002007d8  000200000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
0000002007e0  000300000006 R_X86_64_GLOB_DAT 0000000000000000 _Jv_RegisterClasses + 0
0000002007e8  000400000006 R_X86_64_GLOB_DAT 0000000000000000 __cxa_finalize + 0

Relocation section '.rela.plt' at offset 0x450 contains 1 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000200808  000400000007 R_X86_64_JUMP_SLO 0000000000000000 __cxa_finalize + 0
&lt;/pre&gt;
&lt;p&gt;Thus &lt;tt class="docutils literal"&gt;call_gmon_start&lt;/tt&gt;, rather unsurprisingly, checks the value of
&lt;tt class="docutils literal"&gt;__gmon_start__&lt;/tt&gt; and calls it if it is set.  Presumably this is set
as part of profiling and called during library initialisation -- but
it is clearly not an initialiser by itself.  The &lt;tt class="docutils literal"&gt;retq&lt;/tt&gt; ends up
popping a value off the stack and jumping to it, which in this case
just happens to be &lt;tt class="docutils literal"&gt;0x1&lt;/tt&gt; -- which we can confirm with &lt;tt class="docutils literal"&gt;gdb&lt;/tt&gt; by
putting a breakpoint on the first text address and examining the
stack-pointer:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
(gdb) x/2g $rsp
0x7fffffffe7d8:        0x0000000000000000      0x0000000000000001
&lt;/pre&gt;
&lt;p&gt;So that gives us our ultimate failure.&lt;/p&gt;
&lt;p&gt;Of course, if you're clever, you can get around this and initalise
yourself correctly and actually make your shared-library do something
when executed.  The canonical example of this is &lt;tt class="docutils literal"&gt;libc.so&lt;/tt&gt; itself:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ /lib/x86_64-linux-gnu/libc-2.13.so
GNU C Library (Debian EGLIBC 2.13-37) stable release version 2.13, by Roland McGrath et al.
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
...
&lt;/pre&gt;
&lt;p&gt;You can trace through how this actually &lt;em&gt;does&lt;/em&gt; work in the same way as
we traced through why the trivial example &lt;em&gt;doesn't&lt;/em&gt; work.&lt;/p&gt;
&lt;p&gt;If you wondering my opinion on executable-bits for shared-libraries; I
would not give them execute permissions.  I can't see it does anything
but open the door to confusion.  However, understanding exactly why
the library segfaults the way it does actually ends up being a fun
little tour around various parts of the toolchain!&lt;/p&gt;
</summary></entry><entry><title>Position Independent Code and x86-64 libraries</title><link href="http://www.technovelty.org/c/position-independent-code-and-x86-64-libraries.html" rel="alternate" /><updated>2013-01-05T14:53:00-08:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2013-01-05:c/position-independent-code-and-x86-64-libraries.html</id><summary type="html">&lt;p&gt;&lt;em&gt;A comment pointed out that the original article from 2008 made a few
simplifications that were a bit misleading, so I have taken some time
to update this.  Thanks for the feedback.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If you've ever tried to link non-position independent code into a shared
library on x86-64, you should have seen a fairly cryptic error about
invalid relocations and missing symbols. Hopefully this will clear it up
a little.&lt;/p&gt;
&lt;p&gt;Let's start with a small program to illustrate.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;global&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;global&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Firstly, inspect the disassembley of this function:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$gcc -c function.c

$objdump --disassemble function.o

0000000000000000 &amp;lt;function&amp;gt;:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   8b 05 00 00 00 00       mov    0x0(%rip),%eax        # d &amp;lt;function+0xd&amp;gt;
   d:   03 45 fc                add    -0x4(%rbp),%eax
  10:   c9                      leaveq
  11:   c3                      retq
&lt;/pre&gt;
&lt;p&gt;Lets just go through that for clarity:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;strong&gt;0&lt;/strong&gt;,&lt;strong&gt;1&lt;/strong&gt;: save &lt;tt class="docutils literal"&gt;rbp&lt;/tt&gt; to the stack and save the stack pointer
(&lt;tt class="docutils literal"&gt;rsp&lt;/tt&gt;) to &lt;tt class="docutils literal"&gt;rbp&lt;/tt&gt;. This common stanza is setting up the &lt;em&gt;frame
pointer&lt;/em&gt;, which is essentially a rule used by debuggers (mostly) to
keep track of the base of the stack. It's not important for now.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;4&lt;/strong&gt;:Move the value from &lt;tt class="docutils literal"&gt;edi&lt;/tt&gt; to 4 bytes below the stack pointer.
This is moving the first argument (&lt;tt class="docutils literal"&gt;int i&lt;/tt&gt;) into the &amp;quot;red-zone&amp;quot;, a
128-byte scratch area each function has reserved below the stack
pointer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;7&lt;/strong&gt;,&lt;strong&gt;d&lt;/strong&gt;: Move the value at offset 0 from the current
instruction pointer (&lt;tt class="docutils literal"&gt;rip&lt;/tt&gt;) into &lt;tt class="docutils literal"&gt;eax&lt;/tt&gt; (by convention the return
value is left in register &lt;tt class="docutils literal"&gt;eax&lt;/tt&gt;). Then add the incoming argument to
it (retrieved from the scratch area); i.e. &lt;tt class="docutils literal"&gt;return global + i&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The IP relative move is really the trick here. We know from the code
that it has to move the value of the &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; variable here. The zero
value is simply a place holder - the compiler currently does not
determine the required address (i.e. how far away from the instruction
pointer the memory holding the &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; variable is). It leaves behind
a &lt;em&gt;relocation&lt;/em&gt; -- a note that says to the linker &amp;quot;you should determine
the correct address of &lt;em&gt;foo&lt;/em&gt; (&lt;tt class="docutils literal"&gt;global&lt;/tt&gt; in our case), and then patch
this bit of the code to point to that addresss&amp;quot;.&lt;/p&gt;
&lt;img alt="Relocations with addend" class="align-center" src="http://www.technovelty.org/static/images/pic.png" /&gt;
&lt;p&gt;The top portion of the image above gives some idea of how it works. We
can examine relocations in binaries with the &lt;tt class="docutils literal"&gt;readelf&lt;/tt&gt; tool.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ readelf --relocs ./function.o

Relocation section '.rela.text' at offset 0x518 contains 1 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000009  000800000002 R_X86_64_PC32     0000000000000000 global + fffffffffffffffc
&lt;/pre&gt;
&lt;p&gt;There are many different types of relocations for different situations;
the exact rules for different relocation types are described in the ABI
documentation for the architecture. The &lt;tt class="docutils literal"&gt;R_X86_64_PC32&lt;/tt&gt; relocation is
defined as &amp;quot;the base of the section the symbol is within, plus the
symbol value, plus the addend&amp;quot;. The addend makes it look more tricky
than it is; remember that when an instruction is executing the
instruction pointer points to the &lt;em&gt;next&lt;/em&gt; instruction to be executed.
Therefore, to correctly find the data relative to the instruction
pointer, we need to subtract the extra. This can be seen more clearly
when layed out in a linear fashion (as in the bottom of the above
diagram).&lt;/p&gt;
&lt;p&gt;If you try and build a shared object (dynamic library) with an object
file with this type of relocation, you should get something like:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ gcc -shared function.c
/usr/bin/ld: /tmp/ccQ2ttcT.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/tmp/ccQ2ttcT.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
&lt;/pre&gt;
&lt;p&gt;If you look back at the disassembly, we notice that the
&lt;tt class="docutils literal"&gt;R_X86_64_32&lt;/tt&gt; relocation has left only 4-bytes (32-bits) of space
left for the relocation entry (the zeros in &lt;tt class="docutils literal"&gt;7: 8b 15 00 00 00 00&lt;/tt&gt;).&lt;/p&gt;
&lt;p&gt;So why does this matter when you're creating a shared library?  The
first thing to remember is that in a shared library situation, we can
not depend on the local value of &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; actually being the one we
want.  Consider the following example, where we override the value of
global with a &lt;tt class="docutils literal"&gt;LD_PRELOAD&lt;/tt&gt; library.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ cat function.c
int global = 100;

int function(int i) {
    return i + global;
}

$ gcc -fPIC -shared -o libfunction.so function.c

$ cat preload.c
int global = 200;

$ gcc -shared preload.c -o libpreload.so

$ cat program.c
#include &amp;lt;stdio.h&amp;gt;

int function(int i);

int main(void) {
   printf(&amp;quot;%d\n&amp;quot;, function(10));
}

$ gcc -L. -lfunction program.c -o program

$ LD_LIBRARY_PATH=. ./program
110

$ LD_PRELOAD=libpreload.so LD_LIBRARY_PATH=. ./program
210
&lt;/pre&gt;
&lt;p&gt;If the code in &lt;tt class="docutils literal"&gt;libfunction.so&lt;/tt&gt; were to have a fixed offset into its
own data section, it will not be able to be overridden at run-time by
the value provided by &lt;tt class="docutils literal"&gt;libpreload.so&lt;/tt&gt;.  Additionally, there are only
4-bytes available to patch in for the address of &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; -- since a
shared library could conceivably be loaded anywhere in the 64-bit
(8-byte) address space we therefore need 8-bytes of space to cover
ourselves for all possible addresses &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; might turn up at.&lt;/p&gt;
&lt;p&gt;The two basic possibilities for an object file are to be either linked
into an executable or linked into a shared-library.  In the executable
case, the value of &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; will be in the exectuable's data
section, which should definitely be reachable with a 32-bit offset of
the current instruction-pointer.  The instruction-pointer relative
address can simply be patched in and the executable is finalised.&lt;/p&gt;
&lt;p&gt;But what about the shared-library case, where we know the value of
&lt;tt class="docutils literal"&gt;global&lt;/tt&gt; could essentially be anywhere within the 64-bit address
space?  It is possible to leave 8-bytes of space for the address of
&lt;tt class="docutils literal"&gt;global&lt;/tt&gt;, by telling gcc to use the &lt;em&gt;large-code&lt;/em&gt; model.  e.g.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ gcc -c -mcmodel=large function.c

$ objdump --disassemble ./function.o

./function.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 &amp;lt;function&amp;gt;:
   0:  55                      push   %rbp
   1:  48 89 e5                mov    %rsp,%rbp
   4:  89 7d fc                mov    %edi,-0x4(%rbp)
   7:  48 b8 00 00 00 00 00    movabs $0x0,%rax
   e:  00 00 00
  11:  8b 10                   mov    (%rax),%edx
  13:  8b 45 fc                mov    -0x4(%rbp),%eax
  16:  01 d0                   add    %edx,%eax
  18:  5d                      pop    %rbp
  19:  c3                      retq
&lt;/pre&gt;
&lt;p&gt;However, this creates a problem if you really want to &lt;em&gt;share&lt;/em&gt; this
code.  By having to patch in an address of &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; directly, this
means the run-time code above does &lt;strong&gt;not&lt;/strong&gt; remain unchanged.  Two
separate processes therefore can't share this code -- they each need
separate copies that are identical but for their own addresses of
&lt;tt class="docutils literal"&gt;global&lt;/tt&gt; patched into it.&lt;/p&gt;
&lt;p&gt;By enabling &lt;em&gt;Position Independent Code&lt;/em&gt; (PIC, with the flag &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-fPIC&lt;/span&gt;&lt;/tt&gt;)
you can ensure the code remains share-able.  PIC means that the output
binary does not expect to be loaded at a particular base address, but
is happy being put anywhere in memory (compare the output of &lt;tt class="docutils literal"&gt;readelf
&lt;span class="pre"&gt;--segments&lt;/span&gt;&lt;/tt&gt; on a binary such as &lt;tt class="docutils literal"&gt;/bin/ls&lt;/tt&gt; to that of any shared
library). This is obviously critical for implementing lazy-loading
(i.e. only loaded when required) shared-libraries, where you may have
many libraries loaded in essentially any order at any location.&lt;/p&gt;
&lt;p&gt;Of course, any problem in computer science can be solved with a layer
of abstraction and that is essentially what is done when compiling
with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-fPIC&lt;/span&gt;&lt;/tt&gt;. To examine this case, let's see what happens with PIC
turned on.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ gcc -fPIC -shared -c  function.c

$ objdump --disassemble ./function.o

./function.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 &amp;lt;function&amp;gt;:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   48 8b 05 00 00 00 00    mov    0x0(%rip),%rax        # e &amp;lt;function+0xe&amp;gt;
   e:   8b 00                   mov    (%rax),%eax
  10:   03 45 fc                add    -0x4(%rbp),%eax
  13:   c9                      leaveq
  14:   c3                      retq
&lt;/pre&gt;
&lt;p&gt;It's &lt;em&gt;almost&lt;/em&gt; the same! We setup the frame pointer with the first two
instructions as before. We push the first argument into memory in the
pre-allocated &amp;quot;red-zone&amp;quot; as before. Then, however, we do an IP relative
load of an address into &lt;tt class="docutils literal"&gt;rax&lt;/tt&gt;. Next we de-reference this into &lt;tt class="docutils literal"&gt;eax&lt;/tt&gt;
(e.g. &lt;tt class="docutils literal"&gt;eax = *rax&lt;/tt&gt; in C) before adding the incoming argument to it and
returning.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ readelf --relocs ./function.o

Relocation section '.rela.text' at offset 0x550 contains 1 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
00000000000a  000800000009 R_X86_64_GOTPCREL 0000000000000000 global + fffffffffffffffc
&lt;/pre&gt;
&lt;p&gt;The magic here is again in the relocations. Notice this time we have a
&lt;tt class="docutils literal"&gt;P_X86_64_GOTPCREL&lt;/tt&gt; relocation. This says &amp;quot;replace the data at offset
&lt;tt class="docutils literal"&gt;0xa&lt;/tt&gt; with the &lt;em&gt;global offset table&lt;/em&gt; (GOT) entry of &lt;tt class="docutils literal"&gt;global&lt;/tt&gt;.&lt;/p&gt;
&lt;img alt="Global Offset Table operation with data variables" class="align-center" src="http://www.technovelty.org/static/images/got.png" /&gt;
&lt;p&gt;As shown above, the GOT ensures the abstraction required so symbols
can be diverted as expected. Each entry is essentially a pointer to
the real data (hence the extra dereference in the code above). Since
we can assume the GOT is at a fixed offset from the program code
within plus or minus 2Gib, the code can use a 32-bit IP relative
address to gain access to the table entries.&lt;/p&gt;
&lt;p&gt;So, taking a look a the final shared-library binary we see a final
offset hard-coded&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ gcc -shared -fPIC -o libfunction.so function.c

$ objdump --disassemble ./libfunction.so

00000000000006b0 &amp;lt;function&amp;gt;:
 6b0:  55                      push   %rbp
 6b1:  48 89 e5                mov    %rsp,%rbp
 6b4:  89 7d fc                mov    %edi,-0x4(%rbp)
 6b7:  48 8b 05 8a 02 20 00    mov    0x20028a(%rip),%rax        # 200948 &amp;lt;_DYNAMIC+0x1d8&amp;gt;
 6be:  8b 10                   mov    (%rax),%edx
 6c0:  8b 45 fc                mov    -0x4(%rbp),%eax
 6c3:  01 d0                   add    %edx,%eax
 6c5:  5d                      pop    %rbp
 6c6:  c3                      retq
 6c7:  90                      nop
&lt;/pre&gt;
&lt;p&gt;Every process who wants to share this code just needs to make sure
they have their unique address of &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; at &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;0x20028a(%rip)&lt;/span&gt;&lt;/tt&gt;.
Since each process has a separate address-space, this means they can
all have different values for &lt;tt class="docutils literal"&gt;global&lt;/tt&gt; but share the same code!&lt;/p&gt;
&lt;p&gt;Thus the default of the &lt;em&gt;small-code&lt;/em&gt; model is sensible.  It is
exceedingly rare for an executable to need more than 4-byte offsets
for a relative access to a variable in it's data region, so using a
full 8-byte value would just be a waste of space.  Although leaving
8-bytes would allow access to the variable anywhere in the 64-bit
address space; when building a shared library, you really want to use
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-fPIC&lt;/span&gt;&lt;/tt&gt; to ensure the library can actually be &lt;em&gt;shared&lt;/em&gt;, which
introduces a different relocation and access to data via the GOT.&lt;/p&gt;
&lt;p&gt;This should explain why &lt;tt class="docutils literal"&gt;gcc &lt;span class="pre"&gt;-shared&lt;/span&gt; function.c&lt;/tt&gt; works on x86-32,
but does not work on x86-64. Inspecting the code reveals why:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ objdump --disassemble ./function.o
00000000 &amp;lt;function&amp;gt;:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   a1 00 00 00 00          mov    0x0,%eax
   8:   03 45 08                add    0x8(%ebp),%eax
   b:   5d                      pop    %ebp
   c:   c3                      ret
$ readelf --relocs ./function.o
Relocation section '.rel.text' at offset 0x2ec contains 1 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000004  00000701 R_386_32          00000000   global
&lt;/pre&gt;
&lt;p&gt;We start out the same, with the first two instructions setting up the
frame pointer. However, next we load a memory value into &lt;tt class="docutils literal"&gt;eax&lt;/tt&gt; -- as
we can see from the relocation information, the address of &lt;tt class="docutils literal"&gt;global&lt;/tt&gt;.
Next we add the incoming argument from the stack (&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;0x8(%ebp)&lt;/span&gt;&lt;/tt&gt;) to
the value in this memory location; implicitly dereferencing it.  But
since we only have a 32-bit address-space, the 4-bytes allocated is
enough to access any possible address.  So while this can work, you're
not creating position-independent code and hence not enabling
code-sharing.&lt;/p&gt;
&lt;p&gt;The disadvantage of PIC code is that you require &amp;quot;bouncing&amp;quot; through
the GOT, which requires more loads and reads to find an address than
directly referencing it.  However, if your program is at the point
that this is becoming a performance issue you're probably not reading
this blog!&lt;/p&gt;
&lt;p&gt;Hopefully, this helps clear up that possibly cryptic error message.
Further searches around position-independent code, global-offset
tables and code-sharing should also yield you more information if it
remains unclear.&lt;/p&gt;
</summary></entry><entry><title>Tagging handbrake output for iTunes</title><link href="http://www.technovelty.org/linux/tagging-handbrake-output-for-itunes.html" rel="alternate" /><updated>2013-01-03T22:06:00-08:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2013-01-03:linux/tagging-handbrake-output-for-itunes.html</id><summary type="html">&lt;p&gt;If you rip your DVD's with a unix-y name like
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;longish-show-name-season-01-01.m4v&lt;/span&gt;&lt;/tt&gt; then the iPad and iPhone video
player only shows the prefix of the file-name to it's string limit, so
you only ever see &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;longish-show-na&lt;/span&gt;&lt;/tt&gt; and can never tell what episode
is what in the overview list.&lt;/p&gt;
&lt;p&gt;You can edit the files in iTunes to add meta-data information, but
that's annoying and non-unix-y.  It is of course easy, but I went down
several dead-ends with tools that didn't recognise file-extensions,
corrupted files and plain didn't work.&lt;/p&gt;
&lt;p&gt;The package I had most success with is &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mp4v2-utils&lt;/span&gt;&lt;/tt&gt; and the
&lt;tt class="docutils literal"&gt;mp4tags&lt;/tt&gt; utility.  The help is fairly self-explanatory, but
something like&lt;/p&gt;
&lt;pre class="literal-block"&gt;
mp4tags -show &amp;quot;Show Name&amp;quot; -season &amp;lt;season&amp;gt; -episode &amp;lt;episode&amp;gt; -type &amp;quot;tvshow&amp;quot;  file.m4v
&lt;/pre&gt;
&lt;p&gt;will mean that when you import your file into iTunes it displays
correctly as a TV show (and not a movie), is grouped correctly into
series and has a name you can see.&lt;/p&gt;
&lt;p&gt;Unfortunately, it doesn't seem my Nexus 7 tablet picks up these tags.
And both players interfaces remain terrible at indicating which
episodes have been watched and which have not.  But this has allowed
me to re-visit my old box-set of &lt;a class="reference external" href="http://en.wikipedia.org/wiki/Yes_Minister"&gt;Yes Minister&lt;/a&gt; which, despite a few odd
references to &lt;a class="reference external" href="http://en.wikipedia.org/wiki/Quango"&gt;Quango's&lt;/a&gt; still
seems as relevant as ever!&lt;/p&gt;
</summary></entry><entry><title>Upgrading Samsung Series 9 Laptop BIOS without Windows</title><link href="http://www.technovelty.org/linux/upgrading-samsung-series-9-laptop-bios-without-windows.html" rel="alternate" /><updated>2012-11-21T23:16:00-08:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2012-11-21:linux/upgrading-samsung-series-9-laptop-bios-without-windows.html</id><summary type="html">&lt;p&gt;Having recently acquired a Samsung Series 9 laptop, I'd like to update
the BIOS.  This is proving rather difficult without Windows.&lt;/p&gt;
&lt;p&gt;The first thing you download from the Samsung website isn't actually
BIOS update at all; it's a Windows application which then goes and
finds a BIOS update for your machine.&lt;/p&gt;
&lt;p&gt;Some time with a Windows VM, tcpdump and you'll end up at
&lt;a class="reference external" href="http://sbuservice.samsungmobile.com/"&gt;http://sbuservice.samsungmobile.com/&lt;/a&gt;.  I'll leave interested
parties to play around at that website, but with a little fiddling you
can find the BIOS you're looking for.  In this case, it is
&lt;a class="reference external" href="http://sbuservice.samsungmobile.com/upload/BIOSUpdateItem/ITEM_20121024_754_WIN_P08AAH.exe"&gt;ITEM_20121024_754_WIN_P08AAH.exe&lt;/a&gt;
which is the latest version, P08AAH.&lt;/p&gt;
&lt;p&gt;Unfortunately, this still won't run in any useful way -- in a
virtual-machine it errors out saying the battery isn't connected.  On
previous endeavors such as this, running various incarnations of
&lt;tt class="docutils literal"&gt;zip&lt;/tt&gt; on the &lt;tt class="docutils literal"&gt;.exe&lt;/tt&gt; file has found interesting components, but not
in this case.  Not to be deterred, some work with &lt;tt class="docutils literal"&gt;strings&lt;/tt&gt; will
yield the following&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ strings ./ITEM_20121024_754_WIN_P08AAH.exe | grep Copyright
inflate 1.2.7 Copyright 1995-2012 Mark Adler
deflate 1.2.7 Copyright 1995-2012 Jean-loup Gailly and Mark Adler
&lt;/pre&gt;
&lt;p&gt;Now this is interesting, because it tells us that the components
aren't using some fancy compression scheme we have no hope of figuring
out.  Checking &lt;a class="reference external" href="http://tools.ietf.org/html/rfc1952"&gt;RFC1952&lt;/a&gt; we see
the following header mentioned&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Each member has the following structure:

+---+---+---+---+---+---+---+---+---+---+
|ID1|ID2|CM |FLG|     MTIME     |XFL|OS | (more--&amp;gt;)
+---+---+---+---+---+---+---+---+---+---+
&lt;/pre&gt;
&lt;p&gt;We can easily look for that in the file.  Expanding the values, what
we're looking for is &lt;tt class="docutils literal"&gt;0x1f 0x8b 0x08 0x0 0x0 0x0 0x0 0x0 0x0 0x0&lt;/tt&gt;
which represents the compressed stream (the &lt;tt class="docutils literal"&gt;OS&lt;/tt&gt; byte is &lt;tt class="docutils literal"&gt;0xb&lt;/tt&gt; for
NTFS which we can just ignore). A quick &lt;a class="reference external" href="http://www.wienand.org/junkcode/samsung-bios/look-for-headers.c"&gt;hack&lt;/a&gt;
will get that done and give us the offsets we need&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ ./look-for-headers
Found potential header at 455212
Found potential header at 677136
Found potential header at 1293930
Found potential header at 1294980
Found potential header at 2771193
Found potential header at 2779874
Found potential header at 2789142
Found potential header at 2935778
Found potential header at 3078639
Found potential header at 3083039
Found potential header at 3338552
Found potential header at 3339443
Found potential header at 3352440
Found potential header at 3366757
Found potential header at 3391734
Found potential header at 3444255
Found potential header at 3494372
&lt;/pre&gt;
&lt;p&gt;So, we can feed this into a shell script and extract the streams&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nv"&gt;FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ITEM_20121024_754_WIN_P08AAH.exe

&lt;span class="k"&gt;for &lt;/span&gt;offset in 455212 677136 1293930 1294980 &lt;span class="se"&gt;\&lt;/span&gt;
    2771193 2779874 2789142 2935778 3078639 &lt;span class="se"&gt;\&lt;/span&gt;
    3083039 3338552 3339443 3352440 3366757 &lt;span class="se"&gt;\&lt;/span&gt;
    3391734 3444255 3494372
&lt;span class="k"&gt;do&lt;/span&gt;
&lt;span class="k"&gt;    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;create $offset.gz&amp;quot;&lt;/span&gt;
    dd &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$FILE&lt;/span&gt;  &lt;span class="nv"&gt;bs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 &lt;span class="nv"&gt;skip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$offset&lt;/span&gt; | zcat &amp;gt; &lt;span class="nv"&gt;$offset&lt;/span&gt;.data
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We don't have to worry about the end-point, because &lt;tt class="docutils literal"&gt;zcat&lt;/tt&gt; will
handily stop outputting and ignore junk at the end of the stream.
That will yield the following output files:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ md5sum *
c014e900b02ae39d6e574c63154809b2  1293930.data
982747cd5215c7d51fa1fba255557fef  1294980.data
942c6a8332d5dd06d8f4b2a9cb386ff4  2771193.data
d98d2f80b94f70780b46d1f079a38d93  2779874.data
33ce3174b0e205f58c7cedc61adc6f32  2789142.data
1d6851288f40fa95895b2fadee8a8b82  2935778.data
3a0a9b2fa9b40bdf5721068acde351ce  3078639.data
fc166141d06934e9c7238326f18a4e68  3083039.data
8cf7f86f84afa81363d3cef44b257d50  3338552.data
85e09e18fd495599041e13b4197e1746  3339443.data
0c72004eae0131d52817f23faf8a46a7  3352440.data
26347f93721e7ac49134a17104f56090  3366757.data
26973e301da4948a0016765e40700e8c  3391734.data
c8e4675cda3aa367d523a471bb205fba  3444255.data
7bf5ea753d4cc056b9462a02ac51b160  3494372.data
a2c6562f7f43aaa0e636cc1995b7ee3d  455212.data
d5c91b007dac3a5c6317f8a8833f9304  677136.data
65c0dcd1e7e9f8f87a8e9fb706eb8768  ITEM_20121024_754_WIN_P08AAH.exe

$ file *
1293930.data:                     MS-DOS executable
1294980.data:                     data
2771193.data:                     PE32 executable (native) Intel 80386, for MS Windows
2779874.data:                     PE32+ executable (native) x86-64, for MS Windows
2789142.data:                     PE32 executable (console) Intel 80386 (stripped to external PDB), for MS Windows
2935778.data:                     PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows
3078639.data:                     MS-DOS executable
3083039.data:                     PE32 executable (GUI) Intel 80386, for MS Windows
3338552.data:                     ASCII text, with CRLF line terminators
3339443.data:                     PE32 executable (native) Intel 80386, for MS Windows
3352440.data:                     PE32+ executable (native) x86-64, for MS Windows
3366757.data:                     PE32 executable (DLL) (GUI) Intel 80386, for MS Windows
3391734.data:                     PE32 executable (GUI) Intel 80386, for MS Windows
3444255.data:                     PE32 executable (GUI) Intel 80386, for MS Windows
3494372.data:                     PE32 executable (DLL) (GUI) Intel 80386, for MS Windows
455212.data:                      PE32 executable (DLL) (console) Intel 80386, for MS Windows
677136.data:                      data
ITEM_20121024_754_WIN_P08AAH.exe: PE32 executable (GUI) Intel 80386, for MS Windows
&lt;/pre&gt;
&lt;p&gt;This is where I get a bit stuck.  There is clearly a bunch of WinFlash
stuff that Wine kind-of recognises, but so far I haven't managed to
figure out how I might go about flashing this from a DOS boot disk.&lt;/p&gt;
&lt;p&gt;So far I know it's a &amp;quot;Phoenix SecureCore Tiano&amp;quot; based BIOS, and there
is some indication that there is a UEFI app to flash the BIOS, but
unfortunately I didn't set the laptop up with UEFI (note to anyone
setting this up; probably a good idea to do that).&lt;/p&gt;
&lt;p&gt;I believe that if you have Windows, you can halt the install part-way
through and see these files extracted in a temp directory.  Possibly
if we can put some file-names to the blobs it might help.&lt;/p&gt;
&lt;p&gt;There may also be a much easier way to do this that I'm completely
missing.  If anything comes up, I'll update this post; hopefully a
future Googlenaut who enjoys BIOS hacking will drop a comment...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update 2012-11-27&lt;/strong&gt; I'm unfortunately less optimistic about being
able to update this.&lt;/p&gt;
&lt;p&gt;Certainly the files identified above as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MS-DOS&lt;/span&gt; executable&lt;/tt&gt; are EFI
binaries; however they are both 32-bit binaries.  You can easily get
an EFI shell up and running by putting a 64-bit &lt;a class="reference external" href="https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2/ShellBinPkg/UefiShell/X64/Shell.efi"&gt;EFI shell binary&lt;/a&gt;
on a FAT formatted USB disk in the &lt;tt class="docutils literal"&gt;efi/boot/bootx64.efi&lt;/tt&gt; but it
will not run either of those binaries, presumably because they're
32-bit.  A 32-bit EFI binary doesn't seem to be recognised for boot,
so no luck there.&lt;/p&gt;
&lt;p&gt;A comment also mentioned some issues with Samsung's and UEFI boot &lt;a class="reference external" href="https://bugs.launchpad.net/ubuntu-cdimage/+bug/1040557"&gt;in
this bug&lt;/a&gt;
though it's not clear what models are affected.&lt;/p&gt;
&lt;p&gt;Another comment mentioned &lt;a class="reference external" href="http://code.google.com/p/binwalk/"&gt;binwalk&lt;/a&gt; which also does a nice job of
finding the embedded zip streams.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ strings ./1293930.data
...
RSDS
D:\flash\Source\ENV\src\Efi863\edk\phoenix\edk\Sample\Platform\DUET\ldr16\IA32\oemslp20.pdb
oemslp20.dll
InitializeDriver
2n3u3

$ strings ./3078639.data
en-us;zh-Hans
slp20.marker
slp20.size
slp20.offset
slp20.offset2
read marker fail!
Marker file '%s''s size(%d)!=%d
Not a valided OemMarker!
Update image data error.
marker file is updated.
marker file size too big!
Not a valided OemMarker in ROM.
Update image data error.
SLP marker is reserved.
-PROT
Prot flag is found, mProgFlag is true.
Prot flag is not found, mProgFlag is false.
Please specify input file name.
-PROT
slpFile
OEM Activation 2.0 processing
Failed to Locate Flash Support protocol.
...
D:\flash\Source\ENV\src\Efi863\edk\phoenix\edk\Sample\Platform\DUET\ldr16\IA32\SLP20.pdb
&lt;/pre&gt;
</summary></entry><entry><title>Blog format update</title><link href="http://www.technovelty.org/personal/blog-format-update.html" rel="alternate" /><updated>2012-10-19T21:43:00-07:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2012-10-19:personal/blog-format-update.html</id><summary type="html">&lt;p&gt;7 years, 8 months, 17 days, 22 hours and 25 minutes ago I wrote the
first post for this blog, inspired by a talk by Andrew Tridgell at
linux.conf.au 2004 on the value of a personal &amp;quot;junkcode&amp;quot; repository.
I know this because I've recently gone through considerable effort to
change the format of the blog and keeping the timestamps in sync was
one challenge.&lt;/p&gt;
&lt;p&gt;I've now updated to use &lt;a class="reference external" href="http://docs.getpelican.com/en/3.0/"&gt;Pelican&lt;/a&gt;,
massaged all the old posts into &lt;a class="reference external" href="http://docutils.sourceforge.net/rst.html"&gt;reStructuredText&lt;/a&gt;, done a fair bit of HTML
and CSS hacking and hopefully redirected all the old URLs to the
correct place.  Old comments didn't make the cut, but there wasn't
much value there and I'm happy to outsouce to &lt;a class="reference external" href="http://disqus.com/"&gt;Disqus&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;How things have changed!  As I was writing the CSS to round the
corners of some elements, I remembered back to cribbing from &lt;a class="reference external" href="http://www.slashdot.org"&gt;Slashdot&lt;/a&gt; HTML to do the same thing -- except in
those days you used a table with blank elements in each corner with 4
different rounded-corner gif files!&lt;/p&gt;
&lt;p&gt;Yet I'm still writing in emacs, so some things will forever remain the
same I guess.&lt;/p&gt;
</summary></entry><entry><title>Investigating the Python bound method</title><link href="http://www.technovelty.org/python/investigating-the-python-bound-method.html" rel="alternate" /><updated>2012-03-06T23:46:00-08:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2012-03-06:python/investigating-the-python-bound-method.html</id><summary type="html">&lt;p&gt;You work with Python for a while and you'll become familiar with
printing a method and getting&lt;/p&gt;
&lt;pre class="literal-block"&gt;
&amp;lt;bound method Foo.function of &amp;lt;__main__.Foo instance at 0xb736960c&amp;gt;&amp;gt;
&lt;/pre&gt;
&lt;p&gt;I think there is room for one more explanation on the internet, since
I've never seen it diagrammed out (maybe for good reason!).&lt;/p&gt;
&lt;img alt="An illustration of Python bound methods" class="align-center" src="http://www.technovelty.org/static/images/python-bound-method.png" /&gt;
&lt;p&gt;In the above diagram on the left, we have the fairly simple conceptual
model of a class with a function. One naturally tends to think of the
function as a part of the class and your instance calls into that
function. This is conceptually correct, but a little abstracted from
what's actually happening.&lt;/p&gt;
&lt;p&gt;The right attempts to illustrate the underlying process in some more
depth. The first step, on the top right, is building something like the
following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
       &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;hi!&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As this illustrates, the above code results in two things happening;
firstly a &lt;em&gt;function object&lt;/em&gt; for &lt;tt class="docutils literal"&gt;function&lt;/tt&gt; is created and secondly the
&lt;tt class="docutils literal"&gt;__dict__&lt;/tt&gt; attribute of the class is given a key &lt;tt class="docutils literal"&gt;function&lt;/tt&gt; that
points to this function object.&lt;/p&gt;
&lt;p&gt;Now the thing about this function object is that it implements the
&lt;em&gt;descriptor protocol&lt;/em&gt;. In short, if an object implements a &lt;tt class="docutils literal"&gt;__get__&lt;/tt&gt;
function; then when that object is accessed as an attribute of an object
the &lt;tt class="docutils literal"&gt;__get__&lt;/tt&gt; function is called. You can read up on the descriptor
protocol, but the important part to remember is that it passes in the
&lt;em&gt;context&lt;/em&gt; from which it is called; that is the object that is calling
the function.&lt;/p&gt;
&lt;p&gt;So, for example, when we then do the following:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;what happens is that we get the attribute &lt;tt class="docutils literal"&gt;function&lt;/tt&gt; of &lt;tt class="docutils literal"&gt;f&lt;/tt&gt; and then
call it. &lt;tt class="docutils literal"&gt;f&lt;/tt&gt; above doesn't actually know anything about &lt;tt class="docutils literal"&gt;function&lt;/tt&gt;
as such — what it does know is its class inheritance and so Python goes
searching the parent's class &lt;tt class="docutils literal"&gt;__dict__&lt;/tt&gt; to try and find the
&lt;tt class="docutils literal"&gt;function&lt;/tt&gt; attribute. It finds this, and as per the descriptor
protocol when the attribute is accessed it calls the &lt;tt class="docutils literal"&gt;__get__&lt;/tt&gt;
function of the underlying function object.&lt;/p&gt;
&lt;p&gt;What happens now is that the function's &lt;tt class="docutils literal"&gt;__get__&lt;/tt&gt; method returns
essentially a wrapper object that stores the information to &lt;em&gt;bind&lt;/em&gt; the
function to the object. This wrapper object is of type
&lt;tt class="docutils literal"&gt;types.MethodType&lt;/tt&gt; and you can see it stores some important attributes
in the object — &lt;tt class="docutils literal"&gt;im_func&lt;/tt&gt; which is the function to call, and
&lt;tt class="docutils literal"&gt;im_self&lt;/tt&gt; which is the object who called it. Passing the object
through to &lt;tt class="docutils literal"&gt;im_self&lt;/tt&gt; is how &lt;tt class="docutils literal"&gt;function&lt;/tt&gt; gets it's first &lt;tt class="docutils literal"&gt;self&lt;/tt&gt;
argument (the calling object).&lt;/p&gt;
&lt;p&gt;So when you print the value of &lt;tt class="docutils literal"&gt;f.function()&lt;/tt&gt; you see it report itself
as a &lt;em&gt;bound method&lt;/em&gt;. So hopefully this illustrates that a bound method
is a just a special object that knows how to call an underlying function
with context about the object that's calling it.&lt;/p&gt;
&lt;p&gt;To try and make this a little more concrete, consider the following
program:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;types&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;hi!&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c"&gt;# this is a function object&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__dict__&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;function&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c"&gt;# this is a method as returned by&lt;/span&gt;
&lt;span class="c"&gt;#   Foo.__dict__[&amp;#39;function&amp;#39;].__get__()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;

&lt;span class="c"&gt;# we can check that this is an instance of MethodType&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MethodType&lt;/span&gt;

&lt;span class="c"&gt;# the im_func field of the MethodType is the underlying function&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;im_func&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__dict__&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;function&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c"&gt;# these are the same object&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;im_self&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Running this gives output something like&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ python ./foo.py
&amp;lt;function function at 0xb73540d4&amp;gt;
&amp;lt;bound method Foo.function of &amp;lt;__main__.Foo instance at 0xb736960c&amp;gt;&amp;gt;
True
&amp;lt;function function at 0xb73540d4&amp;gt;
&amp;lt;function function at 0xb73540d4&amp;gt;
&amp;lt;__main__.Foo instance at 0xb72c060c&amp;gt;
&amp;lt;__main__.Foo instance at 0xb72c060c&amp;gt;
&lt;/pre&gt;
&lt;p&gt;To pull it apart; we can see that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Foo.__dict__['function']&lt;/span&gt;&lt;/tt&gt; is a
function object, but then &lt;tt class="docutils literal"&gt;f.function&lt;/tt&gt; is a &lt;em&gt;bound method&lt;/em&gt;. The bound
method's &lt;tt class="docutils literal"&gt;im_func&lt;/tt&gt; is the underlying function object, and the
&lt;tt class="docutils literal"&gt;im_self&lt;/tt&gt; is the object &lt;tt class="docutils literal"&gt;f&lt;/tt&gt;: thus &lt;tt class="docutils literal"&gt;im_func(im_self)&lt;/tt&gt; is calling
&lt;tt class="docutils literal"&gt;function&lt;/tt&gt; with the correct object as the first argument &lt;tt class="docutils literal"&gt;self&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;So the main point is to kind of shift thinking about a function as some
particular intrinsic part of a class, but rather as a separate object
abstracted from the class that gets bound into an instance as required.
The class is in some ways a template and namespacing tool to allow you
to find the right function objects; it doesn't actually implement the
functions as such.&lt;/p&gt;
&lt;p&gt;There is plenty more information if you search for &amp;quot;descriptor protocol&amp;quot;
and Python binding rules and lots of advanced tricks you can play. But
hopefully this is a useful introduction to get an initial handle on
what's going on!&lt;/p&gt;
</summary></entry><entry><title>Python and --prefix</title><link href="http://www.technovelty.org/python/python-and-prefix.html" rel="alternate" /><updated>2012-02-08T16:16:00-08:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2012-02-08:python/python-and-prefix.html</id><summary type="html">&lt;p&gt;Something interesting I discovered about Python and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--prefix&lt;/span&gt;&lt;/tt&gt; that I
can't see a lot of documentation on...&lt;/p&gt;
&lt;p&gt;When you build Python you can use the standard &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--prefix&lt;/span&gt;&lt;/tt&gt; flag to
&lt;tt class="docutils literal"&gt;configure&lt;/tt&gt; to home the installation as you require. You might expect
that this would hard-code the location to look for the support libraries
to the value you gave; however in reality it doesn't quite work like
that.&lt;/p&gt;
&lt;p&gt;Python will only look in the directory specified by &lt;tt class="docutils literal"&gt;prefix&lt;/tt&gt; &lt;em&gt;after&lt;/em&gt;
it first searches relative to the path of the executing binary.
Specifically, it looks at &lt;tt class="docutils literal"&gt;argv[0]&lt;/tt&gt; and works through a few steps — is
&lt;tt class="docutils literal"&gt;argv[0]&lt;/tt&gt; a symlink? then dereference it. Does &lt;tt class="docutils literal"&gt;argv[0]&lt;/tt&gt; have any
slashes in it? if not, then search the &lt;tt class="docutils literal"&gt;$PATH&lt;/tt&gt; for the binary. After
this, it starts searching for &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dirname(argv[0])/lib/pythonX.Y/os.py&lt;/span&gt;&lt;/tt&gt;,
then &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dirname(argv[0])/../lib&lt;/span&gt;&lt;/tt&gt; and so on, until it reaches the root.
Only after these searches fail does the interpreter then fall back to
the hard-coded path specified in the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--prefix&lt;/span&gt;&lt;/tt&gt; when configured.&lt;/p&gt;
&lt;p&gt;What is the practical implications? It means you can move around a
python installation tree and have it all &amp;quot;just work&amp;quot;, which is nice. In
my situation, I noticed this because we have a completely
self-encapsulated build toolchain, but we wish to ship the same
interpreter on the thing that we're building (during the build, we run
the interpreter to create &lt;tt class="docutils literal"&gt;.pyc&lt;/tt&gt; files for distribution, and we need
to be sure that when we did this we didn't accidentally pick up any of
the build hosts python; only the toolchain python).&lt;/p&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;PYTHONHOME&lt;/tt&gt; environment variable does override this behaviour; if
it is set then the search stops there. Another interesting thing is that
&lt;tt class="docutils literal"&gt;sys.prefix&lt;/tt&gt; is therefore &lt;strong&gt;not&lt;/strong&gt; the value passed in by &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--prefix&lt;/span&gt;&lt;/tt&gt;
during configure, but the value of the current dynamically determined
prefix value.&lt;/p&gt;
&lt;p&gt;If you run an &lt;tt class="docutils literal"&gt;strace&lt;/tt&gt;, you can see this in operation.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
readlink(&amp;quot;/usr/bin/python&amp;quot;, &amp;quot;python2.7&amp;quot;, 4096) = 9
readlink(&amp;quot;/usr/bin/python2.7&amp;quot;, 0xbf8b014c, 4096) = -1 EINVAL (Invalid argument)
stat64(&amp;quot;/usr/bin/Modules/Setup&amp;quot;, 0xbf8af0a0) = -1 ENOENT (No such file or directory)
stat64(&amp;quot;/usr/bin/lib/python2.7/os.py&amp;quot;, 0xbf8af090) = -1 ENOENT (No such file or directory)
stat64(&amp;quot;/usr/bin/lib/python2.7/os.pyc&amp;quot;, 0xbf8af090) = -1 ENOENT (No such file or directory)
stat64(&amp;quot;/usr/lib/python2.7/os.py&amp;quot;, {st_mode=S_IFREG|0644, st_size=26300, ...}) = 0
stat64(&amp;quot;/usr/bin/Modules/Setup&amp;quot;, 0xbf8af0a0) = -1 ENOENT (No such file or directory)
stat64(&amp;quot;/usr/bin/lib/python2.7/lib-dynload&amp;quot;, 0xbf8af0a0) = -1 ENOENT (No such file or directory)
stat64(&amp;quot;/usr/lib/python2.7/lib-dynload&amp;quot;, {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
&lt;/pre&gt;
&lt;p&gt;Firstly it dereferences symlinks. Then it looks for &lt;tt class="docutils literal"&gt;Modules/Setup&lt;/tt&gt; to
see if it is running out of the build tree. Then it starts looking for
&lt;tt class="docutils literal"&gt;os.py&lt;/tt&gt;, walking its way upwards. One interesting thing that may
either be a bug or a feature, I haven't decided, is that if you set the
prefix to &lt;tt class="docutils literal"&gt;/&lt;/tt&gt; then the interpreter will not go back to the root and
then look in &lt;tt class="docutils literal"&gt;/lib&lt;/tt&gt;. This is probably pretty obscure usage though!&lt;/p&gt;
&lt;p&gt;All this is implemented in
&lt;a class="reference external" href="http://svn.python.org/projects/python/trunk/Modules/getpath.c"&gt;Modules/getpath.c&lt;/a&gt;
which has a nice big comment at the top explaining the rules in detail.&lt;/p&gt;
</summary></entry><entry><title>pylint and hiding of attributes</title><link href="http://www.technovelty.org/python/pylint-and-hiding-of-attributes.html" rel="alternate" /><updated>2012-01-26T22:09:00-08:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2012-01-26:python/pylint-and-hiding-of-attributes.html</id><summary type="html">&lt;p&gt;I recently came across the &lt;tt class="docutils literal"&gt;pylint&lt;/tt&gt; error:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
E:  3,4:Foo.foo: An attribute affected in foo line 12 hide this method
&lt;/pre&gt;
&lt;p&gt;in code that boiled down to essentially:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
class Foo:

    def foo(self):
        return True

    def foo_override(self):
        return False

    def __init__(self, override=False):
        if override:
            self.foo = self.foo_override
&lt;/pre&gt;
&lt;p&gt;Unfortunately that message isn't particularly helpful in figuring out
what's going on. I still can't claim to be 100% sure what the message is
intended to convey, but I can construct something that maybe it's
talking about.&lt;/p&gt;
&lt;p&gt;Consider the following using the above class&lt;/p&gt;
&lt;pre class="literal-block"&gt;
foo = Foo()
moo = Foo(override=True)

print &amp;quot;expect True  : %s&amp;quot; % foo.foo()
print &amp;quot;expect False : %s&amp;quot; % moo.foo()
print &amp;quot;expect True  : %s&amp;quot; % Foo.foo(foo)
print &amp;quot;expect False : %s&amp;quot; % Foo.foo(moo)
&lt;/pre&gt;
&lt;p&gt;which gives output of:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ python ./foo.py
expect True  : True
expect False : False
expect True  : True
expect False : True
&lt;/pre&gt;
&lt;p&gt;Now, if you read just about any Python tutorial, it will say something
along the lines of:&lt;/p&gt;
&lt;blockquote&gt;
... the special thing about methods is that the object is passed as
the first argument of the function. In our example, the call x.f()
is exactly equivalent to MyClass.f(x). In general, calling a method
with a list of n arguments is equivalent to calling the
corresponding function with an argument list that is created by
inserting the method’s object before the first argument. [&lt;a class="reference external" href="http://docs.python.org/tutorial/classes.html#instance-objects"&gt;Official
Python
Tutorial&lt;/a&gt;]&lt;/blockquote&gt;
&lt;p&gt;The official tutorial above is careful to say &lt;em&gt;in general&lt;/em&gt;; others often
don't.&lt;/p&gt;
&lt;p&gt;The important point to remember is how python internally resolves
attribute references as described by the &lt;a class="reference external" href="http://docs.python.org/reference/datamodel.html"&gt;data
model&lt;/a&gt;. The
&lt;tt class="docutils literal"&gt;moo.foo()&lt;/tt&gt; call is really &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;moo.__dict__[&amp;quot;foo&amp;quot;](moo)&lt;/span&gt;&lt;/tt&gt;; examining the
&lt;tt class="docutils literal"&gt;__dict__&lt;/tt&gt; for the &lt;tt class="docutils literal"&gt;moo&lt;/tt&gt; object we can see that &lt;tt class="docutils literal"&gt;foo&lt;/tt&gt; has been
re-assigned:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
&amp;gt;&amp;gt;&amp;gt; print moo.__dict__
{'foo': &amp;lt;bound method Foo.foo_override of &amp;lt;__main__.Foo instance at 0xb72838ac&amp;gt;&amp;gt;}
&lt;/pre&gt;
&lt;p&gt;Our &lt;tt class="docutils literal"&gt;Foo.foo(moo)&lt;/tt&gt; call is really &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Foo.__dict__[&amp;quot;foo&amp;quot;](moo)&lt;/span&gt;&lt;/tt&gt; -- the
fact that we reassigned &lt;tt class="docutils literal"&gt;foo&lt;/tt&gt; in &lt;tt class="docutils literal"&gt;moo&lt;/tt&gt; is never noticed. If we were
to do something like &lt;tt class="docutils literal"&gt;Foo.foo = Foo.foo_override&lt;/tt&gt; we would modify the
class &lt;tt class="docutils literal"&gt;__dict__&lt;/tt&gt;, but that doesn't give us the original semantics.&lt;/p&gt;
&lt;p&gt;So I postulate that the main point of this warning is to suggest to you
that you're creating an &lt;em&gt;instance&lt;/em&gt; that now behaves differently to its
&lt;em&gt;class&lt;/em&gt;. Because the symmetry of calling an instance and calling a class
is well understood you might end up getting some strange behaviour,
especially if you start with heavy-duty introspection of classes.&lt;/p&gt;
&lt;p&gt;Thinking about various hacks and ways to re-write this construct is kind
of interesting. I think I might have found a hook for a decent interview
question :)&lt;/p&gt;
</summary></entry><entry><title>Exploring $ORIGIN</title><link href="http://www.technovelty.org/linux/exploring-origin.html" rel="alternate" /><updated>2011-09-21T22:16:00-07:00</updated><author><name>Ian Wienand</name></author><id>tag:www.technovelty.org,2011-09-21:linux/exploring-origin.html</id><summary type="html">&lt;p&gt;Anyone who works with building something with a few libraries will
quickly become familiar with &lt;tt class="docutils literal"&gt;rpath&lt;/tt&gt;'s; that is the ability to store a
path inside a binary for finding dependencies at runtime. Slightly less
well known is that the dynamic linker provides some options for this
path specification; one in particular is a path with the special
variable &lt;tt class="docutils literal"&gt;$ORIGIN&lt;/tt&gt;. &lt;tt class="docutils literal"&gt;ld.so&lt;/tt&gt; man page has the following to say about
the an &lt;tt class="docutils literal"&gt;$ORIGIN&lt;/tt&gt; appearing in the &lt;tt class="docutils literal"&gt;rpath&lt;/tt&gt;:&lt;/p&gt;
&lt;blockquote&gt;
ld.so understands the string $ORIGIN (or equivalently ${ORIGIN})
in  an  rpath specification to mean the directory containing the
application  executable.  Thus,  an   application   located   in
somedir/app   could   be  compiled  with  gcc  -Wl,-rpath,'$ORI‐
GIN/../lib' so that it finds an  associated  shared  library  in
somedir/lib  no matter where somedir is located in the directory
hierarchy.&lt;/blockquote&gt;
&lt;p&gt;As a way of a small example, consider the following:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ cat Makefile
main: main.c lib/libfoo.so
    gcc -g -Wall -o main -Wl,-rpath='$$ORIGIN/lib' -L ./lib -lfoo main.c

lib/libfoo.so: lib/foo.c
    gcc -g -Wall -fPIC -shared -o lib/libfoo.so lib/foo.c

$ cat main.c
void function(void);

int main(void)
{
   function();
   return 0;
}

$ cat lib/foo.c
#include &amp;lt;stdio.h&amp;gt;

void function(void)
{
   printf(&amp;quot;called!\n&amp;quot;);
}

$ make
gcc -g -Wall -fPIC -shared -o lib/libfoo.so lib/foo.c
gcc -g -Wall -o main -Wl,-rpath='$ORIGIN/lib' -L ./lib -lfoo main.c

$ ./main
called!
&lt;/pre&gt;
&lt;p&gt;Now, wherever you should choose to locate &lt;tt class="docutils literal"&gt;main&lt;/tt&gt;, as long as the
library is in the right relative location it will be found correctly. If
you are wondering &lt;em&gt;how&lt;/em&gt; this works, examining the back-trace from the
linker function that expands it is helpful:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ gdb ./main
(gdb) break _dl_get_origin
Function &amp;quot;_dl_get_origin&amp;quot; not defined.
Make breakpoint pending on future shared library load? (y or [n]) y

Breakpoint 1 (_dl_get_origin) pending.
(gdb) r
Starting program: /tmp/test-origin/main

Breakpoint 1, _dl_get_origin () at ../sysdeps/unix/sysv/linux/dl-origin.c:37
37  ../sysdeps/unix/sysv/linux/dl-origin.c: No such file or directory.
    in ../sysdeps/unix/sysv/linux/dl-origin.c
(gdb) bt
#0  _dl_get_origin () at ../sysdeps/unix/sysv/linux/dl-origin.c:37
#1  0x00007ffff7de61f4 in expand_dynamic_string_token (l=0x7ffff7ffe128,
    s=&amp;lt;value optimized out&amp;gt;) at dl-load.c:331
#2  0x00007ffff7de62ea in decompose_rpath (sps=0x7ffff7ffe440,
    rpath=0xffffffc0 &amp;lt;Address 0xffffffc0 out of bounds&amp;gt;, l=0x0,
    what=0x7ffff7df780a &amp;quot;RPATH&amp;quot;) at dl-load.c:554
#3  0x00007ffff7de7051 in _dl_init_paths (llp=0x0) at dl-load.c:722
#4  0x00007ffff7de2124 in dl_main (phdr=0x7ffff7ffe6b8,
    phnum=&amp;lt;value optimized out&amp;gt;, user_entry=0x7ffff7ffeb28) at rtld.c:1391
#5  0x00007ffff7df3647 in _dl_sysdep_start (
    start_argptr=&amp;lt;value optimized out&amp;gt;, dl_main=0x7ffff7de14e0 &amp;lt;dl_main&amp;gt;)
    at ../elf/dl-sysdep.c:243
#6  0x00007ffff7de0423 in _dl_start_final (arg=0x7fffffffe7c0) at rtld.c:338
#7  _dl_start (arg=0x7fffffffe7c0) at rtld.c:564
#8  0x00007ffff7ddfaf8 in _start () from /lib64/ld-linux-x86-64.so.2
&lt;/pre&gt;
&lt;p&gt;Just from a first look we can divine that this has found the &lt;tt class="docutils literal"&gt;RPATH&lt;/tt&gt;
header in the dynamic section and has decided to expand the string
&lt;tt class="docutils literal"&gt;$ORIGIN&lt;/tt&gt;.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ readelf --dynamic ./main

Dynamic section at offset 0x7d8 contains 23 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libfoo.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [$ORIGIN/lib]
 0x000000000000000c (INIT)               0x4004d8
 0x000000000000000d (FINI)               0x4006f8
&lt;/pre&gt;
&lt;p&gt;At this point it proceeds to do a &lt;tt class="docutils literal"&gt;readlink&lt;/tt&gt; on &lt;tt class="docutils literal"&gt;/proc/self/exe&lt;/tt&gt; to
get the path to the currently running binary, effectively does a
&lt;tt class="docutils literal"&gt;basename&lt;/tt&gt; on it and replaces the value of &lt;tt class="docutils literal"&gt;$ORIGIN&lt;/tt&gt;. Interestingly,
if this should fail, it will fall back on the environment variable
&lt;tt class="docutils literal"&gt;LD_ORIGIN_PATH&lt;/tt&gt; for unknown reasons. This might be useful if you were
in a bad situation where &lt;tt class="docutils literal"&gt;/proc&lt;/tt&gt; was not mounted and you still had to
run your binary, although you could probably achieve the same thing via
the use of &lt;tt class="docutils literal"&gt;LD_LIBRARY_PATH&lt;/tt&gt; just as well.&lt;/p&gt;
&lt;p&gt;This feature should be used with some caution to avoid turning your
application in a mess that can't be packaged in any standard manner. One
common example of use is probably your java virtual machine to find its
implementation libraries.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ readelf --dynamic /usr/lib/jvm/java-6-sun/bin/java

Dynamic section at offset 0x9a28 contains 25 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libjli.so]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000e (SONAME)             Library soname: [lib.so]
 0x000000000000000f (RPATH)              Library rpath: [$ORIGIN/../lib/amd64/jli:$ORIGIN/../jre/lib/amd64/jli]
 ...
&lt;/pre&gt;
&lt;p&gt;There is a small but interesting security issue. If an attacker can
hardlink to your binary that is using &lt;tt class="docutils literal"&gt;$ORIGIN&lt;/tt&gt; then any path
expansion is now relative to the attackers directory. Hence it is
possible to make the linker read in arbitrary libraries and hence run
arbitrary code. Clearly if your program is running &lt;tt class="docutils literal"&gt;setuid&lt;/tt&gt; as someone
else, such as &lt;tt class="docutils literal"&gt;root&lt;/tt&gt;, you've just given up the keys to the house!&lt;/p&gt;
&lt;p&gt;Luckily, the dynamic linker will not let you shoot yourself in the foot
like this, and prevents expansion of the &lt;tt class="docutils literal"&gt;$ORIGIN&lt;/tt&gt; field if the
program is running setuid.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ sudo chown root ./main
$ sudo chmod +s ./main
$ ./main
./main: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory
&lt;/pre&gt;
&lt;p&gt;However, this is a good example of why you might want to keep users home
and temp directories on separate file systems away from where binaries
live.&lt;/p&gt;
&lt;p&gt;There is also a similar variable &lt;tt class="docutils literal"&gt;$PLATFORM&lt;/tt&gt;, which describes the
current running system. This is gained via the &lt;em&gt;auxiliary vector&lt;/em&gt;, which
I have written about
&lt;a class="reference external" href="http://www.technovelty.org/linux/linux-gate.html"&gt;here&lt;/a&gt; and
&lt;a class="reference external" href="http://www.technovelty.org/linux/proc-auxv.html"&gt;here&lt;/a&gt;. Setting
&lt;tt class="docutils literal"&gt;LD_SHOW_AUXV=1&lt;/tt&gt; will allow you to examine this value.&lt;/p&gt;
&lt;pre class="literal-block"&gt;
$ LD_SHOW_AUXV=1 ls
...
AT_PLATFORM:     x86_64
...
&lt;/pre&gt;
</summary></entry></feed>
