<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>markusbe.com &#187; background</title>
	<atom:link href="http://www.markusbe.com/category/background/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markusbe.com</link>
	<description>clear, concise, complete, correct</description>
	<lastBuildDate>Sun, 14 Nov 2010 21:47:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>How to read a patch or diff and understand its structure to apply it manually</title>
		<link>http://www.markusbe.com/2009/12/how-to-read-a-patch-or-diff-and-understand-its-structure-to-apply-it-manually/</link>
		<comments>http://www.markusbe.com/2009/12/how-to-read-a-patch-or-diff-and-understand-its-structure-to-apply-it-manually/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 16:24:56 +0000</pubDate>
		<dc:creator>Markus Bertheau</dc:creator>
				<category><![CDATA[background]]></category>

		<guid isPermaLink="false">http://www.markusbe.com/?p=303</guid>
		<description><![CDATA[Hi I wrote this article to show you how patch/diff files store difference information. After reading it you will be able to understand patch files when you read them, better understand certain patch error messages and apply changes manually. Patch or diff files are just text files, so you can look at them with less [...]]]></description>
			<content:encoded><![CDATA[<p>Hi <img src='http://www.markusbe.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I wrote this article to show you how patch/diff files store difference information. After reading it you will be able to understand patch files when you read them, better understand certain patch error messages and apply changes manually.</p>
<p>Patch or diff files are just text files, so you can look at them with <tt>less</tt> or a text editor.</p>
<p>If you prefer to use a terminal, <a href="http://colordiff.sourceforge.net/"><tt>colordiff</tt></a> (in the <tt>colordiff</tt> package) provides syntax highlighting. If the patch is long, you may want to use <tt>less</tt> to look at it. You need the <tt>-R</tt> option for <tt>less</tt>, or else the colors won&#8217;t show. I also always use <tt>-S</tt>, which will enable horizontal scrolling in <tt>less</tt> instead of wrapping long lines. The whole command to view a patch with syntax highlighting through <tt>less</tt> in a terminal is:</p>
<pre>
<span class="prompt">markus@ubuntu:~$</span> <strong>cat file.patch | colordiff | less -RS</strong>
</pre>
<h6>Patch formats</h6>
<p><span id="more-303"></span><br />
Each patch file comes in one of 3 formats, called normal, context and unified diff. You can identify them by looking at the contents of the patch file.</p>
<p><strong>Normal diffs</strong> have a lot of lines that start with <tt>&gt;</tt> or <tt>&lt;</tt>:</p>
<pre>
...
51c51
< background-color: red;
---
> background-color: blue;
...
</pre>
<p><a href="#how-to-read-a-normal-diff">Skip to how to read a normal diff</a></p>
<p><strong>Context diffs</strong> have a lot of lines with stars ***** in them and lines starting with <tt>!</tt>, <tt>+</tt> and <tt>-</tt>:</p>
<pre>
...
***************
*** 44,54 ****

  html, body { color: #242626; }
  html {
!   background-color: red;
    height: 100%;
    margin-bottom: 1px;
    overflow-y: scroll;
--- 44,54 ----

  html, body { color: #242626; }
  html {
!   background-color: blue;
    height: 100%;
    margin-bottom: 1px;
    overflow-y: scroll;
...
</pre>
<p><a href="#how-to-read-a-context-diff">Skip to how to read a context diff</a></p>
<p>And finally <strong>unified diffs</strong> have a lot of lines with two <tt>@@</tt>s in them and lines starting with <tt>+</tt> and <tt>-</tt>:</p>
<pre>
...
@@ -44,11 +44,11 @@

 html, body { color: #242626; }
 html {
-  background-color: red;
+  background-color: blue;
   height: 100%;
   margin-bottom: 1px;
   overflow-y: scroll;
...
</pre>
<p><a href="#how-to-read-a-unified-diff">Skip to how to read a unified diff</a></p>
<h6 id="how-to-read-a-normal-diff">How to read a normal diff</h6>
<p>The patch file contains a section for each file that should be changed. That section starts with a line identifying the file. That line has the file name and path of first the old and then the new version in it:</p>
<pre>
diff violet-park-2.orig/style.css violet-park-2/style.css
</pre>
<p>Then follow sets of changes, called hunks. The first line of each hunk contains</p>
<ul>
<li>the line or line range <tt>from,to</tt> in the file before the changes</li>
<li>a letter indicating a change (c), an addition (a) or a deletion (d)</li>
<li>the line or line range in the file after the changes</li>
</ul>
<p>all without spaces inbetween. </p>
<p>A <strong>change</strong> looks like this:</p>
<pre>
73c73
&lt;   red
---
&gt;   blue
</pre>
<p>That means, change line 73, which contains <tt>red</tt>, to <tt>blue</tt>. The new line is line 73 in the file after all changes. </p>
<p>The new line number could also have been, say, 75. In that case all additions, deletions and changes before that line added up to two more lines in the file after all changes.</p>
<p>Instead of single line numbers, a line range can be specified, for example:</p>
<pre>
45,65c90,94
...
</pre>
<p>That means, change lines 45 to 65 in the original file to the following 5 lines, which will be lines 90 to 94 in the new file. The same applies for additions and deletions.</p>
<p>An <strong>addition</strong> looks like this:</p>
<pre>
78a80,81
&gt; line1
&gt; line2
</pre>
<p>That means, in the original file after line 78 add two lines. These will be lines 80 through 81 after all changes.</p>
<p>And finally, a <strong>deletion</strong> looks like this:</p>
<pre>
78d77
&lt; line1
</pre>
<p>That means, delete line 78 in the original file. The line, that preceded line 78 in the original file will be line 77 in the patched file.</p>
<h6 id="how-to-read-a-context-diff">How to read a context diff</h6>
<p>The patch file contains a section for each file that should be changed. That section starts with three lines that contain the file name and path of first the old and then the new version and some additional information:</p>
<pre>
diff -c violet-park-2.orig/archive.php violet-park-2/archive.php
*** violet-park-2.orig/archive.php      2009-10-10 07:37:43.000000000 +0200
--- violet-park-2/archive.php   2009-10-10 09:05:58.000000000 +0200
</pre>
<p><tt>patch</tt> is pretty lax about the format of these lines, as long as it can find out the file names. In patches generated by version control systems like cvs, svn or git these three lines look a little different.</p>
<p>Then follow sets of changes, called hunks. Each hunk starts with a line with 15 asterisks. Then comes a line with the line range <tt>from,to</tt> and the lines from file before the changes. Lines that start with an <tt>!</tt> are changed, lines that start with a <tt>-</tt> are deleted. Then comes the line range and the lines in the file after all changes. Lines that start with a <tt>!</tt> are, again, changed, and lines that start with a <tt>+</tt> are added. Each line modified by the patch is surrounded with 3 lines of context before and after.</p>
<p>A <strong>change</strong> looks like this:</p>
<pre>
***************
*** 70,76 ****
  foo
  bar
  baz
! red
  more context
  and more
  still context
--- 70,76 ----
  foo
  bar
  baz
! blue
  more context
  and more
  still context
</pre>
<p>That means, change line 73 (= 70 + 3 lines of context) in the file before all changes, which contains <tt>red</tt> to <tt>blue</tt>. The changed line is also line 73 (= 70 + 3 lines of context) in the file after all changes.</p>
<p>Here&#8217;s an example for an <strong>addition</strong>:</p>
<pre>
***************
*** 75,80 ****
--- 77,84 ----
  foo
  bar
  baz
+ line1
+ line2
  more context
  and more
  and still context
</pre>
<p>That means, in the original file after line 78 (= 75 + 3 lines of context) add two lines. These will be lines 80 (= 77 + 3 lines of context) through 81 after all changes.</p>
<p>And that&#8217;s how a <strong>deletion</strong> looks like:</p>
<pre>
***************
*** 75,81 ****
  foo
  bar
  baz
- line1
  more context
  and more
  and still context
--- 75,80 ----
</pre>
<p>That means, delete line 78 (= 75 + 3 lines of context) in the original file. The unchanged context will be on lines 75 to 80 after all changes.</p>
<h6 id="how-to-read-a-unified-diff">How to read a unified diff</h6>
<p>The patch file contains a section for each file that should be changed. That section starts with three lines that contain the file name and path of first the old and then the new version and some additional information:</p>
<pre>
diff -u violet-park-2.orig/archive.php violet-park-2/archive.php
--- violet-park-2.orig/archive.php      2009-10-10 07:37:43.000000000 +0200
+++ violet-park-2/archive.php   2009-10-10 09:05:58.000000000 +0200
</pre>
<p><tt>patch</tt> is pretty lax about the format of these lines, as long as it can find out the file names. In patches generated by version control systems like cvs, svn or git these three lines look a little different.</p>
<p>Then follow sets of changes, called hunks. Each hunk starts with a line that contains, enclosed in <tt>@@</tt>, the line or line range <tt>from,no-of-lines</tt> in the file before (with a -) and after (with a +) the changes. After that come the lines from the file. Lines starting with a <tt>-</tt> are deleted, lines starting with a <tt>+</tt> are added. Each line modified by the patch is surrounded with 3 lines of context before and after.</p>
<p>An <strong>addition</strong> looks like this:</p>
<pre>
@@ -75,6 +77,8 @@
 foo
 bar
 baz
+line1
+line2
 more context
 and more
 and still context
</pre>
<p>That means, in the original file after line 78 (= 75 + 3 lines of context) add two lines. These will be lines 80 (= 77 + 3 lines of context) through 81 after all changes.</p>
<p>A <strong>deletion</strong> looks like this:</p>
<pre>
@@ -75,7 +75,6 @@
 foo
 bar
 baz
-line1
 more context
 and more
 and still context
</pre>
<p>That means, delete line 78 (= 75 + 3 lines of context) in the original file. The unchanged context will be on lines 75 to 80 after all changes.</p>
<p>Finally, a <strong>change</strong> looks like this:</p>
<pre>
@@ -70,7 +70,7 @@
 foo
 bar
 baz
-red
+blue
 more context
 and more
 still context
</pre>
<p>That means, change line 73 (= 70 + 3 lines of context) in the file before all changes, which contains <tt>red</tt> to <tt>blue</tt>. The changed line is also line 73 (= 70 + 3 lines of context) in the file after all changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markusbe.com/2009/12/how-to-read-a-patch-or-diff-and-understand-its-structure-to-apply-it-manually/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>About running 32 bit programs on 64 bit Ubuntu and shared libraries</title>
		<link>http://www.markusbe.com/2009/09/about-running-32-bit-programs-on-64-bit-ubuntu-and-shared-libraries/</link>
		<comments>http://www.markusbe.com/2009/09/about-running-32-bit-programs-on-64-bit-ubuntu-and-shared-libraries/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 19:27:37 +0000</pubDate>
		<dc:creator>Markus Bertheau</dc:creator>
				<category><![CDATA[background]]></category>

		<guid isPermaLink="false">http://www.markusbe.com/?p=14</guid>
		<description><![CDATA[Deutsch Русский Quick answer To run 32-bit programs on 64-bit installations of Ubuntu, install the package ia32-libs: markus@ubuntu:~$ sudo apt-get install ia32-libs Long story I wrote this article to expand your knowledge and understanding of how Linux works. This knowledge should increase your problem solving skills and speed in the area of server administration and [...]]]></description>
			<content:encoded><![CDATA[<div class="language">
<a href="http://www.markusbe.de/2009/09/ueber-32-bit-programme-auf-64-bit-ubuntu-und-gemeinsam-genutzte-bibliotheken/"><img src="/wp-content/themes/violet-park-2/img/de.png" width="20" height="12" alt="de" title="Deutsch"/> Deutsch</a> <a href="http://www.markusbe.ru/2009/10/%d0%ba%d0%b0%d0%ba-%d0%b7%d0%b0%d0%bf%d1%83%d1%81%d1%82%d0%b8%d1%82%d1%8c-32-%d0%b1%d0%b8%d1%82%d0%bd%d1%8b%d0%b5-%d0%bf%d1%80%d0%be%d0%b3%d1%80%d0%b0%d0%bc%d0%bc%d1%8b-%d0%bd%d0%b0-64-%d0%b1%d0%b8/"><img src="/wp-content/themes/violet-park-2/img/ru.png" width="20" height="14" alt="ru" title="Русский"/> Русский</a>
</div>
<h5>Quick answer</h5>
<p>To run 32-bit programs on 64-bit installations of Ubuntu, install the package <tt>ia32-libs</tt>:</p>
<pre style="clear: both;"><span class="prompt">markus@ubuntu:~$</span> <strong>sudo apt-get install ia32-libs</strong>
</pre>
<h4>Long story</h4>
<p>I wrote this article to expand your knowledge and understanding of how Linux works. This knowledge should increase your problem solving skills and speed in the area of server administration and command line use. I show how I adapted an installation shell script of a commercial software package, Flash<sup>®</sup> Media Server, which was written for RedHat Linux, to work on 64-bit Ubuntu Linux. For every symptom that occurs in the process, I explain the problem behind it and how to fix it. The end result is available in <a href="http://www.markusbe.com/2009/09/installing-flash-media-server-on-ubuntu-linux/">Installing Flash<sup>®</sup> Media Server on Ubuntu Linux</a>.</p>
<p>This article is the first part in a series on the subject. In it I write about running 32 bit programs on 64 bit operating systems and the concept of shared libraries.<br />
<span id="more-14"></span></p>
<p>The exact versions of the software used in the course are <a href="http://www.ubuntu.com/getubuntu/releasenotes/804overview">Ubuntu 8.04 LTS 64-Bit Server</a> and <a href="http://download.macromedia.com/pub/flashmediaserver/updates/3_5_2/Linux/FlashMediaServer3.5.tar.gz">Flash<sup>®</sup> Media Server 3.5.2</a>, if you want to follow along.</p>
<h5>32 bits on 64 bits</h5>
<p>When you run the original installer you&#8217;ll get an error message about the file <tt>fmsini</tt> not being found:</p>
<pre><span class="prompt">markus@ubuntu:~$</span> <strong>tar xfz <a href="http://download.macromedia.com/pub/flashmediaserver/updates/3_5_2/Linux/FlashMediaServer3.5.tar.gz">FlashMediaServer3.5.tar.gz</a></strong>
<span class="prompt">markus@ubuntu:~$</span> <strong>sudo FMS_3_5_2_r654/installFMS</strong>
FMS_3_5_2_r654/installFMS: 172: FMS_3_5_2_r654/fmsini: not found</pre>
<p>This error message is quite misleading. The file <tt>fmsini</tt> exists and it&#8217;s a 32 bit binary executable:</p>
<pre><span class="prompt">markus@ubuntu:~$</span> <strong>file FMS_3_5_2_r654/fmsini </strong>
fmsini: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux
2.2.5, dynamically linked (uses shared libs), stripped</pre>
<p>Why the error message &#8220;<tt>not found</tt>&#8220;?</p>
<p>The message does not refer to the file <tt>fmsini</tt>. Instead it refers to a <em>helper program</em> that&#8217;s needed to run the 32 bit <em>dynamically linked</em> executable <tt>fmsini</tt>.</p>
<h6>Static and dynamic linkage</h6>
<p>There are two types of binary executables: statically linked and dynamically linked ones. First about the <em>statically linked</em> ones: When a program wants to call a library function, it refers to it by name. When building the program from source, all library functions used in the program are copied from the library into the program. The program then contains its own code as well as the code of the library functions it uses. Then in the calling places the name is changed to the address of the corresponding function in the program. This process is called <em>linking</em> because it links together the name of a function with the function itself, its implementation. It&#8217;s called <em>static</em>, because the link cannot be changed after the program has been built.</p>
<p>Dynamically linked programs work differently: The program also refers to library functions by name. When building the program, two lists are assembled and stored together with the program: a list of which library functions are used in which places, and a list of the libraries that contain the functions used by the program. That&#8217;s all for building the program.</p>
<p>Later, at <em>execution time</em>, a special helper program, the so-called dynamic linker, looks in specific places in the file system for each library on the library list and loads it into memory. Now the dynamic linker knows at what memory addresses the library functions are available. It uses the first list to write the correct address in all places that call library functions. Then the dynamically linked program can be run.</p>
<h6>Advantages and disadvantages</h6>
<p>The biggest disadvantages of statically linked executables stem from the library sharing potential that is not realized. First, programs are bigger and require more space on the hard disk and in memory. That additional space often contains the same library functions over and over again, because they are used in a lot of programs. Second, security or bug fix updates of libraries require the program to be rebuilt from source and redistributed, which can be very costly.</p>
<p>Dynamically linked executables solve these problems: Shared libraries exist in one copy on the hard disk and in one copy in memory. The programs itself are smaller because they don&#8217;t contain library code. Libraries can be updated independently of the programs that use them. The programs themselves don&#8217;t have to be altered, just restarted.</p>
<p>They solve these problems at a price, though: More infrastructure is needed to support shared libraries: the libraries itself in the right version, and a more sophisticated program loader. In some scenarios this price is too high, and statically linked executables are used. They only need themselves to run and don&#8217;t have any dependencies or library compatibility problems.</p>
<h6>The dynamic linker</h6>
<p>The dynamic linker helper program has a manual page: <a href="http://www.kernel.org/doc/man-pages/online/pages/man8/ld.so.8.html"><tt>ld.so(8)</tt></a>. <tt>ld.so</tt> was the name of the dynamic linker in Linux before 1995/96. Now, for 32 bit, the dynamic linker is in the file <tt>/lib/ld-linux.so.2</tt>; for 64 bit it&#8217;s in <tt>/lib64/ld-linux-x86-64.so.2</tt>. The manual page still has the old name, though.</p>
<p>64 bit Ubuntu Linux has the ability built in to execute 32 bit <em>statically linked</em> programs. The dynamic linker for 32 bit programs though is not installed by default. It&#8217;s in the package <tt>libc6-i386</tt>. After installing that package the file <tt>/lib/ld-linux.so.2</tt> exists, and the first step to executing 32 bit dynamically linked executables is done.</p>
<h6>Shared libraries</h6>
<p>After installing <tt>libc6-i386</tt>, you&#8217;ll get another error message when trying to execute <tt>fmsini</tt>:</p>
<pre><span class="prompt">markus@ubuntu:~$</span> <strong>FMS_3_5_2_r654/fmsini</strong>
FMS_3_5_2_r654/fmsini: error while loading shared libraries: libstdc++.so.6:
cannot open shared object file: No such file or directory
</pre>
<p>This is the dynamic linker, trying to load all the libraries in the list and not finding one of them.</p>
<p>Libraries used by dynamically linked executables are called shared libraries. When the dynamic linker finds that a certain library is already loaded, it can refer the new program to use that one instead of loading it again. That way, a library loaded one time is shared among several programs.</p>
<p>Shared libraries come in files that have a <tt>.so</tt> in their name. <tt>so</tt> stands for <em>shared object</em>. &#8220;Object&#8221; just means compiled, binary code.</p>
<p><a href="http://www.kernel.org/doc/man-pages/online/pages/man1/ldd.1.html"><tt>ldd(1)</tt></a> shows what shared libraries a program needs and where the dynamic linker found these libraries, if it did:</p>
<pre><span class="prompt">markus@ubuntu:~$</span> <strong>ldd FMS_3_5_2_r654/fmsini</strong>
	linux-gate.so.1 =>  (0xf7fc8000)
	libpthread.so.0 => /lib32/libpthread.so.0 (0xf7fa0000)
	libdl.so.2 => /lib32/libdl.so.2 (0xf7f9c000)
	libstdc++.so.6 => not found
	libm.so.6 => /lib32/libm.so.6 (0xf7f75000)
	libgcc_s.so.1 => not found
	libc.so.6 => /lib32/libc.so.6 (0xf7e12000)
	/lib/ld-linux.so.2 (0xf7fc9000)
</pre>
<p>The dynamic linker looks for the libraries in a number of places &#8211; the <a href="http://www.kernel.org/doc/man-pages/online/pages/man8/ld.so.8.html">manual page</a> has all the details on that. The two missing libraries are in the packages <tt>lib32stdc++6</tt> and <tt>lib32gcc1</tt> respectively.</p>
<p><tt>ldd</tt>, by the way, is just a shell script that calls the dynamic linker. Unless told otherwise, the dynamic linker loads all libraries and executes the program. <tt>ldd</tt> calls it with arguments that tell it to print information about the shared libraries needed by the program.</p>
<p>The majority of programs today are dynamically linked and use shared libraries. The Ubuntu installation comes with over 400 64 bit shared libraries. There&#8217;s a package <tt>ia32-libs</tt> in Ubuntu&#8217;s universe repository that contains the most commonly used shared libraries in 32 bit versions. Installing that package will allow many 32 bit programs to run on 64 bit Ubuntu. In particular, it allows the installer of Flash<sup>®</sup> Media Server to run (but for other reasons it doesn&#8217;t <em>function well</em> yet). The Flash<sup>®</sup> Media Server itself though needs libraries that aren&#8217;t in that package. How to solve that will be the topic of a later part in that series.</p>
<h5>Feedback</h5>
<p>Feedback is extremely important to me because it gives me clues about what I need to do differently and what to retain in order to reach my goal. My goal is to have this article be interesting and of use to you, as described in the first paragraph. Please don&#8217;t hesitate to leave a comment on which passages are written in a way that hinders comprehension, what should have been left out, or what is missing. I&#8217;d also like to hear from you if there&#8217;s something you liked especially about this article. Thank you. <img src='http://www.markusbe.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.markusbe.com/2009/09/about-running-32-bit-programs-on-64-bit-ubuntu-and-shared-libraries/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

