<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0"><channel>
	<title>Callum's Code Blog</title>
	<link>http://callumscode.com/blog</link>
	<description></description>
		<item>
	<title>Bug Blog: execing a shell </title>
	<link>http://callumscode.com/blog/4</link>
	<pubDate>Thu, 01 Apr 2010 22:30:50 +0100</pubDate>
	<description>&lt;p&gt;I just spent an hour tracking down a bug involving the exec syscall.  I was trying to implement a &quot;shell&quot; function for my programming language project.  Something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;exitcode = shell(&quot;ls -l&quot;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On UNIX-like systems, this involves forking off a child process that will exec the command.  The parent process then waits for the child to finish and returns the exit status.  Simple.&lt;/p&gt;

&lt;p&gt;However, unlike CreateProcess under Windows, the UNIX style exec syscall requires the caller to parse the command line arguments for the new image.  Since I want the user of my shell function to be able to pass anything they could type on the command line, parsing the command line arguments is not a trivial matter.&lt;/p&gt;

&lt;h3&gt;The bug&lt;/h3&gt;

&lt;p&gt;Not to worry though, I can use the sh command to do the parsing for me.  I simply transform the argument to the shell function into &quot;/bin/sh -c '&amp;lt;command&amp;gt;'&quot;.  Now the problem is reduced to execing /bin/sh with 2 arguments.&lt;/p&gt;

&lt;p&gt;But it didn't work.  I got mysterious errors of the form &quot;-c: can't open &amp;lt;command&amp;gt;&quot;.&lt;/p&gt;

&lt;h3&gt;Why?&lt;/h3&gt;

&lt;p&gt;The execve syscall (the low level exec syscall on Linux) takes as one of it's parameters an array of arguments for the new image.  I was passing the array [&quot;-c&quot;, &quot;&amp;lt;command&amp;gt;&quot;].  Wrong!&lt;/p&gt;

&lt;p&gt;The command line arguments array on UNIX always starts with the name under which the binary was invoked, as any C programmer will tell you.  So the shell was being invoked under the name &quot;-c&quot; with the single argument &quot;&amp;lt;command&amp;gt;&quot; which it tried to interpret as a shell script.  Hence the error &quot;-c: can't open &amp;lt;command&amp;gt;&quot;.  The &quot;-c&quot; shell couldn't run the &quot;shell script&quot; called &quot;&amp;lt;command&amp;gt;&quot;.&lt;/p&gt;
	</description>
	</item>
		<item>
	<title>x86-64 assembly on Linux - syscalls</title>
	<link>http://callumscode.com/blog/3</link>
	<pubDate>Thu, 21 Jan 2010 03:56:49 +0000</pubDate>
	<description>&lt;p&gt;There are plenty of tutorials showing how write Linux programs in x86 assembler, a good resource is &lt;a href=&quot;http://asm.sourceforge.net&quot;&gt;http://asm.sourceforge.net&lt;/a&gt;.  However, there is very little on 64bit x86 assembler (x86-64).  x86-64 is very similar to its 32bit counterpart.  The main difference is that the registers are wider and there are more of them.  You might think this would make it easy to adapt 32bit code to run in 64ibt mode under Linux.&lt;/p&gt;

&lt;p&gt;Of course, there are a few gotchas.  Linux has a completely different system call ABI under 64bit mode.  The syscalls have different numbers and are called in a completely different way.  Depending on how your kernel was compiled though, the 32bit interface may be present and usable as well (&lt;a href=&quot;http://lkml.indiana.edu/hypermail/linux/kernel/0412.0/0550.html&quot;&gt;lkml&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In ignorance of this situation, I wrote a whole lot of code making simple 32bit syscalls from 64bit mode.  It worked just fine on my Ubuntu system.  Then I tried it under 64bit &lt;a href=&quot;http://minimalinux.org/ttylinux/&quot;&gt;ttyLinux&lt;/a&gt;, a minimalist Linux distribution.  My programs crashed as soon as they hit the first syscall.&lt;/p&gt;

&lt;p&gt;If you want your assembler programs to work reliably across different kernels and distributions, you ought to use the correct ABI.&lt;/p&gt;

&lt;p&gt;So, how does it work?  It's based on the x86-64 UNIX C ABI which goes like this: arguments 1-6 are passed in the registers RDI, RSI, RDX, RCX, R8, R9.  The result comes back in the RAX register, if 64bits isn't enough the high bits are in RDX.  The parameter registers plus R10 and R11 are clobbered, the rest are saved.  C's int type is 32bits, C's long type is 64bits.  It's all described in this &lt;a href=&quot;http://www.x86-64.org/documentation/abi.pdf&quot;&gt;document&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For the 64bit syscalls, parameter 4 is passed in R10 instead of RCX.  RCX is still clobbered.  The syscall number is passed in RAX, as in 32bit mode, but instead of the &quot;int 0x80&quot; used in 32bit mode, 64bit syscalls are made with the &quot;syscall&quot; instruction.  The syscall numbers can be found in the Linux source code under &lt;a href=&quot;http://lxr.linux.no/#linux+v2.6.32/arch/x86/include/asm/unistd_64.h&quot;&gt;arch/x86/include/asm/unistd_64.h&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'll finish this off with an example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[bits 64]

section .text
global _start
_start:                ; ELF entry point
mov rax, 1             ; sys_write
mov rdi, 1             ; STDOUT
mov rsi, message       ; buffer
mov rdx, [messageLen]  ; length of buffer
syscall

mov rax, 60            ; sys_exit
mov rdi, 0             ; 0
syscall

section .data
messageLen: dq message.end-message
message: db 'Hello World', 10
  .end:
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;compile with &quot;nasm -felf64 &lt;em&gt;filename&lt;/em&gt;&quot; followed by &quot;ld &lt;em&gt;filename&lt;/em&gt;.o&quot;.  No libraries needed, this code stands alone.&lt;/p&gt;

&lt;p&gt;Happy Hacking&lt;/p&gt;
	</description>
	</item>
		<item>
	<title>GTKSourceView language definition for NASM</title>
	<link>http://callumscode.com/blog/2</link>
	<pubDate>Tue, 25 Nov 2008 03:43:41 +0000</pubDate>
	<description>&lt;p&gt;&lt;em&gt;Update 22/7/2010: high register/hex number confusion cured&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Although I generally like gedit (GNOME Editor), I was disappointed to discover that it doesn't have syntax highlighting for x86 assembler.  It turns out that adding a new syntax highlighter is as simple as writing a little xml file and putting it in GTKSourceView's directory.  On my Ubuntu system this is /usr/share/gtksourceview-2.0/language-specs/.&lt;/p&gt;

&lt;p&gt;Here's the &lt;a href=&quot;http://callumscode.com/downloads/nasm.lang&quot;&gt;file&lt;/a&gt; in case you're interested.&lt;/p&gt;
	</description>
	</item>
		<item>
	<title>Centering a div in the window</title>
	<link>http://callumscode.com/blog/1</link>
	<pubDate>Thu, 10 Jul 2008 04:55:21 +0100</pubDate>
	<description>&lt;p&gt;Ever wanted to center a panel in the middle of the browser window?  There's a lot of code out there that claims to do this, but it isn't standards compliant and doesn't work on all browsers.  But never fear, there is an easy way to do it without any browser specific hacks.&lt;/p&gt;

&lt;p&gt;First, you need a couple of styles:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;body {
  background-color: #000000;
}

.reference {
  background-color: transparent;
  position: absolute;
  top: 50%;left: 50%;
  width: 1px;height: 1px;
}

#panel {
  position:absolute;
  left:-485px;top:-256px;
  width:970px;height:512px;
  background-color:#FFFFFF;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now the html:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;div class=&quot;reference&quot;&amp;gt;
  &amp;lt;div id=&quot;panel&quot;&amp;gt;
    Your content here
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The reference style creates a 1px by 1px div centered in the middle of the window.  Inside this div is the panel.  Normally this would place the upper left hand corner of the panel in the center of the window.  To correct for this, the left and top properties are set to -(width/2) and -(height/2) respectively.  Since the width and height of the panel is known these can be hard coded into the styles.&lt;/p&gt;

&lt;p&gt;Happy Hacking&lt;/p&gt;
	</description>
	</item>
	</channel></rss>
