<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://herzamos.ch/feed.xml" rel="self" type="application/atom+xml" /><link href="https://herzamos.ch/" rel="alternate" type="text/html" /><updated>2022-01-14T11:04:17+00:00</updated><id>https://herzamos.ch/feed.xml</id><title type="html">Amos’ Blog</title><author><name>Amos Herz</name><email>herzamos@gmail.com</email></author><entry><title type="html">C linker symbols</title><link href="https://herzamos.ch/c/system-programming/2022/01/03/linker_symbols.html" rel="alternate" type="text/html" title="C linker symbols" /><published>2022-01-03T14:00:00+00:00</published><updated>2022-01-03T14:00:00+00:00</updated><id>https://herzamos.ch/c/system-programming/2022/01/03/linker_symbols</id><content type="html" xml:base="https://herzamos.ch/c/system-programming/2022/01/03/linker_symbols.html">&lt;p&gt;Disclaimer: this post is a copy of an answer found on the VIS Community Solution platform. The answer was so well written that I felt it was a shame not to share it with as many people as possible. You can find the original answer &lt;a href=&quot;https://exams.vis.ethz.ch/exams/hw3wnogr.pdf#yvryiskgee1q7btg&quot;&gt;here&lt;/a&gt;, full credits are given to Theo Weidmann (&lt;a href=&quot;mailto:tweidmann@ethz.ch&quot;&gt;weidmann@ethz.ch&lt;/a&gt;).&lt;/p&gt;

&lt;h2 id=&quot;origin&quot;&gt;Origin&lt;/h2&gt;
&lt;p&gt;This answer originated from a discussion between students. The discussion was caused by a question which read as follows:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;For each listed identifier in the following C object file, say whether it is a strong linker symbol, a weak linker symbol, a macro, a symbol local to the compilation unit, or none of these.
The C program contained, together wiht many other stuff, the declaration (but not definition) of a function, the declaration of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extern char *&lt;/code&gt; and the definition of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;struct&lt;/code&gt;. All three of those were said not to belong to any of the above possibilities, and this generated a lot of confusion.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;what-is-a-symbol&quot;&gt;What is a symbol?&lt;/h2&gt;

&lt;p&gt;A symbol is a name for a memory address within assembly code.&lt;/p&gt;

&lt;p&gt;Let’s look at an example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test.s&lt;/code&gt;. In the below code doSomething and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;theCount&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doAnotherThing&lt;/code&gt; are symbols:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  .globl  theCount
  .bss
  .size   theCount, 4
theCount:

  .zero   4
  .text
  .globl  doSomething
doSomething:
  pushq   %rbp
  movq    %rsp, %rbp
  movl    $0, %eax
  call    doAnotherThing
  popq    %rbp
  ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;theCount&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doSomething&lt;/code&gt; appear as labels, i. e. they appear at the beginning of a line followed by a colon, which defines the symbol. By contrast, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doAnotherThing&lt;/code&gt; only appears as an instruction operand. We call a symbol that appears as an operand a reference.&lt;/p&gt;

&lt;p&gt;So &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test.s&lt;/code&gt; does define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;theCount&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doSomething&lt;/code&gt; but it does not define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doAnotherThing&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s assemble our test file into an object file:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gcc test.s -c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The assembler creates a symbol table as part of the object file, which is a list of all symbols it encountered. With each symbol, it notes whether the file defined or only referenced the symbol. We can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nm&lt;/code&gt; to look at the generated symbol table:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;                 U doAnotherThing
0000000000000000 T doSomething
0000000000000000 B theCount
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; mean that these symbols were defined in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bss&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;text&lt;/code&gt; section, respectively. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doAnotherThing&lt;/code&gt; means that the symbol is not defined but was referenced.&lt;/p&gt;

&lt;h2 id=&quot;what-are-symbols-and-the-symbol-table-good-for&quot;&gt;What are symbols and the symbol table good for?&lt;/h2&gt;

&lt;p&gt;Linking allows us to split up code. It allows us to define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doAnotherThing&lt;/code&gt; in one file and use it from another file.&lt;/p&gt;

&lt;p&gt;So let’s define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doAnotherThing&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;anotherThing.s&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  .globl  doAnotherThing
doAnotherThing:
  pushq   %rbp
  movq    %rsp, %rbp
  popq    %rbp
  ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If assembled, the symbol table looks like this:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0000000000000000 T doAnotherThing
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;So far, we have only created individual object files. To create a binary from these object files, we need to involve the linker.&lt;/p&gt;

&lt;p&gt;The linker reads all symbol tables and ensures that for every reference to a symbol, there is a definition of that symbol. In practice, this means checking that all symbols with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; entry in one symbol table have a defining entry in another table from another file.&lt;/p&gt;

&lt;p&gt;The actual step of replacing symbol names with addresses has been covered in exercises.&lt;/p&gt;

&lt;h2 id=&quot;what-about-strong-and-weak&quot;&gt;What about strong and weak?&lt;/h2&gt;

&lt;p&gt;Everything seems good so far. We can use symbols in one file and provide a definition in another file. But it turns out there are cases where we want to define the same symbol in multiple files.&lt;/p&gt;

&lt;p&gt;Take, for example, this (purely hypothetical) situation:&lt;/p&gt;

&lt;p&gt;We’ve built a huge library of very complex operations. Since most applications only ever use one of these complex operations, every operation (which consists of many functions) is provided as a single object file. Some of these operations need a helper function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;performCalculation&lt;/code&gt;. So we define it in the object file of each operation that needs it. But this leads to an interesting problem when someone links two object files that both contain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;performCalculation&lt;/code&gt; into their program. Which of the two definitions should the linker use?&lt;/p&gt;

&lt;p&gt;If we allowed any symbol to be defined more than once, this could have disastrous effects. What if we name two things the same by accident? So by default, the linker will only allow one definition of every symbol. We call symbols that can only be defined one time &lt;strong&gt;strong&lt;/strong&gt;. If a strong symbol is defined multiple times, the linker will give an error.&lt;/p&gt;

&lt;p&gt;Still, we want to solve our problem. We do so by introducing an option to allow multiple definitions of the same symbol. We call these symbols &lt;strong&gt;weak&lt;/strong&gt;. To fix our issue, we make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;performCalculation&lt;/code&gt; weak in all our files (note the first line):&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  .weak   performCalculation
performCalculation:
  pushq   %rbp
  movq    %rsp, %rbp
  movl    %edi, -20(%rbp)
  movl    %esi, -24(%rbp)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If there are multiple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;performCalculation&lt;/code&gt; definitions at link-time, the linker will choose any of these. Since all implementations of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;performCalculation&lt;/code&gt; are the same, this will work out fine. Problem solved.&lt;/p&gt;

&lt;p&gt;The relation between strong and weak was thoroughly covered in the lecture ETH “System Programming and Computer Architecture” course, 251-0061-00L.&lt;/p&gt;

&lt;h2 id=&quot;at-the-level-of-c&quot;&gt;At The Level of C&lt;/h2&gt;

&lt;p&gt;So what does all this mean at the level of C?&lt;/p&gt;

&lt;p&gt;First of all, it means that we can only define symbols that were actually defined in C code. If we have&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-C&quot;&gt; struct element *pull();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;we cannot meaningfully define this symbol. We don’t even know what this function does!&lt;/p&gt;

&lt;p&gt;The keyword extern means exactly the same thing:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-C&quot;&gt;extern char *otherstring;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It tells the compiler that there is a variable otherstring in one of the other translation units (resulting object files). But this is not the definition. So again, it would not make any sense to emit a symbol definition.&lt;/p&gt;

&lt;p&gt;These declarations are only necessary to allow the C compiler to do type checking. (As a matter of fact, early versions of C behaved much like assembly and allowed you to call undeclared, unknown functions…)&lt;/p&gt;

&lt;p&gt;This translation unit defines push, head and tail, so they will also result in a symbol definition.&lt;/p&gt;

&lt;p&gt;The symbol otherstring will not be referenced in the resulting object file, because we did not use it. If we used it, it would appear as an undefined reference in the symbol table.&lt;/p&gt;

&lt;p&gt;Note, that the compiler must always emit definitions as it cannot know which symbols other object files might reference. (The exception being static, of course.)&lt;/p&gt;

&lt;h2 id=&quot;final-remarks&quot;&gt;Final Remarks&lt;/h2&gt;

&lt;p&gt;But why are uninitialized globals weak? I would guess for historic reasons. The C standard actually prohibits defining variables more than once.&lt;/p&gt;

&lt;p&gt;And how to define a weak function in C? You cannot. But other compilers for other languages do emit weak functions.&lt;/p&gt;

&lt;p&gt;Where are you taking this from? Weak and strong were not made up by the lecture. It’s widely used. See for example, LLVM, the industry-leading tool for building compilers: &lt;a href=&quot;https://llvm.org/docs/LangRef.html#linkage-types&quot;&gt;https://llvm.org/docs/LangRef.html#linkage-types&lt;/a&gt;. (weak and strong is nonetheless an abstraction, the actual Linux implementation is more complicated.)&lt;/p&gt;</content><author><name>Amos Herz</name><email>herzamos@gmail.com</email></author><category term="C" /><category term="system-programming" /><category term="C" /><category term="system-programming" /><summary type="html">Disclaimer: this post is a copy of an answer found on the VIS Community Solution platform. The answer was so well written that I felt it was a shame not to share it with as many people as possible. You can find the original answer here, full credits are given to Theo Weidmann (weidmann@ethz.ch).</summary></entry><entry><title type="html">Deciphering C declaration swith the right-left rule</title><link href="https://herzamos.ch/c/system-programming/2021/12/27/deciphering-C-declaration.html" rel="alternate" type="text/html" title="Deciphering C declaration swith the right-left rule" /><published>2021-12-27T13:30:00+00:00</published><updated>2021-12-27T13:30:00+00:00</updated><id>https://herzamos.ch/c/system-programming/2021/12/27/deciphering-C-declaration</id><content type="html" xml:base="https://herzamos.ch/c/system-programming/2021/12/27/deciphering-C-declaration.html">&lt;p&gt;Disclaimer: this post is an adaptation of &lt;a href=&quot;https://cseweb.ucsd.edu//~ricko/rt_lt.rule.html&quot;&gt;this&lt;/a&gt; blog post, which I felt was not polished enough.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Have you ever looked at a C declaration, something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int *x(int *, int)&lt;/code&gt; and wondered “Well, what the hell is that?” before proceeding to get an headache trying to understand how to read it? If the answer is yes, then we are in the same boat! Or at least we were, until I discovered this simple rule which makes reading C declarations as easy as it can get.&lt;/p&gt;

&lt;h2 id=&quot;the-rule&quot;&gt;The Rule&lt;/h2&gt;
&lt;p&gt;For the following, keep this table in mind:&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Symbol(s)&lt;/td&gt;
      &lt;td&gt;To be read as&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;“pointer to”&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;“array of”&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;“function returning”&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The process is really simple, and it works as follow:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Find the identifier.  This is your starting point.  Then say to yourself, “identifier is.”  You’ve started your declaration.&lt;/li&gt;
  &lt;li&gt;Look at the symbols on the right of the identifier.  If, say, you find &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt; there, then you know that this is the declaration for a function.  So you would then have “identifier is function returning”.  Or if you found a  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt; there, you would say “identifier is array of”.  Continue right until you run out of symbols &lt;em&gt;OR&lt;/em&gt; hit a &lt;em&gt;right&lt;/em&gt; parenthesis &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;)&lt;/code&gt;.  (If you hit a  left parenthesis, that’s the beginning of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt; symbol, even if there is stuff in between the parentheses.  More on that below.)&lt;/li&gt;
  &lt;li&gt;Look at the symbols to the left of the identifier.  If it is not one of our symbols above (say, something like “int”), just say it.  Otherwise, translate it into English using that table above.  Keep going left until you run out of symbols &lt;em&gt;OR&lt;/em&gt; hit a &lt;em&gt;left&lt;/em&gt; parenthesis &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now repeat steps 2 and 3 until you’ve formed your declaration.&lt;/p&gt;

&lt;h2 id=&quot;some-examples&quot;&gt;Some Examples&lt;/h2&gt;
&lt;p&gt;(Taken from the autumun semester exam of the “System Programming and Computer Architecture” course at ETH, 251-0061-00L)
Say you want to decipher this not so simple C declaration:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Start by finding the identifier&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The declaration reads now as “x is.”. Proceed with step 2 and we get to the end of the line.&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;^^^^&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The declaration is now “x is an array 10 of.”.
Proceed with step 3 and we run out of symbols:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;^^^^^^&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Our declaration is finally “x is an array 10 of pointer to pointer to int.”.
The solution was indeed “declare x as array 10 of pointer to pointer to int”.&lt;/p&gt;

&lt;p&gt;Say you now want to decipher the following declaration:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Start with steps 1 and 2:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;o&quot;&gt;^^^^^^^^^^^^^&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The declaration is, up until now, “x is a function (pointer to int, int) returning.”
Now step 3:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;^^^^^&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We obtain “x is a function (pointer to int, int) returning pointer to int.”&lt;/p&gt;</content><author><name>Amos Herz</name><email>herzamos@gmail.com</email></author><category term="C" /><category term="system-programming" /><category term="C" /><category term="system-programming" /><summary type="html">Disclaimer: this post is an adaptation of this blog post, which I felt was not polished enough.</summary></entry><entry><title type="html">The Marching Cubes Algorithm 2: Meshes &amp;amp; Marching Squares</title><link href="https://herzamos.ch/marching-cubes/2021/04/10/marching-cubes-2.html" rel="alternate" type="text/html" title="The Marching Cubes Algorithm 2: Meshes &amp;amp; Marching Squares" /><published>2021-04-10T16:30:00+00:00</published><updated>2021-04-10T16:30:00+00:00</updated><id>https://herzamos.ch/marching-cubes/2021/04/10/marching-cubes-2</id><content type="html" xml:base="https://herzamos.ch/marching-cubes/2021/04/10/marching-cubes-2.html">&lt;h2 id=&quot;mesh&quot;&gt;Mesh&lt;/h2&gt;

&lt;h3 id=&quot;first-steps-into-meshes&quot;&gt;First steps into meshes&lt;/h3&gt;
&lt;p&gt;The first thing I had to do was to Google what a mesh is, because I really had no clue. A quick Wikipedia read and I am now a meshes expert! If you are also confused like I was by this term, it turns out being a really simple concept:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;In 3D computer graphics and solid modeling, a polygon mesh is a collection of vertices, edges and faces that defines the shape of a polyhedral object.
(Courtesy of &lt;a href=&quot;https://en.wikipedia.org/wiki/Polygon_mesh&quot;&gt;Wikipedia&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Even though I was trying to accomplish it in 2D, I think you got the idea (and if you didn’t, you wil soon, just keep reading :p): I am going to try to smooth out the borders of the caves from &lt;a href=&quot;https://www.herzamos.ch/marching-cubes/2021/04/09/marching-cubes-1.html&quot;&gt;my previous post&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;the-algorithm&quot;&gt;The algorithm&lt;/h3&gt;
&lt;p&gt;The algorithm is the same as the one we discussed before for the 3D case, but in 2D (that’s why it’s called “Marching Squares”). We start by dividing our space into squares, each one having his 4 vertices on 4 adjacent pixels in the drawing (see Figure 1 for an example).&lt;/p&gt;

&lt;div class=&quot;image&quot; style=&quot;text-align:center;margin-bottom: 10px;margin-top: 10px;&quot;&gt;
    &lt;img src=&quot;/assets/img/square-division.png&quot; alt=&quot;Image couldn't be loaded&quot; width=&quot;300&quot; style=&quot;margin-bottom: 10px;&quot; /&gt;
    &lt;figcaption&gt;Figure 1: An example of how the cave is divided into squares, using the seed &quot;Marching Cubes&quot;&lt;/figcaption&gt;
&lt;/div&gt;

&lt;p&gt;Each one of these squares will have 4 control points, one on each one of the square’s edges. These points will then be connected based on which vertices have to be inside the wall and which have to be outside; figure 2 displays all the (16) possible combinations.&lt;/p&gt;

&lt;div class=&quot;image&quot; style=&quot;text-align:center;margin-bottom: 10px;margin-top: 10px;&quot;&gt;
    &lt;img src=&quot;/assets/img/look-up-table-marching-squares.png&quot; alt=&quot;Image couldn't be loaded&quot; width=&quot;300&quot; style=&quot;margin-bottom: 10px;&quot; /&gt;
    &lt;figcaption&gt;Figure 2: All the possible combinations&lt;/figcaption&gt;
&lt;/div&gt;

&lt;div class=&quot;image&quot; style=&quot;text-align:center;margin-bottom: 10px;margin-top: 10px;&quot;&gt;
    &lt;img src=&quot;/assets/img/close-up-marching-squares.png&quot; alt=&quot;Image couldn't be loaded&quot; width=&quot;300&quot; style=&quot;margin-bottom: 10px;&quot; /&gt;
    &lt;figcaption&gt;Figure 3: Close up showing the &quot;control points&quot;&lt;/figcaption&gt;
&lt;/div&gt;

&lt;p&gt;Unfortunately, I had to stop working on this “series” of blog posts because of school reasons :( Hopefully, one day I’ll come back to it.&lt;/p&gt;</content><author><name>Amos Herz</name><email>herzamos@gmail.com</email></author><category term="marching-cubes" /><category term="marching-cubes" /><category term="computer-graphic" /><summary type="html">Mesh</summary></entry><entry><title type="html">The Marching Cubes Algorithm 1: Introduction &amp;amp; Procedural Cave Generation in 2D</title><link href="https://herzamos.ch/marching-cubes/2021/04/09/marching-cubes-1.html" rel="alternate" type="text/html" title="The Marching Cubes Algorithm 1: Introduction &amp;amp; Procedural Cave Generation in 2D" /><published>2021-04-09T16:30:00+00:00</published><updated>2021-04-09T16:30:00+00:00</updated><id>https://herzamos.ch/marching-cubes/2021/04/09/marching-cubes-1</id><content type="html" xml:base="https://herzamos.ch/marching-cubes/2021/04/09/marching-cubes-1.html">&lt;h2 id=&quot;the-marching-cubes-algorithm&quot;&gt;The marching cubes algorithm&lt;/h2&gt;

&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;
&lt;p&gt;During the holydays, since ETH wasn’t giving me enough work to do, I decided to learn how to use &lt;a href=&quot;https://unity.com/&quot;&gt;Unity&lt;/a&gt;.
While deciding the project to implement, I stumbled upon the marching cube algorithms, so now here we are!&lt;/p&gt;

&lt;h3 id=&quot;the-problem&quot;&gt;The problem&lt;/h3&gt;
&lt;p&gt;The main need the Marching Cube Algorithm tries to satisfy, is the need to form a facet approximation to an isosurface through a scalar field, sampled on a rectangle grid. A lot of big words huh? Let’s make this simple: imagine to have a lot of points nicely distributed on a section of space. We now decide which of these points we want to be contained in our 3d surface, and which not. There we are! We can now pass our “marching cube” over all the points and make it draw triangles to “exclude” external vertices: each triangle will have his vertices between an excluded cube vertex and an included one (see Figure 1 for a reference).&lt;/p&gt;

&lt;h3 id=&quot;the-algorithm&quot;&gt;The algorithm&lt;/h3&gt;
&lt;p&gt;The main problem of this algorithm is the amount of data we’d need to compute, but luckily someone has already done that for us, and pre-computed tables are &lt;a href=&quot;http://paulbourke.net/geometry/polygonise/&quot;&gt;available online&lt;/a&gt;.
As everyone knows a cube has 8 vertices. Since each one of those vertices can be either inside the isosurface or outisde, there are \(2^{8} = 256\) possible combinations of vertices. Luckily (again!) only \(14\) of those are actually relevant, since all the other are just rotations or mirrorings of these “base” cases.&lt;/p&gt;

&lt;div class=&quot;image&quot; style=&quot;text-align:center;margin-bottom: 10px;margin-top: 10px;&quot;&gt;
    &lt;img src=&quot;/assets/img/cube-combinations.jpg&quot; alt=&quot;Image couldn't be loaded&quot; width=&quot;300&quot; style=&quot;margin-bottom: 10px;&quot; /&gt;
    &lt;figcaption&gt;Figure 1: The fundamental cases of the marching cubes algorithm&lt;/figcaption&gt;
&lt;/div&gt;

&lt;h2 id=&quot;cave-generation&quot;&gt;Cave Generation&lt;/h2&gt;

&lt;h3 id=&quot;starting-slow&quot;&gt;Starting slow&lt;/h3&gt;
&lt;p&gt;Before going 3D, I thought starting with a 2D cave generator would have been a good idea to first learn the basics of Unity. That’s when I encountered &lt;a href=&quot;https://www.youtube.com/watch?v=v7yyZZjF1z4&quot;&gt;this&lt;/a&gt; great video series, which helped me learn a lot.&lt;/p&gt;

&lt;h3 id=&quot;first-steps-into-unity&quot;&gt;First steps into Unity&lt;/h3&gt;

&lt;p&gt;The first thing I got to work was generating a 2D map, made of squares which are randomlz chosen to be black or white (Figure 2).&lt;/p&gt;

&lt;div class=&quot;image&quot; style=&quot;text-align:center;margin-bottom: 10px;margin-top: 10px;&quot;&gt;
    &lt;img src=&quot;/assets/img/cave-gen-1.png&quot; alt=&quot;Image couldn't be loaded&quot; width=&quot;300&quot; style=&quot;margin-bottom: 10px;&quot; /&gt;
    &lt;figcaption&gt;Figure 2: A first approach to a random generated map&lt;/figcaption&gt;
&lt;/div&gt;

&lt;p&gt;As you maybe already noticed, this set-up really looks like the starting point of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life&quot;&gt;Conway’s Game of Life&lt;/a&gt; game. Indeed, the procedural map generation will be based on &lt;a href=&quot;https://en.wikipedia.org/wiki/Cellular_automaton&quot;&gt;cellular automation&lt;/a&gt;, but not on the set of rules Conway had defined.  &lt;br /&gt;
The map generation is based on a seed system, and I also added some variables to play with for the map generation, such as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fillPercent&lt;/code&gt; field, with whom I could easily manage how much i wanted the map to be filled.&lt;/p&gt;

&lt;p&gt;The rule I’ve used are failry simple:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;If the cell as more then 4 alive neighbour cells, make it alive;&lt;/li&gt;
  &lt;li&gt;If it has less than 4 alive neighbour cells, make it die;&lt;/li&gt;
  &lt;li&gt;If it has exactly 4 alive neighbours, let it be as it is;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This precise rule is what I found working the best to generate a cave-resembling shape. Moreover, I’ve added some tweaks to the code to make sure that near walls more cells become alive, and ensure that the more we go towards the end of the cave, the thicker the walls are. I decided to apply the rule for 5 iterations, which seemed to work just fine. Some results can be obvserved below.&lt;/p&gt;

&lt;div class=&quot;image&quot; style=&quot;text-align:center;margin-bottom: 10px;margin-top: 10px;&quot;&gt;
    &lt;img src=&quot;/assets/img/cave-gen-results.png&quot; alt=&quot;Image couldn't be loaded&quot; width=&quot;600&quot; style=&quot;margin-bottom: 10px;&quot; /&gt;
    &lt;figcaption&gt;Figure 3: Some of the results of the algorithm: on the left, two results displayed on a 128x64 grid; on the right, two bigger grids (500x250 on the top, 1000x500 on the bottom). All measures in &quot;squares&quot;.&lt;/figcaption&gt;
&lt;/div&gt;

&lt;p&gt;In the end I played around with grid dimension, seeds, number of iterations and also tried changing the rule threshold (which was previously 4), but I didn’t get any result worth mentioning.&lt;/p&gt;</content><author><name>Amos Herz</name><email>herzamos@gmail.com</email></author><category term="marching-cubes" /><category term="marching-cubes" /><category term="computer-graphic" /><summary type="html">The marching cubes algorithm</summary></entry></feed>