An example of Linux/glibc symbol versioning.

September 20, 2019 C/C++ development and debugging. 3 comments , , , , , , ,

Here’s a little introduction to Linux/glibc symbol versioning.  The sources, linker version scripts, and makefile from this post can be found here in github.

The definitive reference for this topic is Ulrich Drepper’s dsohowto document.

Linux symbol versioning is a very logical way to construct a definitive API for your product.  If you are careful, you can have a single version of your shared library that is binary compatible with all previous versions of code that have linked it, and additionally hide all symbols that you don’t want your API consumer to be able to use (as if all non-exported symbols have a “static” like scope within the library itself).

That visibility hiding can be done in other ways (such as using static, or special compiler options), but utilizing a symbol version linker script to do so also has all the additional binary compatibility related benefits.

Suppose that we have a “v1.1” API with the following implementation:

#include <stdio.h>

void internal_function_is_not_visible()
{
   printf( "You can't call me directly.\n" );
}

void foo( int x )
{
   internal_function_is_not_visible();

   printf( "foo@@V1: %d\n", x );
}

and we build this into a shared library like so:

$ make libfoo.1.so
rm -f libfoo.1.so
cc foo.c -g -Wl,--version-script=symver.map -fpic -o libfoo.1.so -Wl,-soname,libfoo.so -shared
rm -f libfoo.so
ln -s libfoo.1.so libfoo.so

Everything here is standard for building a shared library except for the –version-script option that is passed into the linker with -Wl,. That version script file has the following contents:

$ cat symver.map
MYSTUFF_1.1 {
	  global:
foo;
	  local: *;
	};

This defines a “V1.1” API where all the symbols that are exported with a symbol version @@MYSTUFF_1.1. Note that internal_function_is_not_visible is not in that list. It’s covered in the local: catch-all portion of the symbol version file. Code that calls foo does not look out of the ordinary:

void foo(int);

int main()
{
   foo(1);

   return 0;
}

Compiling and linking that code is also business as usual:

$ make caller1
cc caller.c -g -Wl,-rpath,`pwd` -o caller1 -lfoo -L.

However, look at the foo symbol reference that we have for this program:

$ nm caller1 | grep foo
                 U foo@@MYSTUFF_1.1

If we run this, we get:

$ ./caller1
You can't call me directly.
foo@@V1: 1

If you add in a call to internal_function_is_not_visible() you’ll see that compilation fails:

void foo(int);
void internal_function_is_not_visible();

int main()
{
   foo(1);
   internal_function_is_not_visible();

   return 0;
}
$ make caller1
cc caller.c -g -Wl,-rpath,`pwd` -o caller1 -lfoo -L.
/run/user/1002/ccqEPYcu.o: In function `main':
/home/pjoot/symbolversioning/caller.c:7: undefined reference to `internal_function_is_not_visible'
collect2: error: ld returned 1 exit status
make: *** [caller1] Error 1

This is because internal_function_is_not_visible is not a visible symbol. Cool. We now have versioned symbols and symbol hiding. Suppose that we now want to introduce a new binary incompatible change too our foo API, but want all existing binaries to still work unchanged. We can do so by introducing a symbol alias, and implementations for both the new and the OLD API.

#include <stdio.h>

void internal_function_is_not_visible()
{
   printf( "You can't call me directly.\n" );
}

void foo2( int x, int y )
{
   if ( y < 2 )
   {
      internal_function_is_not_visible();
   }
   printf( "foo@@V2: %d %d\n", x, y );
}

void foo1( int x )
{
   internal_function_is_not_visible();
   printf( "foo@@V1: %d\n", x );
}

This is all standard C up to this point, but we now add in a little bit of platform specific assembler directives (using gcc specific compiler sneaks) :

#define V_1_2 "MYSTUFF_1.2"
#define V_1_1 "MYSTUFF_1.1"

#define SYMVER( s ) \
    __asm__(".symver " s )

SYMVER( "foo1,foo@" V_1_1 );
SYMVER( "foo2,foo@@" V_1_2 );

We’ve added a symbol versioning alias for foo@@MYSTUFF_1.2 and foo@MYSTUFF_1.1. The @@ one means that it applies to new code, whereas the @MYSTUFF_1.1 is a load only function, and no new code can use that symbol. In the symbol version script we now introduce a new version stanza:

$ cat symver.2.map
MYSTUFF_1.2 {
	  global:
foo;
};
MYSTUFF_1.1 {
	  global:
foo;
	  local: *;
};

$ make libfoo.2.so
rm -f libfoo.2.so
cc foo.2.c -g -Wl,--version-script=symver.2.map -fpic -o libfoo.2.so -Wl,-soname,libfoo.so -shared
rm -f libfoo.so
ln -s libfoo.2.so libfoo.so

If we call the new V1.2 API program, like so:

void foo(int, int);
int main()
{
   foo(1, 2);
   return 0;
}

our output is now:

$ ./caller2
foo@@V2: 1 2

Our “binary incompatible changes” are two fold. We don’t call internal_function_is_not_visible if our new parameter is >= 2, and we print a different message.

If you look at the symbols that are referenced by the new binary, you’ll see that it now explicitly has a v1.2 dependency:

$ nm caller2 | grep foo
                 U foo@@MYSTUFF_1.2

Book sales stats: slow and steady

September 2, 2019 Incoherent ramblings No comments

I don’t have full stats for all my kindle-direct sales, but am surprised how steady the sales for the GA book have been

Jan 1- March 31 2019 April 1 – June 30, 2019 June 4 – Sept 1, 2019

Geometric Algebra for Electrical Engineers

24 24 26

Statistical Mechanics

3 2

Relativistic Electrodynamics

2

Classical Optics

4

Quantum Field Theory

2 2

Condensed Matter

1

Graduate Quantum Mechanics

1

Some of the earlier sales were to family members who wanted copies, but the rest are legitimate.

An easy fix for a loose electrical outlet

September 1, 2019 Home renos No comments

We’ve had a dangerously seeming loose wall outlet since we moved in, and I’ve been meaning to deal with it forever. This was one of those little projects that I procrastinated forever, since it was one that I figured would be messy, but also one that I knew I wouldn’t know what I had to do until I started.

Here’s the outlet with the cover off

one side sticks out about 3/8″ of an inch, and the whole thing is loose, with about a 1/4″ of play in many spots.

As it turned out, the stud that the outlet was attached to was rotated about 35 degrees, which was the root of the problem. You can see that in this picture, if you look closely, as I have the blade of the drywall saw at the bottom of the hole perpendicular to the wall surface, and touching the back edge of the stud:

I ended up cutting a giant hole, big enough that I could try to run a new parallel stud. However, reaching in to my giant hole, I found that there was enough slack that I could reroute the wires to the unused box beside it. That unused box had an unused telephone wire it, and has always had a blank cover plate on it:

Despite being a box for telephone wire, it wasn’t a low voltage box, but was a standard electrical grade box, so I was able to fix the loose outlet by just recycling the unused box for the once loose outlet:

 

Now I just have a hole to patch:

I’ve put in strapping to anchor the drywall patch to, but it seems that I’ve thrown out all my drywall scraps, so the next steps will have to wait a bit.

John Grisham: The Brethren

August 31, 2019 Incoherent ramblings No comments ,

(spoilers here)

This was an enjoyable book, and a page turner, even if it’s a bit predictable, and contained a few large holes in the plot logic.  The basic idea is that there’s a group of incarcerated older judges in a federal prison, who with nothing left to loose, concoct a blackmail scam.  They use their lawyer as a mule for gay hook-up themed “penpal” letters.  After some private investigator work, also initiated by their lawyer, they try to discover the real identities of their correspondents, looking for in-the-closet married men that are nicely blackmailable.

This blackmail story is intertwined with story of a senator who is determined by the head of the CIA, to be “so clean” that he is a good candidate to secretly finance for a can’t lose presidential run.  I found that idea to be pretty naive and comical, as it goes against my suspicion that many politicians win their selections because they can’t be compromised, but are pushed to positions of head-clown and distraction-chiefs precisely because they are compromised.  In this book, this new would be presidential candidate selection is promised the job if he exclusively pushes a help the military become great again agenda, which will be aided by convenient terrorism incidents, and massive sums of PAC money from military-industrial people and individuals.  Clearly Mr So Clean, is intrinsically dirty under the covers, as he has no objections to people dying in these engineered terrorism incidents if it gets him into the presidential role.  Of course, he’s also been secretly participating in some gay procurement penpal letters courtesy of the judges, and you can tell it’s only a matter of time before his true identity becomes known to the judges, and they get ready for their best blackmail haul.

Complicating things for the judges is the fact that the CIA watches their soon to be president carefully, and they discover the blackmail plot to be before their man, and intercept the situation.  The lawyer is first paid off and then taken out, and eventually the CIA director swings presidential pardons (from the lame duck president, in exchange for past favors) for the judges, and gets them all paid off and safely out of the country.

It’s a kind of weird ending, because the soon to be president has been saved from blackmail (by resources and gobs of CIA dirty black money), and the judges are out of jail.  Everybody wins except the letter mule lawyer who was taken out while attempting to run with some of that CIA cash.  This “good ending” obscures the fact that the new president is a scumbag that didn’t have any trouble killing a pile of innocents to get the job.  In that respect, he’s not much different than Trump, Obama, either of the Clintons, or either of the Bushes.

I enjoyed this book, but it assembled some strange conspiracy-theory style themes, in ways that just don’t make sense.

My office hardware, fully deployed today

August 30, 2019 Incoherent ramblings No comments

I’ve got all my desktop hardware deployed today:

  1. Intel NUC6i7KYB (Skull canyon NUC)
  2. Intel NUC6i7KYB
  3. Large cup of coffee
  4. Mac thunderbolt monitor
  5. Mac thunderbolt monitor
  6. Monitor for my NUCs (not usually connected)
  7. Mac Laptop, a really expensive way to run terminal (to access my NUCs)
  8. NUC keyboard (not usually connected)
  9. Keyboard for my mac
  10. Mac trackpad
  11. NUC mouse (not usually connected)

(and half concealed by monitor (4) is my WD “My Book” backup drive for the Mac).

The lldb TUI (text user interface)

August 26, 2019 C/C++ development and debugging. No comments , , ,

It turns out, like GDB, that lldb has a TUI mode too, but it’s really simplistic.  You enter with

(lldb) gui

at which point you get a full screen of code or assembly, and options for register exploration, thread and stack exploration, and a variable view.  The startup screen looks like:

If you tab over to the Threads window, you can space select the process, and drill into the stack traces for any of the running threads

You can also expand the regsiters by register class:

I’d like to know how to resize the various windows.  If you resize the terminal, the size of the stack view pane seems to remain fixed, so the symbol names always end up truncated.

Apparently this code hasn’t been maintained or developed since it was added.  Because there is no console pane, you have to set all desired breakpoints and continue, then pop into the GUI to look at stuff, and then <F1> to get back to the console prompt.  It’s nice that it gives you a larger view of the code, but given that lldb already displays context around each line, the lldb TUI isn’t that much of a value add in that respect.

This “GUI” would actually be fairly usable if it just had a console pane.

My youngest reader

August 23, 2019 Incoherent ramblings No comments ,

My nephew Jake is a prodigy, and is already tackling QM!

Two more books dispatched: Art of the Deal, and Deception Point.

August 3, 2019 Incoherent ramblings, Reviews No comments , , , ,

I’ve been working hard to take down my backlog of books to read, and have now finished two more.

1) Trump’s: The Art of the Deal.

Other possible alternate titles for this book would be “How I financed my projects at others’ expense using  tax rebates and other tricks”, and “How I used PR to get what I want.”  Reading this leaves you with the slightly nauseous feeling that you have after talking to a slimy used car salesman.  A lot of what was stated left me with the feeling that relevant facts were being omitted.  I’d like to see a fact checking “Coles Notes” for this book, and to look at how the projects that are named in the book are doing now.

I am inclined to enumerate all the people that Trump mentions in the book and dig into the relationships that Trump took the time to name drop in this book.  Trump’s pedophile buddy Epstein didn’t make the cut in the book, but Adnan Khashoggi did. A lot of the other names I didn’t recognize.

EDIT: here’s some backstory on the book.  Included in that article was one more interesting name drop, Roy Cohn, Trump’s lawyer.  That name may have been mentioned in the book, but if so, I didn’t recognize it when I read that part of the book.  With Epstein’s case reactivated, I now recognize Cohn’s name from Whitney Webb’s writing [1], [2] and her interview with Pierce Redmond.

2) Dan Brown’s: Deception point.

This book would be a lot better as a movie. Like a lot of Michael Crichton books, this one moves very fast, but is pretty shallow, as well as predictable and probably forgettable.  I did enjoy it, but it’s definitely not one to keep, and I intend to bring it to the second hand bookstore, or if they don’t want it, to the communal take-or-leave a book shelf at the recycling depot.

References

[1] Hidden in Plain Sight: The Shocking Origins of the Jeffrey Epstein Case

[2] Government by Blackmail: Jeffrey Epstein, Trump’s Mentor and the Dark Secrets of the Reagan Era

Gatto’s “Dumbing Us Down”

July 20, 2019 Incoherent ramblings 1 comment , , ,

 

I’ve just read John Taylor Gatto’s “Dumbing Us Down, the hidden curriculum of compulsory schooling.”  I’ve heard Brett Veinotte on the School Sucks Podcast talk about Gatto’s exposition of the origins of the North American school system.  Given that, I expected a lot more from this particular book.  Instead this book is a largely a collection of speeches, converted into essay form, as opposed to a systematic deconstruction of the school system.

I did enjoy those essays, but my reaction included a lot of “Sir, you are preaching to the choir.”  I am guessing that the book that I really wanted was his “The Underground History of American Education“, which weighs in at ~450 pages.

A interesting video with some analysis of the Wim Hof method

July 14, 2019 Incoherent ramblings 3 comments , , ,

I’d heard the Wim Hof interview on Joe Rogan a while ago, which was pretty interesting.

Due to the indoctrination of my youth(*), I recognize that I’m predisposed to the idea that the mind can control the body, so the techniques that Wof described in the Rogan podcast seemed plausible.  However, that plausibility wasn’t enough to make me want to spend the money me to purchase his book.

I have to admit that I did try some Wim Hof style intense breathing before jumping in the late fall 50F pool, after some time in the hot tub.  I suspect that I was not doing the breathing exercises correctly.  At that pool temperature, without the ablity to self-regulate my body heat, I find that I don’t warm up, even after a number of laps.

For anybody that finds the idea of body self regulation interesting, here is some analysis of the Wim Hof method on the medlife youtube channel.  It may not be the way to acquire Bene Gesserit like abilities, however,  if you also have the urge to play the hot-tub/cold-pool alternation game, or climb mountains, it does sound like the breathing techniques are worth knowing.


Footnotes:

(*) I grew up Scientology household where the actor at the head of the body-mind-spirit story is an all-powerful entity, somewhat akin to a Jin, but in need of significant re-training.